ianadd
(ian)
December 4, 2022, 1:22am
1
Is there a way to turn all lights off except one.
Something like…
action:
light.turn_off
target:
entity_id: all
exclude: light.night_light
I realise I could make a group and list every other light in the house, yawn…, but would need maintenance if a house light changed. Is there a way to generate a group list automatically ?
tom_l
December 4, 2022, 1:44am
2
You can write a script to do it,
script:
all_lights_off:
description: Turn all lights off except an exclude list
mode: single
fields:
exclude_entity_ids:
description: entity_ids to exclude
sequence:
- service: light.turn_off
target:
entity_id: >
{{
states.light
| selectattr('state', 'eq', 'on')
| rejectattr('entity_id', 'in', exclude_entity_ids)
| map(attribute='entity_id')
| join(', ')
}}
Use it like this:
- service: script.all_lights_off
data:
exclude_entity_ids:
- light.night_light
5 Likes
ianadd
(ian)
December 4, 2022, 1:50am
3
Perfect, it is also to deal with a fan which I control using pwm and turns up as a light entity.
Maybe there is a way to turn it into a fan.xxx ??
I wonder if something has changed. I used to have another solution (very similar) and now I’ve tried this one, but Home Assistant turns off all the light when the script is called. I’ve made my script and automation like this:
Script - slightly modified:
alias: lights_off_except
sequence:
- service: light.turn_off
target:
entity_id: |
{{
states.light
| selectattr('state', 'eq', 'on')
| rejectattr('entity_id', 'in', exclude_lights)
| map(attribute='entity_id')
| join(', ')
}}
mode: single
icon: mdi:home-lightbulb
Automation for when the alarm turns on - turn off all lights except outside light:
alias: Alarm fully armed - turn off light
description: ""
trigger:
- platform: state
entity_id: alarm_control_panel.section_1
to: armed_away
condition: []
action:
- service: script.lights_off_except
data:
exclude_lights:
- light.udendors_lys_1
- light.udendors_lys_2
- light.udendors_lys_3
- light.atriumgard_lys
- light.udendors_stikkontakter
- light.udendors_halvmur_indgang
- light.udendors_terrasse
mode: single
ianadd
(ian)
January 19, 2023, 11:41pm
5
Note the difference between yours and this, which works…
script:
all_lights_off:
description: Turn all lights off except an exclude list
mode: single
fields:
exclude_entity_ids:
description: entity_ids to exclude
sequence:
- service: light.turn_off
target:
entity_id: >
{{
states.light
| selectattr('state', 'eq', 'on')
| rejectattr('entity_id', 'in', exclude_entity_ids)
| map(attribute='entity_id')
| join(', ')
}}
1 Like
ianadd:
>
{{
states.light
| selectattr('state', 'eq', 'on')
| rejectattr('entity_id', 'in', exclude_entity_ids)
| map(attribute='entity_id')
| join(', ')
}}
I’ve tried to copy this one to one and it still doesn’t work. When I put in entity_id: >
and save, it gets transformed into entity_id: |
next time I load the script. I guess I’ll revert back to listing all the lights I want to turn off. Weird, that this suddenly “broke” as it’s worked for months and suddenly stopped working. I’m on the latest version of Home Assistant.
ianadd
(ian)
February 10, 2023, 8:24pm
7
The vertical bar conversion is fine.
Bernd2378
(Bernd S.)
April 14, 2023, 11:50am
8
sorry to say but this script does not work for me.
Here is my code:
all_lights_off:
description: Turn all lights off except an exclude list
mode: single
fields:
exclude_entity_ids:
description: entity_ids to exclude
sequence:
- service: light.turn_off
target:
entity_id: >
{{
states.light
| selectattr('state', 'eq', 'on')
| rejectattr('entity_id', 'in', exclude_entity_ids)
| map(attribute='entity_id')
| join(', ')
}}
i trigger it via node red and the following payload:
{
"exclude_entity_ids": [
"light.shelly_maindoor",
"light.light_garage_inside",
"light.bedroom",
"light.huelight_bedroom-playbar"
]
}
in 80% of cases this works fine. But sometimes the excluded lights are also turned off, see my log:
I had to disable it now, WAF decreases more and more…
This is how it works, change data target to data_template.
alias: Routine - Bedtime Lights
description: Turn all lights off except an exclude list
mode: single
fields:
exclude_entity_ids:
description: entity_ids to exclude
sequence:
- service: light.turn_off
data_template:
entity_id: >
{{
states.light
| selectattr('state', 'eq', 'on')
| rejectattr('entity_id', 'in', exclude_entity_ids)
| map(attribute='entity_id')
| join(', ')
}}
bulbanos
(Viktor Petrov)
September 2, 2024, 2:17pm
10
This will turn off the group of lights the light belongs to. So you will end up in the dark no matter what.
I have found no solution for that.
Rofo
(Ro)
September 9, 2024, 12:50pm
11
Isn’t that link also the solution ?
It shows how to send the light turn off command to a list of entities, and exclude specific ones, which is exactly what you want to do ?
bulbanos
(Viktor Petrov)
September 9, 2024, 12:59pm
12
I want to switch off all lights in the house except for the driveway lights. But
states.light
| selectattr(‘state’, ‘eq’, ‘on’)
will return the group light.outdoor_lights too… And driveway lights are part of outdoor_lights group.
So it will turn off that group and thus my driveway lights too.
I do not want to list all groups manually because that can change in the future. I will add groups and it is a pain to find all automations where I have to list them once more.
Rofo
(Ro)
September 9, 2024, 1:04pm
13
Ok, so the method of excluding the entities is in this bit of code:-
If you don’t want to manually update this list when you add more devices that you want to exclude, then you could do it with a naming convention.
Say every device you want to exclude has the word ‘outdoor’ in its friendly name somewhere, then you could change the above to:-
rejectattr('name', 'search',
'outdoor')
And it will reject all entities with that in their name.
Or just stick with the entity name:-
rejectattr('entity_id', 'search',
'outdoor')