WTH Why doesn't ESPHome support icon templating?

It would be nice if ESPHome supported icon templating directly. For example if you create a sensor you may want a different icon based on the state of that sensor, etc.

I am aware of workarounds to set the the icon in a lambda or to create a duplicate template sensor in Home Assistant itself which then has the icon being templated. But it always just seems like a workaround, and means I’ve duplicated many sensors with a template just for the icons, why can’t ESPHome just support templating of icons?

If it turns out there is already a clean way to do this then I’d love to hear how.

Setting the icon in the lambda doesn’t work any more it seems. So this is still a WTH for me :(.

What are you trying to set the icon for? And what icons?

You can kind of set icons by setting the device_class in your yaml:

  - platform: gpio
    name: fogger ready
    device_class: power
    pin:
      number: 3
...

This will use the default icons for the device_class in the front end.

Some device classes:

You can further/also customize those icons in Home Assistant as patterns or domains:

Or per-entity in Lovelace:

both without making a new template sensor.