Templating IF statement with multiple variables

I am still very new to templating and have this working, but wondering if it’s possible to use multiple variables in a single if statement? If so how would I format that? If ender 3 or cr10 states are on then color should be green. What I have below works, but trying to save some typing. :grinning:

{% set ender3 = states('switch.193_1') %}
{% set cr10 = states('switch.193_3') %}
{% if ender3 == 'off' %}
  red
{% elif ender3 == 'on' %}
  green
{% elif cr10 == 'off' %}
  red
{% elif cr10 == 'on' %}
  green
{% endif %}

Under what circumstances will your if-elif chain ever get to the second elif that tests the state of cr3?

I lied it’s not working as I wanted, both ender3 and cr10 have to be on for it to be green. How would I go about this?

Do both have to be off for it to be red?

What color should be reported if one is on and the other is off?

Good point, I didn’t think about the off state…ughhh…

If either of them are on I would want it to be green, but both would need to be off for it to be red.

So which is it? Both or either must be on for green?

If either one is on, then green.

{% if states('switch.193_1') == 'off' and states('switch.193_3') == 'off' %}
  red
{% else %}
  green
{% endif %}

Or simply use an immediate if statement.

{{ iif(states('switch.193_1') == 'off' and states('switch.193_3') == 'off', 'red', 'green') }}

@123 Thank you!

1 Like