Template sensor dynamic icons - how to do it right?

Hi,
I’m trying to get my template sensors to show different icons depending on state. The template sensors are attributes of a thermostat auto-discovered over wifi. I’m having trouble with the details of the if statement. I’m trying to get at the attributes of the auto-discovered climate item (like the fan status below). The options I’ve tried did not work.

I’m using the template sensor identifier…which may not be appropriate. The result is the default eye icon.

sensor:
  - platform: template
    sensors:
      hvac_fan_action:
        friendly_name: "Fan Status"
        value_template: "{{ state_attr('climate.thermostat_xx_xx_xx', 'fan_action') }}"
        icon_template: >-
          {% if is_state(hvac_fan_action, 'on') %}
            mdi:fan
          {% else %}
            mdi:fan-off
          {% endif %}

So I tried using the value template statement. This always results in the default (else) icon, so it’s not evaluating the if statements correctly.

        icon_template: >-
          {% if is_state(state_attr('climate.thermostat_xx_xx_xx', 'fan_action'), 'on') %}
            mdi:fan
          {% else %}
            mdi:fan-off
          {% endif %}

I’ve tried ‘on’ without the single quotes. Still same result.

I also tried dot fields. I don’t think home assistant works like that.

        icon_template: >-
          {% if climate.thermostat_xx_xx_xx.fan_action == 'on') %}
            mdi:fan
          {% else %}
            mdi:fan-off
          {% endif %}

hi - wanted to see if I can bump this question. Thanks!

same issue here

Although this thread is a bit old and probably no longer needed:

template:
  - sensor:
      - unique_id: hvac_fan_action
        name: "Fan Status"
        state: "{{ state_attr('climate.thermostat_xx_xx_xx', 'fan_action') }}"
        icon: "{{ 'mdi:fan' if is_state_attr('climate.thermostat_xx_xx_xx', 'fan_action','on') else 'mdi:fan-off' }}"