How to template more or less a value with is_states or is_number?

Trying to do a math calculation in a template sensor only if another criteria is met.

Similar to this, but the >1000 doesn’t do anything:

"{% if is_number('sensor.three') >1000 %}{{ states('sensor.one') + states('sensor.two') }}{% else %}0{% endif %}"

If sensor.three is above 1000, then is should calculate sensor.one+sensor.two.
Otherwise set to 0.

1 Like

is_number is a boolean operation to check if an entity is an integer or float. Use states and convert to int to perform the operation.

states('sensor.three')|int >1000
3 Likes
if states('sensor.three') | float > 1000

is_number(…) returns True or False, if the state is numeric, but does not return the state‘s value as number.

1 Like

Thanks, works much better :slight_smile:

Ah, thanks!

Hi
I’m trying to do something similar, but with no luck.
Can someone help me with this code?
What I want is to return 0 if (inv1 + inv2) returns “unavailable”.

  - sensor:
      - name: "Total Watt Produksjon"
        unit_of_measurement: "W"
        state: >
          {% set inv1 = states('sensor.solisL.active_power') | float %}
          {% set inv2 = states('sensor.solisH.active_power') | float %}
          {% set inv3 = (inv1 + inv2) %}
          {% if states('inv3') | float > 1 %}
            {{ inv3 | round(0) }}
          {% else %}
            {{ 0 }}

{{ iif ( not is_number ( inv1 ) and not is_number ( inv2 ) , 0 , 1 )  }}

Like this?

state: >
          {% set inv1 = states('sensor.solisL.active_power') | float %}
          {% set inv2 = states('sensor.solisH.active_power') | float %}
          {{ iif ( not is_number ( inv1 ) and not is_number ( inv2 ) , 0 , 1 )  }}

I’m not sure what you are trying to do? but I think you want

state: >
          {% set inv1 = states('sensor.solisL.active_power') | float %}
          {% set inv2 = states('sensor.solisH.active_power') | float %}
          {% set inv3 = iif (is_number (inv1 + inv1) , inv1 + inv1 , 0) %}