Repeat multiple times
The Repeat multiple times building block runs a group of actions a set number of times. Use it when you know in advance how often the actions should run, for example, to flash a light 3 times.
Using this building block from the user interface
- Go to Settings > Automations & scenes and select an automation. To edit a script, go to the Scripts tab and select the script.
- In the Then do section of an automation, or the Sequence section of a script, select Add action.
- Select Blocks, then select Repeat multiple times.
- In Count, enter how many times to run the actions.
- Under Actions, select Add action, and add the actions to repeat.
- Select Save.
Options in the UI
Using this building block in YAML
In YAML, use repeat with count and sequence. The count can be a template. The template is rendered when the repeat starts. This example has two scripts. The second script calls the first one to flash the hallway light 3 times:
flash_light:
mode: restart
sequence:
- action: light.turn_on
target:
entity_id: "light.{{ light }}"
- alias: "Cycle light 'count' times"
repeat:
count: "{{ count | int * 2 - 1 }}"
sequence:
- delay: 2
- action: light.toggle
target:
entity_id: "light.{{ light }}"
flash_hallway_light:
sequence:
- alias: "Flash hallway light 3 times"
action: script.flash_light
data:
light: hallway
count: 3
Options in YAML
The repeat settings.
How many times to run the actions. Can be a template.
Repeat variable
While the actions repeat, the repeat variable shows which round is running:
-
repeat.index: The number of the round:1,2,3, and so on. -
repeat.first:trueduring the first round. -
repeat.last:trueduring the last round.
Good to know
- You can use any building block inside the repeat, including another repeat.