IF state with a AND IF

Hi All,

Almost all my migration from Domoticz to HA has been done… But there is one script I cant get it running:

          {% if states.sensor.br_temperature_2.state > "16" and states.sensor.entryway_thermostat_target.state > 16 %} 10
          {% elif states.sensor.br_temperature_2.state < "10" and and states.sensor.entryway_thermostat_target.state < 10 %} 16
          {% endif %}

I want create a IF state but need to check 2 devices in 1 action.

Can someone tell me how to do this?

Cheers,
Peter

I revised your template as follows:

          {% set temp = states('sensor.br_temperature_2') | float %}
          {% set therm = states('sensor.entryway_thermostat_target') | float %}
          {% if temp > 16 and therm > 16 %} 10
          {% elif temp < 10 and therm < 10} 16
          {% else %} 12
          {% endif %}

First thing you may notice is that I used the float filter to ensure all entity states are numeric. Second, when testing the variables, I compared them to numeric constants, like 16, as opposed to strings, like "16". Lastly, I added a final {% else %} to handle the case where neither of the first two tests is satisfied (I chose a random value of 12). This ensures the template always returns something (as opposed to nothing). If you are certain that returning nothing is acceptable for this template, then you can remove that line.

4 Likes

thanks for the great explanation !!! This help me a lot to understand the way you create the template!

And just to add a bit more information…

you can still do it the way you had it written but with a few fixes.

As @123 said you need to add the “float” filter to force it to a floating point number (or you could use “int” if you only wanted to match integers).

Next you need to remove the second “and” in your “elif” statement.

And it would be best practice to add the “else” also as 123 said above to catch other conditions tho it isn’t strictly required. However, you will get an error in your log if the template fails to render due to an uncaught condition. So it’s better to have it than not.

And, lastly, it would be best to use the states() method to extract the state of the entity rather than the states.xxx.state method because if those values don’t exist you will also get errors in the log.

so the end result should look like this using your original syntax:

{% if states.sensor.br_temperature_2.state | float > 16 and states.sensor.entryway_thermostat_target.state | float > 16 %} 10
{% elif states.sensor.br_temperature_2.state | float < 10 and states.sensor.entryway_thermostat_target.state | float < 10 %} 16
{% else %} 12
{% endif %}

or like this using the states() method:

{% if states('sensor.br_temperature_2') | float > 16 and states('sensor.entryway_thermostat_target') | float > 16 %} 10
{% elif states('sensor.br_temperature_2') | float < 10 and states('sensor.entryway_thermostat_target') | float < 10 %} 16
{% else %} 12
{% endif %}
3 Likes

Thank you for the info! Clear and understandable. So the state() gives a error when it not exists? So that’s a better solution. I already find out I made some little typo mistakes and didn’t find out why a automation is not working haha :slight_smile:

No, sorry, it’s the other way around.

using states() prevents errors if the entity doesn’t exist.