{% 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 ...
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.
{% 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' }}