Script help please - if is_state(state_attr( - Not working

I am trying to set a weather icon, I have created an hourly sensor that is working.

I need to pull the weather condition from the sensor for hour+1 +2 etc and then set the icon.

I have the following, the state IS set correctly but the icon is no, no error, but is not setting the correct icon, always falls through to the catch-all last ‘else’.

  - name: "weather_1h"
    state: "{{state_attr('sensor.weather_hourly', 'forecast')[0].condition}}"
    icon: >
      {% if is_state(state_attr('sensor.weather_hourly', 'forecast')[0].condition, 'clear-night') %}
      mdi:weather-night
      ...
      ...
      ...
      {% elif is_state(state_attr('sensor.weather_hourly', 'forecast')[0].condition, 'partlycloudy') %}
      mdi:weather-partly-cloudy
      {% else %}
      mdi:weather-cloudy-clock
      {% endif %}

Lose the ‘is_state’ in the ifs/elifs.
Try it with e.g. {% if is_state_attr(‘sensor.weather_hourly’, ‘forecast’)[0].condition, ‘clear-night’) %}.

I got it, such a muppet!

Use the value of the sensor i’m setting the icon for to make it easy and use ‘is_state’

{% if is_state(‘sensor.weather_1h’, ‘clear-night’) %}

What?! That won’t work.

is_state_attr(ENTITY_ID, ATTRIBUTE_NAME, STATE_TO_TEST)

You can’t randomly stick extra indexing in there.

OP:

icon: >
  {% set c = state_attr('sensor.weather_hourly', 'forecast')[0].condition %}
  {{ 'mdi:weather-' ~
     { 'clear-night': 'night',
       'partlycloudy': 'partly-cloudy' }
     .get(c, 'cloudy-clock') }}

Add extra mappings as you need.

This is what i have done:

  - name: "weather_1h"
    state: "{{state_attr('sensor.weather_hourly', 'forecast')[0].condition}}"
    icon: >
      {% if is_state('sensor.weather_1h', 'clear-night') %}
      mdi:weather-night
      {% elif is_state('sensor.weather_1h', 'cloudy') %}
      mdi:weather-cloudy
      {% elif is_state('sensor.weather_1h', 'fog') %}
      mdi:weather-fog
      {% elif is_state('sensor.weather_1h', 'hail') %}
      mdi:weather-hail
      {% elif is_state('sensor.weather_1h', 'lightning') %}
      mdi:weather-lightning
      {% elif is_state('sensor.weather_1h', 'lightning-rainy') %}
      mdi:weather-lightning-rainy
      {% elif is_state('sensor.weather_1h', 'pouring') %}
      mdi:weather-pouring
      {% elif is_state('sensor.weather_1h', 'rainy') %}
      mdi:weather-rainy
      {% elif is_state('sensor.weather_1h', 'snowy') %}
      mdi:weather-snowy
      {% elif is_state('sensor.weather_1h', 'snowy-rainy') %}
      mdi:weather-snowy-rainy
      {% elif is_state('sensor.weather_1h', 'sunny') %}
      mdi:weather-sunny
      {% elif is_state('sensor.weather_1h', 'windy') %}
      mdi:weather-windy
      {% elif is_state('sensor.weather_1h', 'windy-variant') %}
      mdi:weather-windy-variant
      {% elif is_state('sensor.weather_1h', 'exceptional') %}
      mdi:weather-lightning
      {% elif is_state('sensor.weather_1h', 'partlycloudy') %}
      mdi:weather-partly-cloudy
      {% else %}
      mdi:weather-cloudy-clock
      {% endif %}