Icon change on sensor state change not working

Hi guys

I’ve setup a template that essentially monitors for electrical mains failure and would like the icon to update accordingly, I’ve copied a similar template from my config but the icon is not updating for some reason, I’m no coder so not sure what I’ve done wrong but perhaps someone could help me out? Thank you.

  - platform: template
    sensors:
      current_loadshedding_status:
        friendly_name: 'Current Loadshedding Status'
        value_template:  >-
          {% if is_state('sensor.grid_failure', '240') %}
            Power is off
          {% elif is_state('sensor.grid_failure', '1') %}
            Power is on
          {% else %}
            {{ states('sensor.current_loadshedding_status') }}
           icon_template: >-
            {% if is_state('sensor.grid_failure', '1') %}
              mdi:power-plug
            {% else %}
              mdi:power-plug-off
            {% endif %}
          {% endif %}

icon_template: is indented too far, move it to be in line with value_template. Also you mixed up the two templates. They should be separate. Like this:

  - platform: template
    sensors:
      current_loadshedding_status:
        friendly_name: 'Current Loadshedding Status'
        value_template:  >-
          {% if is_state('sensor.grid_failure', '240') %}
            Power is off
          {% elif is_state('sensor.grid_failure', '1') %}
            Power is on
          {% else %}
            {{ states('sensor.current_loadshedding_status') }}
          {% endif %}
        icon_template: >-
          {% if is_state('sensor.grid_failure', '1') %}
            mdi:power-plug
          {% else %}
            mdi:power-plug-off
          {% endif %}

thanks so much @tom_l its working now.

1 Like