Can you turn off/on all lights in an Area?

I don’t really get the point of the Areas but it would be great if you could just simply trigger things on all devices in an area, such as “turn of all lights in area “Bedroom””. Is that possible?

It looks like the answer is yes …

1 Like

Yes, as shown in the screenshot above using the Automation Editor and here’s how in YAML (fragment of an automation):

action:
- service: light.turn_off
  target:
    area_id: "{{ area_id('Kitchen') }}"
5 Likes

Not sure what that has to do with Home Assistant sending commands to smart devices… The OP’s question has been answered, the way that is achieved electrically comes down to what (and how many) smart devices they have installed, which is not the question here.

Welcome to the forum. I think you are confused. What has house wiring to do with switching lights off ?

:grinning_face_with_smiling_eyes:hardware vs software

Anybody figured out how to turn off everything in an area: this could be lights, switches, fans, media players? Sort of similar to the area switch that is shown in the overview card at the top … that just excludes media players actually

homeassistant.turn_off

1 Like

You could make use of the group option now in the Helpers, to make a group of switches, lights or whatever, that way you get one switch or light which is very easy to switch on or off.
If some switches control lights you can now easily change them into lights too, so they all can get placed under one option.

1 Like

I’m revisiting this topic for clarification:
I need to disable all the switches in the ‘lastrico_solare’ area except one.
Reading various topics I managed to write this code which seems to work:

    - service: switch.turn_off
      target:
        entity_id: >
          {{expand(states.switch) 
            | selectattr('entity_id', 'in', area_entities('lastrico_solare'))
            | selectattr('state', 'eq', 'on') 
            | rejectattr('entity_id', 'eq', 'switch.lastricosolare_interruttoregenerale')
            | map(attribute='entity_id')
            | list
          }}

I wondered how I was supposed to write it if I started out like this:

    - service: switch.turn_off
      target:
        entity_id: >
          {{expand(area_entities('lastrico_solare')) 
            | selectattr( **** only switch ***) <<=== How?
            | selectattr('state', 'eq', 'on') 
            | rejectattr('entity_id', 'eq', 'switch.lastricosolare_interruttoregenerale')
            | map(attribute='entity_id')
            | list
          }}

Thanks

use this instead

{{ area_entities('lastrico_solare') 
  | select('search', '^switch.') 
  | select('is_state', 'on') 
  | reject('eq', 'switch.lastricosolare_interruttoregenerale') 
  | list
}}

It won’t be throttled to 1 update per second, and it won’t traverse all switches. It just uses the area’s switches.

1 Like

Thanks for the reply.
Needless to say, it works perfectly.

P.S. What is the purpose of ^ before switch?
I looked for it in the jinja manual but couldn’t find it

search uses regex. The ^ symbol in regex means “startswith”. So it’s basically saying serach for a string that starts with switch.

:ok_hand: thanks for the quick reply

For a template sensor storing a list - e.g. names of all lights that are on, is there an upper limit to the length of the list that can be stored?

I believe it works as intended till the count of lights on reaches 15. Up to 14 lights are listed out by entity name, but when the 15th light comes on the sensor value becomes unknown. Is that expected behavior?

    lights_on_count:
      friendly_name: Lights On
      value_template: >
        {{expand(states.light) 
          | rejectattr('entity_id', 'search', 'screen')
          | selectattr('state', 'eq', 'on') 
          | map(attribute='entity_id')
          | list
          | count
        }}
    lights_on_names:
      friendly_name: Lights On Names
      value_template: >
        {{expand(states.light) 
          | rejectattr('entity_id', 'search', 'screen')
          | selectattr('state', 'eq', 'on') 
          | map(attribute='name')
          | list          
        }}

Put that list in an attribute and you won’t have a limit. Well you do, but the limit is huge and it’s not a character limit, it’s a byte limit. And it’s only a byte limit for recorder, so if you omit it from the database, you won’t hit any limits.

Thank You Petro. How do I create an attribute for a template sensor?

Also adding a | sort before the list in the above code works in the template checker (dev tools) but not in the sensor when I create it. No idea why.

you can put the sort after the list, but that won’t make any difference, there must be something wrong with the config, post the full thing

Thank you I figured it out. Not sure why the sort was not working earlier but I added it back in again and restarted and it did work. I was just reloading YAML config earlier. not sure why the restart would have been different but it works now.

Also figured out how to add templates - so the sensor.yaml file is much more readable now. THANK YOU!

All of these sensors and attributes is used for dashboards primarily and some for automations. For the dashboard piece - using custom button cards - there is no better way to get these template based counts and such other than creating sensors and using those sensor states in the js templates in the button card right? Referring to your post here - assuming that is still true in 2024, right?