Sensor how to change icon on state change?

  - sensors:
      door_lock:
        friendly_name: "Nuki_test"
        value_template: >-
          {% if is_state("sensor.Nuki_motor_status", "locked") %}
            Locked
          {% elif is_state("sensor.Nuki_motor_status", "unlocked") %}
            Unlocked
          {% else %}
            Jammed
          {% endif %}
        icon_template: >-
          {% if is_state("sensor.Nuki_motor_status", "locked") %}
            mdi: mdiLock
          {% elif is_state("sensor.Nuki_motor_status", "unlocked") %}
            mdi: mdiLockOpenAlert
          {% else %}
            mdi: lock-alert-outline
          {% endif %}

what I want is
unlocked = mdi: mdiLockOpenAlert
locked = mdi: mdiLock
elser = mdi: mdilock

Wrong syntax. Compare here: Material Design Icons - Icon Library - Pictogrammers

1 Like

Sorry please ignore

you mean:

- platform: template
  sensors:
    voordeur:
      friendly_name: "Nuki_test"
      value_template: >-
        {% if is_state("sensor.Nuki_motor_status", "on") %}
          Open
        {% elif is_state("sensor.Nuki_motor_status", "off") %}
          Locked
        {% else %}
          Closed
        {% endif %}
      icon_template: >-
        {% if is_state("sensor.Nuki_motor_status", "on") %}
          mdi:lock-alert-outline
        {% elif is_state("sensor.Nuki_motor_status", "off") %}
          mdi:lock
        {% else %}
          mdi:lock
        {% endif %}

I put “on” and ''off" in the state, so be sure of your device’s state, so for me, the door sensor gives open and close, but the states are “on” and “off”.

and for the icon like @pedolsky mentioned go the Material Design Icons - Icon Library - Pictogrammers and search for the icon when you find it press on it put home assistant and copy the icon name

so for this icon it is mdi:lock and the other icon is mdi:lock-alert-outline

1 Like

Yes it was the wrong mid: names for the icons

Thanks everyone.

1 Like