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?
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.
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).