Automation to turn off switched on lights

Hi,

I’m trying to do an automation to execute when going out of my home to turn off turned on lights.

I want to searh for turned on lights
If there are turned on lights, notify and turn off lights.

For doing that I have defined an automation variable with turned on lights.

variables:
  turned_on_lights: >
    {{ states.light 
      | selectattr('state', 'eq', 'on') 
      | selectattr('entity_id',  'search', '^light.group_') | list }}
  light_names: >
    {% set names = turned_on_lights | map(attribute='friendly_name') | list %}
    {{ names }}

My problem is getting names of turned on lights to use it on the notify action. I’m trying to use the turned_on_lights variable to avoid repeting code for getting names but I have try several options but didn’t work.

With regards,

So…question. Why not simply send off to all the possible lights? They won’t turn off twice? Add them to a light group and send off to it.

…Unless you’re specially using this to understand how templating works?

Hi Juan,

What he said, mostly. Turn off all lights, they will all turn off. Use all as a thing in the entity field. Don’t need to enumerate them. If there are one or 2 you want to leave on, just turn those back on…

2 Likes

Hi,

Yes, I want to learn how to use templating, but I don’t want to turn off all, I just want to take in consideration light groups.
For example I have shelly devices which are configured as dettached switches because I don’t want to turn electricity off…

With regards,

I don’t understand why my code is working on template editor, but not on automation execution.

Here is my automation editor

Error

Then configure your detached channels as switches and all lights as lights, then simply turn off all lights with light.turn_off
This will only turn off lights and leave all switches on.

Can you please help me doing it in the way that I’m exposing? I prefer by this way to learn and because it’s more customizable.
Thanks!