Group of Lights turn off if none have changed on/off/brightness within x time

Have a multi use building with guests coming and going at all hours. We have approximately 88 Lutron Ra2 Dimmers. There’s scenarios where a guest might turn a light on or off at odd hours in the night. I know the description of what I’m looking for will call for motion sensors to turn lights off, however at this point there’s no money left in the budget

Is there a way to use some sort of template so that if at say 2:00 AM, no lights have been adjusted on or off for say an hour, trigger All off Scene?

The short answer is yes but what do you want to do in the case where, at 02:00, one of the lights has changed within the last hour? Try again at 03:00?

Yup, that would make sense.

Why is there a concern for the lights that may have been turned off in the previous hour? If the goal is to turn off lights at 02:00 then it only needs to concern itself with the lights that are on (and have been on for more than an hour). Or have I misunderstood this automation’s purpose?

That was bad description of the automation purpose. We don’t need to monitor off, just on and brightness changes.

This automation assumes you have an existing group containing all 88 lights.

alias: example 1
variables:
  lights_on: >
    {{ expand('group.all_lights')
        | selectattr('last_changed', 'lt', now() - timedelta(hours=1))
        | selectattr('state', 'eq', 'on')
        | map(attribute='entity_id') | list }}
trigger:
- platform: time
  at:
  - '02:00:00'
  - '03:00:00'
condition: "{{ lights_on | count > 0 }}"
action:
- service: light.turn_off
  target:
    entity_id: "{{ lights_on }}"

Be advised that an entity’s last_changed property is reset to the current time at startup. In other words, whenever Home Assistant restarts, entities get their last_changed altered (thereby losing whatever value they had). This shouldn’t present a problem unless your instance of Home Assistant is restarted during the hour prior to 02:00. The calculation might also be affected when DST changes occur (typically at 02:00).


EDIT

Correction. Replaced entity_id with state within template for lights_on variable.

1 Like

@123 , You’re a genius. Really appreciate the help. I had to tweak one of the expand limiters from entity_id to state which was causing an issue, but was a good homework assignment which forced me to learn how to use this. I slightly modified the automation after playing around with it and adjusting the scenario’s.

alias: NightNanny-Station-InteriorLights
description: >-
  Checks to see if lights were left on by checking at night if lights were
  recently turned on or off
trigger:
  - platform: time_pattern
    minutes: /15
condition:
  - condition: time
    after: '00:00:00'
    before: '04:30:00'
  - condition: template
    value_template: '{{ lights_on | count > 0 }}'
action:
  - service: scene.turn_on
    target:
      entity_id: scene.phantom_phantomone_station_off
mode: single
variables:
  lights_on: |
    {{ expand('group.stationinteriorlights')
        | selectattr('last_changed', 'lt', now() - timedelta(minutes=60))
        | selectattr('state', 'eq', 'on')
        | map(attribute='entity_id') 
        | list }}

I have corrected the example posted above (it was due to a leftover from a copy-paste). Thanks for bringing it to my attention.


The Time Pattern Trigger you added makes the automation slightly busier than necessary. It causes the automation’s condition to be evaluated every 15 minutes throughout the entire day, even though we already know in advance that the time range of interest is just between midnight and 04:30. In other words, about 96 times a day instead of only about 18 times.

To be fair, it’s not like it expends a lot of CPU cycles evaluating the condition every quarter-hour outside of the desired time range. However, an alternative is to explicitly specify the desired times in a Time Trigger (like the way it’s done in the original example). Admittedly, it means about 18 time entries but that’s just a bit of the ol’ copy-paste (and then remove the Time Condition you added).

Anyway, it’s your choice.