If condition with a rounded sensor

Hello you all,

I want to have an icon in different colours based on the condition of a sensor.

The sensor however doesn’t have “clear” values like 0, 1, 2 … but is like 0,0035 or 6,032 - so it needs to be rounded. The sensor has “Ampere” (A) as unit.

How do I do that?

I know this code:

  {% if is_state('sensor.mysensor', '0 A') %}
    red
  {% else %}
    grey
  {% endif %}

But it always shows grey. If I use if states, the sensor is always red - no matter how the value is. How do I do that?

Thank you in advance!

Try this:

{% if states('sensor.mysensor') | float(0) | round(0) == 0 %}
  red
{% else %}
  grey
{% endif %}

Or it’s shortest version:

{{ 'red' if states('sensor.mysensor') | float(0) | round(0) == 0 else 'grey' }}
1 Like

Or if you are looking for multiple above / below etc, try something like this:

{% if states('sensor.mysensor') | float(0) < 100 %}
red
{% elif states('sensor.mysensor') | float(0) < 200 %}
blue
{% elif states('sensor.mysensor') | float(0) < 300 %}
pink
{% else %}
grey
{% endif %}
1 Like
{{ iif(states('sensor.mysensor') | round(default=0) == 0, 'red', 'grey') }}
1 Like

Basically, let’s say your sensor returns 3.141694. Then, those will be the results:
states('sensor.mysensor') will return the raw content of the sensor (maybe as a string), like "3.141694".
states('sensor.mysensor') | float(0) will convert the result to a number (with decimals) and if it’s not a number it will convert to 0, so 3.141694 (as a number).
states('sensor.mysensor') | float(0) | round(0) will round that number with 0 decimals, so 3.
"3.96" | float(0) | round(0) returns 4.
"3.96" | int(0) returns 3, the integer part (not rounded).
"unknown" | int(-99) returns the default integer, -99.

1 Like

THANK YOU ALL!

How would I combine two sensor conditions?
e.g. sensor1 = 0 and sensor2 = 1000

And how would I do it if sensor is between X and Y?

Thank you in advance!

For future reference, there’s no obligation to use float before round.


That’s why I omitted it in my example above.

{{ iif(states('sensor.mysensor') | round(default=0) == 0, 'red', 'grey') }}
2 Likes

That’s right, but just remember {{ "unavailable" | round(default=0) }} returns 0, however {{ "unavailable" | round }} or {{ "unavailable" | round(0) }} won’t work.

This fails for a self-evident reason; round isn’t provided a default value.
{{ "unavailable" | round }}

This fails because round still isn’t provided a default value.
{{ "unavailable" | round(0) }}

round can accept three arguments, the first is precision, the second is the rounding method, and the third is the default value. So round(0) only specifies precision (not the default value).

Example:

{{ "unavailable" | round(0, 'common', 0) }}

1 Like

If it’s never negative, if you’re happy with <1A = red and unknown = grey:

{{ 'red' if states('sensor.mysensor')[0] == '0' else 'grey' }}

That just looks at the first character of the sensor state, and selects red if it’s 0. Shorter version:

{{ ['grey','red'][states('sensor.mysensor')[0]=='0'] }}