Template: is_state() matching multiple values

I have the following template:

{% if is_state("weather.home", "cloudy" or "fog" or "partlycloudy" ) -%} cloudy {% elif  is_state("weather.home", "hail" or "pouring" or "rainy" or "snowy" or "snowy-rainy" )-%} rain {% elif  is_state("weather.home", "lightning" or "lightning-rainy") %} lightning {% else %} sun {% endif %}

The expected outcome, if weather.home is “partlycloudy”, would be for the output to be “cloudy”, but instead I get sun, because only the first item of the lists is matched (i.e. a state of “cloudy” correctly gets “couldy” as the output.

Is there a better way to template this, or do I have to call is_state() multiple times like this:

{% if is_state("weather.home", "cloudy") or is_state("weather.home", "fog") or is_state("weather.home", "partlycloudy" ) -%} cloudy {% elif  is_state("weather.home", "hail") or ...
{% if states("weather.home") in ["cloudy", "fog", "partlycloudy"] %} 
  cloudy 
{% elif states("weather.home") in ["hail", "pouring", "rainy", "snowy", "snowy-rainy"] %}
  rain 
{% elif states("weather.home") in ["lightning", "lightning-rainy"] %}
  lightning
{% else %}
  sun
{% endif %}
6 Likes

Thank you! I tried the in [] syntax but with the is_state() function and was getting errors. This is exactly what i was trying to do.

1 Like

That’s because that function needs two tests (and only two tests) in the function and returns a boolean if the test conditions match.

using the state() function returns the actual string state so you can compare that string to a list of other strings for a match which will return a boolean if the string is in the list. Which is what tom’s template does.

Just for fun, here’s another way:

{% set x = {"cloudy":0, "fog":0, "partlycloudy":0, "hail":1, "pouring":1, "rainy":1, "snowy":1, "snowy-rainy":1, "lightning":2, "lightning-rainy":2} %}
{% set y = ['cloudy', 'rain', 'lightning'] %}
{% set z = states('weather.home')  %}
{{ y[x[z]] if x[z] is defined else 'sun' }}
1 Like