Icon doesnt change - Mushroom Template card

Hi, following problem. i try to get this code running. why doesnt the icon update even after hours?

{% if states('weather.forecast_home') ,'clear-night' %}
mdi:weather-night
{% elif states('weather.forecast_home') ,'cloudy' %}
mdi:weather-cloudy
{% elif states('weather.forecast_home') ,'fog' %}
mdi:weather-fog
{% elif states('weather.forecast_home') ,'hail' %}
mdi:weather-hail
{% elif states('weather.forecast_home') ,'lightning' %}
mdi:weather-lightning
{% elif states('weather.forecast_home') ,'lightning-rainy' %}
mdi:weather-lightning-rainy
{% elif states('weather.forecast_home') ,'partlycloudy' %}
mdi:weather-partly-cloudy
{% elif states('weather.forecast_home') ,'pouring' %}
mdi:weather-pouring
{% elif states('weather.forecast_home') ,'rainy' %}
mdi:weather-rainy
{% elif states('weather.forecast_home') ,'snowy' %}
mdi:weather-snowy
{% elif states('weather.forecast_home') ,'snowy-rainy' %}
mdi:weather-snowy-heavy
{% elif states('weather.forecast_home') , 'sunny' %}
mdi:weather-sunny
{% elif states('weather.forecast_home') , 'windy' %}
mdi:weather-windy
{% elif states('weather.forecast_home') , 'windy-variant' %}
mdi:weather-windy-variant
{%endif%}

kindly asking for help. i have the similar code for different entity working fine

Suggest to check your templates in Dev tools → Templates.
Your code is wrong.
Try this

states('weather.forecast_home') == 'hail'
1 Like

As Ildar said, your syntax is incorrect, but it also isnt complete.

you should carefully place your brackets:

{% if states('weather.forecast_home') ,'clear-night' %}

should be

{% if is_state('weather.forecast_home' ,'clear-night') %}

etc for all states you need to evaluate.
You dont have an ‘endif’ which makes any template checker choke…

so, end the complete section with

{% endif %}

next up would be making this template better looking by setting a variable, and using that variable inside the ifs:

{% set weather = states('weather.forecast_home') %}
{% if is_state(weather, 'clear_night') %}
{% elif is_state(weather,'cloudy') %}
etc
{% endif %}

next, I’d suggest making a map of those conditions so you can do

{% set weather = states('weather.forecast_home') %}
{% set icons = {'clear-night':'night',
                'cloudy':'cloudy',
                'fog':'fog',
                'hail':'hail',
                'lightning':'lightning',
                'lightning-rainy':'lightning-rainy',
                'partlycloudy':'partly-cloudy',
                'pouring':'pouring',
                'rainy':'rainy',
                'snowy':'snowy',
                'snowy-rainy':'snowy-heavy',
                'sunny':'sunny' } %}
mdi:weather-{{ icons.get(weather) }}

finalizing on that, as there are only 3 states having another string than the mdi icons offer you can do:

{% set weather = states('weather.buienradar') %}
{% set icons = {'clear-night':'night',
                'partlycloudy':'partly-cloudy',
                'snowy-rainy':'snowy-heavy'} %}
mdi:weather-{{icons.get(weather) if weather in icons else weather }}

meaning: if the state of the weather component is in those 3 , then use the mapper, else simply map the state to the icon as is

3 Likes

btw, suggest to avoid using “if()” without “else”.

so true, always ‘guard’ your templates.

I did wonder if I should make an extra option for the ‘unknown’ in my mapper above.
it could be done, easily, but it would get the mdi_weather prefix, and you have to like that.
Otherwise it can be guarded by an extra if/else.

but probably no problem, until your weather component goes crazy :wink: