Change Icon in a template sensor

hi there,

i have created a sensor for a hue motion sensor to add the battery status:

sensor:
  - platform: template
    sensors:
      hue_motion_battery_aussen:
        friendly_name: "Hue Sensor Battery aussen"
        value_template: '{{state_attr("binary_sensor.bewegungssensor_aussen_motion", "battery_level")}}'
        unit_of_measurement: "%"

now i want to change the icon. can anyone point me where to put for example icon: mdi:home?

thanks a lot

1 Like

If you do this:

sensor:
  - platform: template
    sensors:
      hue_motion_battery_aussen:
        friendly_name: "Hue Sensor Battery aussen"
        value_template: '{{state_attr("binary_sensor.bewegungssensor_aussen_motion", "battery_level")}}'
        unit_of_measurement: "%"
        device_class: battery

The battery icon will change reflect the state of charge of the battery, like this:

Or use the icon_template in the legacy template sensor.
The code is the same as in the icon of the modern template.

1 Like

@tom_l that is quiet cool. thanks for this. definitely gonna keep that in mind. but not quite what i’m looking for. i want to change the battery icon of said sensor to a house icon (hence mdi:home).

@VDRainer thanks for the pointer. but i can’t figure out where to put icon_template in the above sensor. i’m quite new to HA and have been using it for only two month or so.

nevermind. should have tried more. i figured it out:

sensor:
  - platform: template
    sensors:
      hue_motion_battery_aussen:
        friendly_name: "Hue Sensor Battery aussen"
        value_template: '{{state_attr("binary_sensor.bewegungssensor_aussen_motion", "battery_level")}}'
        unit_of_measurement: "%"
        icon_template: mdi:home

does the trick.
thanks a lot for your help.

3 Likes

:slightly_smiling_face:
Static icons are easier with customizing or directly in the UI.

I prefer to keep them with the entity config if possible. That way if I delete the entity six months later I don’t forget to delete the customization and end up with orphan icons in customize (that do not generate errors).

3 Likes