Trying to create a template to show if any devices are on in an area

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 %}

Appreciate any help with this.

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.

1 Like

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.

Use the match filter with a regex pattern that specifies the entity domains you want.

{{ area_entities('Living Room')
  | select('match', '(light|switch|fan|cover)')
  | select('is_state', ['on', 'open'])
  | list }}
1 Like

That’s the ticket. Thnaks!

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:

styles:
  img_cell:
    - justify-content: start
    - position: absolute
    - width: 120px
    - height: 120px
    - left: 0
    - bottom: 0
    - margin: 0 0 -20px -20px
    - background: |
        {% if is_state('binary_sensor.lr_devices_all_off', 'on') %}
          rgb(211,211,211)
        {% else %}
          rgb(145,145,145)
        {% endif %}

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.

Is it the custom Button Card by RomRider?

If it is then its documentation indicates it supports Javascript templates (not Jinja2 templates like in the example you posted).

This example is from the card’s documentation:

  - background-color: >
     [[[
       if (states['input_number.test'].state == 0)
         return "green"; 
       return "red"; 
     ]]]

Just a guess but try something like this (I can’t confirm it works because I don’t use that custom card).

  - background: >
     [[[
       if (states['binary_sensor.lr_devices_all_off'].state == 'on')
         return 'rgb(211,211,211)';
       else return 'rgb(145,145,145)'; 
     ]]]
1 Like

Your guess works great. Thanks!

Edit: answered my own question.

You’re welcome!

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.

1 Like