I want to replace default icons for a door. But Home Assistant applies the status of the sensor to the icon. How can I make sure that a custom icon has also two states, that show in e.g. my entities card.
You could have voted for this eight months ago:
Basically: there is no method to do what you want other than choosing from a limited set of device classes.
Gosh… if I knew it eight months ago. I’m firing up the DeLorean!
Before you do…
You could use this: Mariusthvdb/custom-ui: Add templates and icon_color to Home Assistant UI
In customize.yaml with this:
binary_sensor.andreas_in_bed:
templates:
icon: if(state === 'off') return 'mdi:bed-empty'; if(state === 'on') return
'mdi:bed';
icon_color: if(state === 'on') return 'rgb(0,255,0)'; if(state === 'off') return
'rgb(255,0,0)';
I get this output:
This is a nice idea and a good workaround. But that would make a lot of template sensors for me!
It’s not template sensors.
It’s just templates to customize the graphics of an entity.
My entity comes from an ESP:
And the customize sets the templates part of the entity
that would be written like:
binary_sensor.andreas_in_bed:
templates:
icon: >
if (state === 'off') return 'mdi:bed-empty';
return 'mdi:bed';
icon_color: >
if (state === 'on') return 'rgb(0,255,0)';
return 'rgb(255,0,0)';
or my personal preference:
binary_sensor.andreas_in_bed:
templates:
icon: >
return state === 'off' ? 'mdi:bed-empty' : 'mdi:bed';
icon_color: >
return state === 'on') ? 'rgb(0,255,0)' : 'rgb(255,0,0)';
having said that, I am supposed to advise against the use of custom-ui (it still works perfectly as far as I tested recently) and should promote the use of card_mod for these modifications:
card_mod:
style: |
:host {
--card-mod-icon:
mdi:{{'bed' if is_state(config.entity,'on') else
'bed-empty'}};
--card-mod-icon-color: {{'rgb(0,255,0)' if is_state(config.entity,'on') else 'rgb(255,0,0)' }}
}
custom-ui was design for the use in states supporting cards a long time ago, and does not work in all modern cards… card_mod can modify more cards, though even for Tile card you have to pull several tricks out of your sleeve to get it all to behave as you’d like