How to turn on/off all lights (and only lights) in an area?

Hey there. I’m trying to get a script to turn on or off all the lights in an area, but am struggling to get it working.

I can do it by using e.g. homeassistant.turn_off with area_id: living_room but the problem is that it turns everything off, including media players, etc.

If I try light.turn_off with area_id: living_room, entity_id: living_room, area_id: area.living_room or entity_id: living_room nothing happens at all.

Is my only option to just list each individual light in the room in the script separately? That’s not too onerous, it just feels like I’m missing an obvious answer.

light.turn_off with entity_id: group.living_room also doesn’t seem to work

This works as expected for me. Note that area_id should be an array if you’re doing more than one. So for example using Dev Tools:

service: light.turn_off
data: {}
target:
  area_id:
    - kitchen
    - br_3
1 Like

thanks for posting your working code. it made me go back and keep trying. i kept creating and fixing errors along the way, so i can’t be certain, but i think my problem was:

i was using homeassistant.turn_on/off for individual lights, which allowed me to ignore whether something was actually a light.name or switch.name type entity. i essentially forgot there was a difference and in my head light.turn_on worked for both types even though it didn’t.

Then when I was testing room level functionality I was using light.turn_on for a room that only contained switches…it took me a while to realize this because by the time I had tried another room with actual lights I had introduced more errors in my voice assistant → automation → script pipeline.

It’s all working now. My solution was just to have the script (both the one for individual lights and areas) call both light.turn_on and switch.turn_on using the same entity id, so that it gets handled either way and the other one just harmlessly errors.

again, thanks. having a confirmed “this works” is super useful to know to look elsewhere for the problem.

You also could use “old style” groups. You can put whatever you wish into the group (switches/lights).
I don’t (rarely) use “areas”, I find groups much more rich.

With “old style” groups, you get benefits including:

  1. Knowing the state of the whole group (all are on, all or off)
  2. You can create groups that contain other groups. This is really key. I have lights I want in “backyard”, but also in “pergola”, but also in “dogs” … all “pergola” lights are in “dogs” and all “dogs” lights are in “backyard”. Essentially, you can do whatever you wish with multiple overlapping groups and not be isolated to calling something in one “area”.

LIke this:

#
# Pergola
#
pergolalights:
    name: Pergola
    icon: 'mdi:home-variant'
    entities:
      - switch.switchlinc_relay_dual_band_1f_ad_f7
      - switch.switchlinc_relay_dual_band_1f_a8_f1
    all: true
# 
# Dogs
#
doglights:
    name: Dogs
    icon: mdi:dog
    entities:
      - group.pergolalights
      - switch.keypadlinc_on_off_1f_15_0a_main
      - light.in_linelinc_dimmer_4a_ad_d3
      - light.in_linelinc_dimmer_4a_ab_70
    all: true
# 
# Backyard
#
backyardlights:
    name: Backyard
    icon: 'mdi:tree'
    entities:
      - group.doglights
      - switch.keypadlinc_on_off_1f_14_f6_main
    all: true

Like if I was displaying status and I turn on:

Pergola – pergola state in “on”
Dogs – dogs AND pergola state is “on”
Backyard – backyard AND dogs AND pergola state is “on”

1 Like

I can definitely think of a few places where that would be useful. Thanks!