Turn lights on with delay

Hi everyone,

I have a group of lights an want to turn them on one-by-one with an delay of a second:

alias: Treppenlicht_auf
description: ""
trigger:
  - type: turned_on
    platform: device
    device_id: 9c208a4e400da6eaf1130ea4d080143a
    entity_id: 5510139321e4a0347034c5367bc24e12
    domain: binary_sensor
  - type: turned_on
    platform: device
    device_id: 2ac981c5f7da6af0e4f9462deda7dc47
    entity_id: 29b940a5cf00bb394f9afa880d66adff
    domain: binary_sensor
condition: []
action:
  - type: turn_on
    device_id: c03832049754246119fe09162727fdd1
    entity_id: 55e9c2d7301e000ab5017db3841d4d36
    domain: light
  - delay:
      hours: 0
      minutes: 0
      seconds: 1
      milliseconds: 0
  - type: turn_on
    device_id: 5762229f89a420ab03f5a1eae7a8f2b1
    entity_id: e32ef13b8e34dcb7a70b5377f4243fae
    domain: light
  - delay:
      hours: 0
      minutes: 0
      seconds: 1
      milliseconds: 0
  - type: turn_on
    device_id: 8515ea3227fe6ed1f23ac02d027cf2c7
    entity_id: 17558781fa63f05c9fdb0bc9ceb1ea38
    domain: light
  - delay:
      hours: 0
      minutes: 0
      seconds: 1
      milliseconds: 0
  - type: turn_on
    device_id: 7805245e7e7a2b790d67c77530bbdce0
    entity_id: 9793ad7651b94cfd416e177d1561e08e
    domain: light
  - delay:
      hours: 0
      minutes: 0
      seconds: 1
      milliseconds: 0
  - type: turn_on
    device_id: 89e5899f5e2b75a9a079644a5e1f86f5
    entity_id: 88cb3b51521c813e63fadae19671e941
    domain: light
  - delay:
      hours: 0
      minutes: 0
      seconds: 10
      milliseconds: 0
  - service: light.turn_off
    data: {}
    target:
      entity_id: light.treppenlichter
  - service: python_script.set_state
    data_template:
      entity_id: binary_sensor.sensor_schaltaktor_2_1_fach_r_diele
      state: "OFF"
mode: single

I also have all lights in a group (light.treppenlichter) but don’t know the best aproach to iterate trough them. Could anybody give an advice?

You can use a Repeat for Each action to iterate through the members of the light group as follows:

action:
  - repeat:
      for_each: "{{ state_attr('light.treppenlichter', 'entity_id') }}"
      sequence:
        - service: light.turn_on
          target:
            entity_id: "{{ repeat.item }}"
        - delay: 1
  - delay: 10
...

If you want them to turn on in a particular order you can either use more templating to sort the light entities or by listing the entities manually as shown in the action docs.

Thank you very much - works great :smiley:
I tried to sort everything by name like described (Templating - Home Assistant) but it doesn’t work:

"{{ state_attr('light.treppenlichter', 'entity_id')|sort(attribute="entity_id") }}"

OUT:

"['light.treppe_spot5', 'light.treppe_spot4', 'light.treppe_spot3', 'light.treppe_spot2', 'light.treppe_spot1']"```

Since what the state_attr() is returning is just a list of the entity ID strings, not their state objects, you just use sort() without additional arguments.

"{{ state_attr('light.treppenlichter', 'entity_id') | sort() | list }}"
1 Like