I’m pretty new to Home Assistant and have just started to wade into building a dashboard. I am using room cards and I would like to change the card color if all relevant entities (lights, fans, covers, media players) in said room are off or closed.
I’ve been Googling and trying different things with no luck. I’m assuming I need (or it’s best) to create a template:
I’ve tried the following, but it always returns “not_all_off”:
- sensor:
- name: Living Room All Off
unique_id: living_room_all_off
state: >
{% if area_entities('Living Room') | map('states') | select("in",['on','open']) | list | length and
is_state('light.lights_br', 'on') %}
not_all_off
{% else %}
all_off
{% endif %}
Please describe what you expect to happen versus what is currently happening.
The template you have shown is not limited to “lights, fans, covers, media players”, it will very likely include many other entities that can have a state of “on” or “open”… As a test, you can paste the following into the Template editor tool to see what’s causing your template to return “not_all_off”:
{{ area_entities('Living Room') | select('is_state', ['on','open']) | list }}
FWIW, you should consider switching to a binary sensor. If you continue using templates, you will likely find that having a bunch of sensors with non-standard binary state values can become very annoying to work with.
The template editor is very helpful. Thanks for that. Indeed, as you said, I am getting many other entities that have an “on” state that I wasn’t considering.
How do I go about limiting the results to the entities that are relevant to me?
Point taken about the binary sensor. Thanks for the info. It’s very helpful for a newbie like me.
Of course, now I’m having an issue implementing that in the final step. I have a custom button card and I want to set the background color based on this state. Here is what I have that isn’t working:
This results in no color at all. One things that confuses me trying to learn this is that some code works in one place but not another. I assume it’s the difference between jinja, template, js, etc and that some can be used in some situations, but not in others. And being new, I can’t tell one from the other.
Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which indicates to others that the topic has been solved. This helps other users find answers to similar questions.