Template error? or am I doing something wrong?

I’m trying to make a sensor for my plants but can’t understand why it’s giving me the wrong output. any ideas?

{{states('sensor.bonsaitrad_3_moisture')}}

output = 8

 {% if states('sensor.bonsaitrad_3_moisture') < '20' %}
 Dry
{% elif states('sensor.bonsaitrad_3_moisture') > '40' %}
 Wet
{% else %}
 Good
{% endif %} 

Output = Wet

In this case the output should be dry?

I found that i had to add a float filter to everything to get actual numbers. If it is interpreting everything as strings, 8 > 2 , 8 >4 therefore wet is the correct result.

states() always returns a string, so you need to use a float filter turn it into a number. You also need to remove the quotes around 20 and 40, right now they are strings.

Try this:

{% if states('sensor.bonsaitrad_3_moisture') | float < 20 %}
  Dry
{% elif states('sensor.bonsaitrad_3_moisture') | float > 40 %}
  Wet
{% else %}
  Good
{% endif %}
2 Likes