Convert to numeric value

Hi,

I´m using a rest interface to read out some temperatures, but I have now got this warning

Sensor sensor.eta_utetemp has device class temperature, state class None and unit °C thus indicating it has a numeric value; however, it has the non-numeric value: −1,6 (<class ‘str’>)

the integration looks like this:

- platform: rest
  resource: http://192.xxx.xx.xx:8080/user/var/24/10241/0/0/12197
  name: eta_utetemp
  device_class: temperature
  unit_of_measurement: "°C"
  value_template: '{{ value_json["eta"]["value"]["@strValue"] }}'

How do I convert the value to numeric?

You need to convert your European “decimal comma” to a decimal point:

  value_template: '{{ value_json["eta"]["value"]["@strValue"].replace(",",".") }}'

Thanks,

but it still get this error

Sensor sensor.eta_utetemp has device class temperature, state class None and unit °C thus indicating it has a numeric value; however, it has the non-numeric value: −0.4 (<class ‘str’>);

But now with . instead of ,

name: eta_utetemp

  device_class: temperature
  unit_of_measurement: "°C"
  value_template: '{{ value_json["eta"]["value"]["@strValue"].replace(",",".") }}'

Is that an en-dash rather than a minus sign?! Who designed that sensor?!

value_template: '{{ value_json["eta"]["value"]["@strValue"].replace(",",".").replace("–","-") }}'
# copy this character from the error message------------------------------------------^

I assume from your ["@strValue"] that this is coming from XML, and that “strValue” suggests it’s a “human-readable” output. Is there another value attribute that would be better to read?

It would be this (i have secret editing powers to copy from his post), which is slightly different character from what you had.

value_template: '{{ value_json["eta"]["value"]["@strValue"].replace(",",".").replace("−","-") }}'

:smiley: I wasn’t trusting the forum software not to have changed it like it does with quotes. Covering both bases:

value_template: '{{ value_json["eta"]["value"]["@strValue"].translate({44:46,8211:45,8722:45}) }}'

Yeah, so I can click posts and “edit” them. Which shows the raw un-forum-tainted text. Then I just cancel the edit. Making it code, like in my post above, leaves the characters unaltered. So your template with the character change in my post should work.

1 Like

It was as you sad, not an minus sign. Now I have to wait until the temperature drops below zero again to see if it works :slight_smile:

Thanks!

Your correct it´s an XML, unfortunately are there no other data objects.

The sensor comes from a Wood Chip Boiler, ETA. However they have now opened up a TCPModus interface maybe it´s better to go over to that interface.

To be fair, − is a minus sign. It’s just not one that any programming language uses.

But what if some template sensor is “unknown” on HA startup?
Log is flooded by these warnings.

Then you need to account for that by applying an availability template or outputting a default value.

Agree, in 95% of my templates sensors there is an “availability” attribute to handle “bad” input data; so these sensors are either with some “good” data or “unavailable”.

But there are template sensors for which:

  • “availability” = true;
  • state = either processed “good” data or PREVIOUS data if input data are “bad”; this behavior is needed since I want to memorize last “good” data.

And because of unclear reasons these sensors are “unknown” on HA startup.

Yes, during that time you’ll get the warnings unless you provide a default for that case.

But I cannot realize from the sensor’s point of view when this sensor is about to become “unknown”.
Here is a simplified logics:

state: >-
  if states(some-source-sensor) in [‘unavailable’,’unknown’]
    states(this-sensor)
  else
    states(some-source-sensor)

when restarting, all states aren’t populated. So you’re getting nothing in your else.

Shall I try this code?

if states(some-source-sensor) in [‘unavailable’,’unknown’]
    If states(this-sensor) != ‘unknown’
      states(this-sensor)
    else
      ?? some-default-value ??
  else
    states(some-source-sensor)

?

And defining that “default value” is not an easy task too. Ideally it must be the last “good” value.

Not clear((. Assume some source sensor is NOT unavailable or unknown - then it anyway may be not populated?

When you restart, HA does not restore pre-restart states. So the state objects in memory are empty. HA has been like this for years. There’s a few things that can restore states. Some template sensors can restore states, but I believe that’s trigger only template sensors. So you have to account for a restart. I’m not sure why you don’t just make it unavailable as that’s the most appropriate for the situation. When the other sensor that it feeds from comes back to life, your sensor will no longer be unavailable.

Probably there is a language barrier between us which prevents understanding.
Let me explain then.

As I said this is what I do for 95% of my sensors:
– source sensor is ok - then use it’s data;
– otherwise (device is offline, sensor became unavailable, data are invalid - like negative battery_level etc) availability=false.

But imagine a special case: if source data are bad, the PREVIOUS good data are used.
Agree that in general this could be a bad approach: you are using OLD data, which may be not valid now.

But in some cases the source device is not sending changed data very often (i.e. these data do not change very often), or these changes are not critical.
Imagine a vacuum robot, an air purifier which send data about their filters: in both cases filters are degrading slowly; in case of vacuum - it degrades only after cleaning.
In similar cases it is rather OK to use previous data when currently data are absent (assume that you switched off the vacuum - but still interested to see data about filters; and same about jet printer etc).

So, for these cases algorithm is like this:
– source sensor is ok - then use it’s data;
– otherwise - use previous data (i.e. the current sensor’s state);
– availability=true (always).

It works fine and allows to see the last “good” data if currently data are not available.

The only problem occurs on HA startup - these sensors become “unknown”.
This was not a problem; but since 2023.2 these events are reported in the Log; surely I may filter them in “logger”, but I want to make algorithms better.

You wrote - “ When you restart, HA does not restore pre-restart states. So the state objects in memory are empty. ”
So these “previous data” are supposed to be not available on startup, yes?
But after HA startup I still see data about filters in a vacuum robot from yesterday:
– yesterday the sensor got this data about filters;
– today HA was restarted;
– on HA startup the sensor became “unknown”;
– then the sensor got the yesterday’s data about sensor since the vacuum is OFF.
And here I do not understand a reason of this “unknown” state.