I’m trying to implement a GUI representation of a sensor that has 3 states - open, closed and tampered.
I like the way binary_sensor changes icon and colour depending on its state and want to have the same functionality.
The trouble is I don’t know how to change colour.
Here is my config from sensor: section
- platform: template
sensors:
contact_safe_gui:
friendly_name: safe
value_template: "{{ states('sensor.contact_safe') }}"
icon_template: >
{% set state = states('sensor.contact_safe') %}
{% if state == states('sensor.contact_always_open') %}
mdi:door-open
{% elif state == states('sensor.contact_always_closed') %}
mdi:door-closed
{% elif state == states('sensor.contact_always_tampered') %}
mdi:image-broken
{% else %}
mdi:cloud-question
{% endif %}
You can’t do it in the sensor definition, unfortunately. You can do it in customize.yaml, but you have to install/enable custom_ui. Then you should be able to do something like this:
sensor.zone_001:
friendly_name: Front Door
templates:
icon_color: >
return 'rgb(255, 0, 0)';
This also works:
return 'red'
icon_color was broken for a few Hass versions, but the latest custom_ui release fixed it.
Thanks guys, but I want to keep it simple and as standard as possible.
If it involves custom_ui etc, I’ll live without colour OR find another solution
Sad they pushed us to Lovelace without giving such a basic thing…
Thousands of people missing years of the learning curve:
Install HACS, Uninstall HACS, Install Custon_ui, Uninstall Custon_ui, Install Custon Cards, Uninstall.
Just because a sensor model doesn’t behave like the binary sensor (blue-yellow) and doesn’t see the simple attribute as it should.
I don’t think they put in half a dozen code just to keep the community going.