Icon_template for MQTT sensor

MQTT sensors do not have an icon_template attribute.

They do support device classes though.

What is your sensor, there may be a class that fits?

These are UV sensor and rain sensors: rain per hour(mm/hour) and rain for 24hours(mm).
I want to change an icon depending from the sensor state.

 icon_template: >-
       {% if value | float(0) > 0 %}
           mdi:umbrella-outline
       {% else %}
           mdi:umbrella-closed-outline
       {% endif %}

Ok, neither of those have a suitable device class.

Don’t forget to vote for your own request.

1 Like

For following feature request has been closed in 2019 by a bot:

Well, this is where you put in feature requests, not in issues on github. Issues are for bugs.

template.binary_sensor.icon supports templates
mqtt.binary_sensor.icon doesn’t
that seems like a bug

Here is a working version (just copy pasting stuff from template_entity.py): Comparing home-assistant:dev...Cyberbeni:mqtt-template-icon · home-assistant/core · GitHub

Those are 2 separate integrations…

The bug is that it is not defined in 1 place that any entity that you define in yaml has an icon attribute that accepts templates.

That’s not how the development in HA works. Sorry. Every integration implements it’s own functionality. This is a feature request. You’re welcome to argue me all you want, but that is the simple truth.

Secondly, if you implement json_attributes_template on your mqtt entity, you can provide a template for all attributes. If you include icon, it’ll work just like an icon_template.