Changing mdi icon depending on sensor state

Hi folks,

I’ve looked through some of the topics on this and I thought I had done this correctly, but Computer says no. So maybe someone can slap me over the head and correct me.

  - platform: template
    sensors:
      kitchen_door_2_status:
        friendly_name: "Kitchen Door 2"
        value_template: >-
          {% if is_state("sensor.aeon_labs_zw120_door_window_sensor_gen5_access_control_2", "23")%}
          Closed
          {% elif is_state("sensor.aeon_labs_zw120_door_window_sensor_gen5_access_control_2", "22")%}
          Open
          {% else %}
          Unknown
          {% endif %}
        entity_value_template: >-
          {% if is_state("sensor.aeon_labs_zw120_door_window_sensor_gen5_access_control_2", "23")%}
            mdi:door-closed
          {% elif is_state("sensor.aeon_labs_zw120_door_window_sensor_gen5_access_control_2", "22")%}
            mdi:door-open
          {% endif %}

entity_value_template? Where’d you get that from?

It’s icon_template:

  - platform: template
    sensors:
      kitchen_door_2_status:
        friendly_name: "Kitchen Door 2"
        value_template: >-
          {% if is_state("sensor.aeon_labs_zw120_door_window_sensor_gen5_access_control_2", "23")%}
            Closed
          {% elif is_state("sensor.aeon_labs_zw120_door_window_sensor_gen5_access_control_2", "22")%}
            Open
          {% else %}
            Unknown
          {% endif %}
        icon_template: >-
          {% if is_state("sensor.aeon_labs_zw120_door_window_sensor_gen5_access_control_2", "23")%}
            mdi:door-closed
          {% elif is_state("sensor.aeon_labs_zw120_door_window_sensor_gen5_access_control_2", "22")%}
            mdi:door-open
          {% endif %}
1 Like

God damn it! That’s what I get for doing this while still half asleep. I was looking at that for half a bloody hour trying to work out what I’d done wrong.

Thanks buddy.

Ps. I have no idea where I got entity_value_template from. I mean I’m sure I did at the time, but I can’t find it now.

I want to do this too. Where is this code going? Is it in your configuration.yaml, customize.yaml, or is it going on like a template card or something?

It goes in your configuration.yaml though I’m not sure if it’s still valid, as I don’t use it any longer. At the time it worked quite well though.