Time Delay for Lighting Control

Hi All, I’ve seen this come up a few times, but not for a while and given the pace of updates, I’m hoping there’s a better way now…

My HA setup (RPi, ZWave) earns most of its keep controlling lights (lamps, xmas lights, etc), mainly through smart plugs. Whilst the control of on/off is pretty good and I have some more advanced features in there too, including motion control for one in the kids area, random off time within 10 min window, I’d like to space out the off time so there’s a delay between each light going out - i.e. not all at the same time.

As I mentioned, there are some topics on this where the solutions seem oddly complex, but hoping there’s something a little cleaner now.

Thanks in advance,

Anthony…

So you have an automation that turns off all the lights, and you want to space it out? Or is this something you want to sprinkle into all your automations?

You can just do a random delay before turning off

sequence:
  - service: light.turn_on
    entity_id: light.my_light
  - delay:
      # Turn off somewhere between 5 to 10 minutes from now
      minutes: "{{ range(5,10) | random }}"
  - service: light.turn_off
    entity_id: light.my_light

Or, if you have a ‘goodnight’ type automation that turns off all lights. just do a repeat sequence.

  - variables:
       on_lights: "{{states.light | selectattr('state', 'eq', 'on') | map(attribute='entity_id') | list }}"
  - repeat:
      while:
        - condition: template
          value_template: "{{ on_lights | count > 0 }}"
        # Don't do it too many times
        - condition: template
          value_template: "{{ repeat.index <= 20 }}"
      sequence:
        - delay: 
            minutes: "{{ range(5,10) | random }}"
        - service: light.turn_off
          data:
            # Pick a light at random.  Or pick 'first' or 'last'. Doesn't matter
            entity_id: "{{ on_lights | random }}"
        # update the variables for the next loop
        - variables:
            on_lights: "{{states.light | selectattr('state', 'eq', 'on') | map(attribute='entity_id') | list }}"

…correct - I have a “Bedtime” automation that triggers the various lights to go out - I’ll take a look properly at the above, but seems to be what I’m looking for - thanks very much. Will report back later!