I have a card with an icon_colour
that I want to change based on the entity sensor.myphone_steps
value… e.g. red/yellow/green if <1000, 1001-9999, >10,000.
I know {%if ... %}
statements are a thing, but I’ve not been successful googling how to (simply?!) reference an entity’s value so I can set different colours based on the number of steps.
Would love some pointers!
- type: entity
entity: sensor.myphone_steps
name: Walked
icon: mdi:shoe-print
icon_color: cyan
You need to use a card type that supports templating. The Entity card and almost all other core cards, do not support templating.
One popular card set that does is Mushroom Cards, but there are other options.
there must be a zillion threads on this, please just search the community.
card_mod is a good start to modify any icon_color no matter the card’s native ability to support templates, and yes, even some custom cards that provide the functionality
@Mariusthvdb If there are “a zillion threads on this,” then my 15+ minutes of googling didn’t turn any up, which is why I felt I had to ask. Sorry. I’ll keep looking, but if you have any direct links to posts that answer my question then that would be appreciated.
@Didgeridrew Thanks – yes, I’m using Mushroom cards. I just don’t know how to get an entity’s value. I’ll look into templating.
Just search icon_color and state…
type: custom:mushroom-template-card
primary: Walked
secondary: "{{ states(entity)}}"
entity: sensor.myphone_steps
icon: mdi:shoe-print
icon_color: |
{% if not has_value(entity) %}
grey
{% else %}
{% set val = states(entity)|int(0) %}
{% if val <= 1000 %}
red
{% elif 1000 < val <= 9999 %}
yellow
{% elif val > 9999 %}
green
{% endif %}
{% endif %}
1 Like
@Didgeridrew THANK YOU! That’s what I was looking for (just tried it in a manual widget) and still can’t find elsewhere. I didn’t know about using states(entity)
or the ability to specify entity:
on its own line like that – thank you for providing a working example
Thing is, copying this doesn’t really help you, as you still don’t understand what you’re doing.
Finding out about this stuff yourself and learning from it will be very useful in the end.
Now you got a working example for this particular card, but what about all other situations .
Believe me , enter the 2 words in the community search box and all of those threads will open up.
Key is the concept ‘state’ here .
You’re opening config with type: entity
still doesn’t work …
This should get you going 🔹 Card-mod - Add css styles to any lovelace card
And see these for more;