Add ICON to Template Sensor

I’d like to add an icon to a sensor. The sensor is configured in a templates.yaml file. I’ve tried using ‘icon_template:’ without achieving the desired result. No icon appears on the entity. My templates.yaml file follows. The sensor I wish to add the icon to in near the end of the file. The name of the sensor is “Rain in next 48 hours”.

###################################################
# "Outdoor Temperature Feels Like" Configuration  #
###################################################
- sensor:
    - name: "Feels Like"
      device_class: temperature
      unit_of_measurement: "°C"
      state: >
        {% if not is_state('sensor.stratford_humidex', 'unknown') %}
          {{ states('sensor.stratford_humidex') }}
        {% elif not is_state('sensor.stratford_wind_chill', 'unknown') %}
          {{ states('sensor.stratford_wind_chill') }}
        {% else %}
          {{ states('sensor.stratford_temperature') | round(0) }}
        {% endif %}

    #####################################
    # "Rain in 48 Hours" Configuration  #
    #####################################
    - name: "Rain in next 48 hours"
      state: >
        value_template: >-
          {% set p0 = state_attr('weather.stratford', 'forecast')[0].condition == "rainy" %}
          {% set p1 = state_attr('weather.stratford', 'forecast')[1].condition == "rainy" %}
          {{ 'Yes' if p0 or p1 else 'No' }}
        icon_template: {{'mdi:weather-rainy'}}

You have defined a template declaration {{ ... }} but not enclosed it in quotes. This would solve that issue (but see below):

        icon_template: "{{ 'mdi:weather-rainy' }}"

…but is pointless for a static value:

EDIT: just noticed very weird things going on here. This is ChatGPT generated, isn’t it?

You’ve mixed legacy and modern format here (state plus value_template :crazy_face:, icon vs icon_template). Try this:

    - name: "Rain in next 48 hours"
      state: >
        {% set p0 = state_attr('weather.stratford', 'forecast')[0].condition == "rainy" %}
        {% set p1 = state_attr('weather.stratford', 'forecast')[1].condition == "rainy" %}
        {{ 'Yes' if p0 or p1 else 'No' }}
      icon: mdi:weather-rainy

I’d suggest this should be a binary sensor:

- binary_sensor:
    - name: "Rain in next 48 hours"
      state: "{{ 'rainy' in (state_attr('weather.stratford','forecast')|map(attribute='condition')|list)[:2] }}"
      icon: mdi:weather-rainy

also in the new modern way you can add a lot more, like label etc here’s one of mine

    - name: "Security State"
      unique_id: 84c316cb-2b49-4d12-883c-xxxxxxxxxx
      state: >
        {% if is_state('cover.roller_door', 'open') or is_state('binary_sensor.garage_door', 'on') or is_state('binary_sensor.front_door', 'on') or is_state('binary_sensor.back_door', 'on') %}
        door_open
        {% elif is_state('alarm_control_panel.house_alarm', 'armed_away') and is_state('alarm_control_panel.garage_alarm', 'armed_away') %}
        fully_armed
        {% elif is_state('alarm_control_panel.house_alarm', 'armed_away') or is_state('alarm_control_panel.garage_alarm', 'armed_away') %}
          {% if is_state('alarm_control_panel.garage_alarm', 'armed_away') %}
          garage_only
          {% else %}
          house_only
          {% endif %}
        {% else %}
        disarmed
        {% endif %}
      icon: >
        {% if is_state('sensor.security_state', 'fully_armed') %}
        mdi:shield-lock
        {% elif is_state('sensor.security_state', 'garage_only') or is_state('sensor.security_state', 'house_only') %}
        mdi:shield-lock-outline
        {% elif is_state('sensor.security_state', 'door_open') %}
          {% if is_state('alarm_control_panel.house_alarm', 'armed_away') or is_state('alarm_control_panel.garage_alarm', 'armed_away') %}
          mdi:shield-lock-outline
          {% else %}
          mdi:shield-off
          {% endif %}
        {% else %}
        mdi:shield-off
        {% endif %}
      attributes:
        navigation: "security"
        label: >
          {% if is_state('sensor.security_state', 'door_open') %}
          Door Open
          {% elif is_state('sensor.security_state', 'fully_armed') %}
          Fully Armed
          {% elif is_state('sensor.security_state', 'garage_only') %}
          Garage Only
          {% elif is_state('sensor.security_state', 'house_only') %}
          House Only
          {% else %}
          Disarmed
          {% endif %}
1 Like

Not that I’m aware of. I obtained the information from a Home Assistant Community post ( Smart Sprinkler Automation (skip watering if there is rain in next 48 hours - 2 days) )

That works!

Now that I think about it, that would be a better approach. Thank you.

EDIT: Using a binary sensor is a much better approach. Thanks.

Thanks, Andy. I’ve lot’s to think about now!

Ah, I see. That’s quite an old post, and the preferred template sensor format has changed since then. You were trying to shoehorn old format configuration into a new format sensor.

That sort of mix-up one of the “tells” for ChatGPT, as its training corpus is quite old and it has no actual intelligence on the two different approaches. My apologies for suggesting your own intelligence might be artificial :slight_smile:.

Thank you. I’m always getting stumped by format changes. I’ll have to start paying attention to post dates when researching.

1 Like

yeah, and to make it worse, somethings like template covers, still use the old way.

you could at least simplify that label template to

{{states('sensor.security_state')|replace('_',' ')|capitalize}}

and possibly use the this.state since you’re self-referencing the main state

nice thanks, i’ll give it a try, still fairly new to HA.