Template getting non-numeric value after filtering unknown values

Hi

At the beginning I was getting a lot of this kind of values when my zigbee devices were providing and “unknonw” value. After correcting the code, This is the only one I’m getting and I don’t understand the reaso why, becouse I’m filtering the non numeric values in the template configuration.

This is the error:

ValueError: Sensor sensor.salon_target_temperature has device class ‘temperature’, state class ‘measurement’ unit ‘°C’ and suggested precision ‘None’ thus indicating it has a numeric value; however, it has the non-numeric value: ‘unknown’ (<class ‘str’>)

And this is my template configuration (done with the UI)

{% set T1 = state_attr('climate.termostato_salon','temperature') %}

{% if T1|is_number %}
  {{ (T1 )  | round(1) }}
{% else %}       
  {{ this.state }}
{% endif %}

What do you get with this code in Developer Tools?

{{ state_attr('climate.termostato_salon','temperature') }}

You’ll see something like this on the right… Result type: number

I’d share the entire template code

Hi

That’s the complete template code. It is configure in the UI, not in the configuration.yaml

Whit that code i’m getting the same i’m usually getting with my template, a number with the value of the variable in my zigbee device. The problem is that when there is a problem on the zigbee network or the HAOS or zigbee addon is restarting, I’m getting this unknown value and the error appears in the logs.

To be clear you are using Z2M addon, correct?

Yes, that’s correct.

At the beggining I was getting a lot of errors with multiple templates from zigbee variables. A similar template configuration as the above solved all except this one.

Can you share the device’s data via Developer Tools?

it may be {{ state_attr('climate.termostato_salon','current_temperature') | int()}}

vs temperature


Temperature and current_temperature are different variables. Temperature is the setpoint and Current_temperature is the measurement.

Thanks for the screenshot…Try

{% set T1 = state_attr('climate.termostato_salon','temperature') | int(0) %}

{% if T1|is_number %}
  {{ (T1 )  | round(1) }}
{% else %}       
  {{ this.state }}
{% endif %}

sorry, but if I understand your code correctly, it is defining a default value = 0. Is my asumption correct?

That’s not an option because I’m using this template to replicate the setpoint temperature across multiple climate entities (radiator valves). If the value is set to 0 everytime there is an error in the zigbee network, it will change the setpoint of the radiators valves to 0.

This would cause the valve to move from 100% open to 0% and coming back to 100% when the actual value is read, consuming the batteries of the valves.

I just wanted you to test that value. It feels like its struggling with {{ this.state }}

Test

{% else %}       
 {{ (T1) }}
{% endif %}

This adds another level to consider. Your OP didn’t make it clear you were using multiple device attributes.

Did this not get solved previously?

As you can see is a different problem. That problem was solved correcting the templating format, but in this case the problem is with the non-numeric values

All values start as a string

I dont’ understand your point here:

This adds another level to consider. Your OP didn’t make it clear you were using multiple device attributes

The problem origin is here, when assigning value to the template variable, not when reading it to use it in the automation

That shouldn’t matter as long as the string is converted to an integer

thanks for clarifying, very helpful.

What i meant was that the problem is when I’m getting a string that cannot be converted to a numeric value.

The code should filter those values to avoid being written into the template but is not working as expected.

Can we focus now in the problem?

I am not trying to be difficult and want to help. I just came across the previous thread.

I tested your code against my climate device multiple ways, but where we disagree is that a default needs to be set .

In my opinion, when setting the temperature across multiple climate entities (radiator valves) there still needs to be a default value if representing an integer.

How are you combining the radiator values?

Your template does not sufficiently account for how the this variable works when the new sensor entity is created.

{% set T1 = state_attr('climate.termostato_salon','temperature') %}
{% if T1 | is_number %}
  {{ (T1 )  | round(1) }}
{% else %}       
  {{ this.state if this is not none 
  and this.state is defined else None }}
{% endif %}

FWIW, using trigger-based template sensors allows more control without having to use convoluted expressions to handle this. However, they cannot currently be configured as a Helper. You have to add them to your YAML configuration.

template:
  - trigger: 
      - trigger: state
        entity_id: climate.termostato_salon
        attribute: temperature
    condition:
      - "{{ trigger.to_state.attributes.temperature | is_number }}"
    sensor:
      - name: Salon Temperature
        state: "{{ trigger.to_state.attributes.temperature }}"