Icon template not working - yet another!

I am using the following to set the state and hopefully the corresponding icon for the garage_door sensor:

template:
      sensor:
      - name: "garage_door"
        state: >
          {% if is_state('binary_sensor.gduppin', 'off') and is_state('binary_sensor.gddownpin', 'on') %}
            Closed
          {% endif %}  
          {% if is_state('binary_sensor.gduppin', 'on') and is_state('binary_sensor.gddownpin', 'on') %}  
            Moving
          {% endif %}
          {% if is_state('binary_sensor.gduppin', 'on') and is_state('binary_sensor.gddownpin', 'off') %}  
            Open
          {% endif %}
        icon: >
          {% if is_state('sensor.garage_door', "Open") %}
            mdi:garage-open-variant
          {% endif %}
          {% if is_state('sensor.garage_door', "Moving") %}
            mdi:garage-alert-variant
          {% endif %}
          {% if is_state('sensor.garage_door', "Closed") %}
            mdi:garage-variant 
          {% endif %} 
          

The state template works fine, showing the proper value on the dashboard and updating as soon as the binary inputs change, but the icon displayed is always the garage-open-variant. It does not change regardless of the state. I tried changing the order of the icon template tests thinking it may just default to the first choice but that is not the case. I also tried with single quotes rather than double. That also had no affect.
Any suggestions greatly appreciated.

Is there any reason you are not using a Template Cover?

If there is and you want to keep using the sensor, you should structure your templates like this:

template:
  sensor:
    - name: "garage_door"
      state: >
        {% if is_state('binary_sensor.gduppin', 'off') and is_state('binary_sensor.gddownpin', 'on') %}
          Closed
        {% elif is_state('binary_sensor.gduppin', 'on') and is_state('binary_sensor.gddownpin', 'on') %}  
          Moving
        {% elif is_state('binary_sensor.gduppin', 'on') and is_state('binary_sensor.gddownpin', 'off') %}  
          Open
        {% endif %}
      icon: >
        {% if is_state('sensor.garage_door', "Open") %}
          mdi:garage-open-variant
        {% elif is_state('sensor.garage_door', "Moving") %}
          mdi:garage-alert-variant
        {% elif is_state('sensor.garage_door', "Closed") %}
          mdi:garage-variant 
        {% endif %} 
1 Like

Hi and thanks for the quick response.
I am only reporting the status of the door at this point, not controlling it. So I thought a template sensor would avoid the control icon that a cover template would create on the dashboard.

I made the changes you suggested but nothing has changed. The door state still reports just fine but the icon is always the same (mdi:garage-open-variant).

I discovered this kinda buggy thing too. If you’re using a template sensor in this new style (as encouraged in the docs here), it doesn’t work. The “old” way does work as I found out.

binary_sensor:
  - platform: template
    sensors:
      me_asleep2:
        value_template: >-
          {% if is_state('binary_sensor.door_bedroom_on_off', 'off') %}
            on
          {% else %}
            off
          {% endif %}
        icon_template: >-
          {% if is_state('binary_sensor.door_bedroom_on_off', 'off') %}
            mdi:sleep
          {% else %}
            mdi:human
          {% endif %}
        delay_on: 0:05:00
        delay_off: 0:05:00

# Templates
template:
  - binary_sensor:
      - name: "Me"
        unique_id: "binary_sensor.me_asleep"
        state: >-
          {% if is_state('binary_sensor.door_bedroom_on_off', 'off') %}
            on
          {% else %}
            off
          {% endif %}
        icon: >-
          {% if is_state('binary_sensor.door_bedroom_on_off', 'off') %}
            mdi:sleep
          {% else %}
            mdi:human
          {% endif %}
        delay_on: 0:05:00
        delay_off: 0:05:00

I think it’s a bug, I will report it as such.

Disregard last post. It should be working at the root level.