Only turn off lights in a group that are on

Is it possible to make a script or similar to check if a light in a custom group is on and then turn it off?

I have a lot of lights and when I turn everything off for the night or when I go out it takes a long time. So I would like to try to only turn off lights that are on:)

2 Likes
service: light.turn_off
data_template:
  entity_id: >
    {{ states.light
         |selectattr('entity_id','in',state_attr('group.MY_LIGHTS','entity_id'))
         |selectattr('state','eq','on')|map(attribute='entity_id')|join(',') }}

Hi

Thanks for the template, but when I try to add it I get the following error:
Invalid config for [automation]: [to] is an invalid option for [automation]. Check: automation->trigger->0->to.

Any idea:)?

Well, you didn’t say how/where you wanted to use this. Assuming (now) you want to use this in an automation, then it goes in the action part, not the trigger.

ahh the problem was all mine, I had some wrong settings in the trigger section. Sorry about that!

Working great now, thanks!

1 Like