lucruc
January 13, 2022, 8:08pm
1
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
m0wlheld
(Christoph Dahlen)
January 13, 2022, 8:45pm
3
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
lucruc
January 15, 2022, 7:19pm
4
Thanks, works much better
abbamust
(abbamust)
November 21, 2022, 11:23am
7
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 ) }}
abbamust
(abbamust)
November 21, 2022, 1:18pm
9
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) %}