Using a label but changing timings for items part of the same label

I have a label called “lamps” that I use in my automation to turn on all lamps at a certain time.

Is there away to do this so that the lamps turn on at slightly different times from each other i.e. there is a random delay before each lamp switches on.

alias: "Lighting: Sunset - turn on"
description: ""
trigger:
  - platform: sun
    event: sunset
    offset: "-00:15:00"
condition: []
action:
  - service: switch.turn_on
    data: {}
    target:
      label_id: lamps
mode: single

Use a Repeat for Each action with a template to provide the entities that share the label, and a random length delay. The example below will provide a random 10-60 second delay between turning on each item labelled lamps.

alias: "Lighting: Sunset - turn on"
description: ""
trigger:
  - platform: sun
    event: sunset
    offset: "-00:15:00"
condition: []
action:
  - repeat:
      for_each: "{{ label_entities('lamps') }}"
      sequence:
        - service: homeassistant.turn_on
          target:
            entity_id: "{{ repeat.item }}"
        - delay:
            seconds: "{{ range(10, 60)|random }}"
mode: single

The generic service homeassistant.turn_on is used just-in-case, so that the automation will continue to work without issue if you add the label to any light entities in the future.

@Didgeridrew any thanks :slight_smile:

I also have a script that I use with a button.

With your help got the following too :slight_smile:

alias: Switch on  all Lamps (Duplicate)
sequence:
  - repeat:
      for_each: "{{ label_entities('lamps') }}"
      sequence:
        - service: homeassistant.turn_on
          target:
            entity_id: "{{ repeat.item }}"
        - delay:
            seconds: "{{ range(10, 60)|random }}"
mode: single
icon: mdi:desk-lamp-on

I have the following Lamps

image

When I run the above scripts, all except “smartswitch-white-lamp-01” come on.

This is the last switch that I have added to the Lamps group. Configured exactly the same as the others.

Any idea why the loop might stop before this last one is turned on or off?

If I run the below, they all turn off or on (depending on the script)

alias: Switch off  all Lamps
sequence:
  - service: switch.turn_off
    target:
      label_id: lamps
    data: {}
mode: single
icon: mdi:desk-lamp-off

It’s just like the repeat loop I use for the random sequence, times out or completes before reaching all the lamps. This said the Script stays toggled on for a while i.e. it is running but the last lamp (smartwitch-white-lamp-01) is not affected.