Repeat for each
The Repeat for each building block runs a group of actions once for each item in a list. Use it when you want to do the same thing for several items, for example, to turn off a list of lights one by one.
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 for each.
-
In For each item in list, enter the list of items in YAML, for example:
- "living_room" - "kitchen" -
Under Actions, select Add action, and add the actions to run for each item. To use the current item in a template, use
repeat.item. -
Select Save.
Options in the UI
Using this building block in YAML
In YAML, use repeat with for_each and sequence. This example turns off three lights:
repeat:
for_each:
- "living_room"
- "kitchen"
- "office"
sequence:
- action: light.turn_off
target:
entity_id: "light.{{ repeat.item }}"
The items can also be templates, or mappings of keys and values:
repeat:
for_each:
- language: English
message: Hello World
- language: Dutch
message: Hallo Wereld
sequence:
- action: notify.phone
data:
title: "Message in {{ repeat.item.language }}"
message: "{{ repeat.item.message }}!"
Options in YAML
The repeat settings.
The list of items, or a template that returns a list.
Repeat variable
While the actions repeat, the repeat variable shows which item is used:
-
repeat.item: The current item of the list. -
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
- The list can be a fixed list, or a list created by a template. If the template doesn’t return a list, the automation or script stops with an error.
Examples
Automation: Send a notification for each sensor with low battery
Every evening, this automation goes through all entities with the labelLabels in Home Assistant allow grouping elements irrespective of their physical location or type. Labels can be assigned to areas, devices, entities, automations, scenes, scripts, and helpers. Labels can be used in automations and scripts as a target for actions. Labels can also be used to filter data. [Learn more] “Battery check”, and sends a separate notification for each one that is below 20%. The notification includes the name of the sensor. Create the label yourself, and add it to your battery sensors, in Settings > Areas, labels & zones > Labels.
The list comes from a template, so it changes when you add the label to more sensors. Inside the repeat, repeat.item is the entity ID of the current sensor.
-
Trigger: Time
-
At time:
20:00
-
At time:
-
Action: Repeat for each
-
For each item in list:
{{ label_entities('Battery check') }} -
Actions:
-
Action: If-then
-
If:
-
Condition: Template
-
Value template:
{{ states(repeat.item) | float(100) < 20 }}
-
Value template:
-
Condition: Template
-
Then:
-
Action: Send a notification message
-
Target: My Device (
notify.my_device) -
Message:
{{ state_attr(repeat.item, 'friendly_name') }} has {{ states(repeat.item) }}% battery left.
-
Target: My Device (
-
Action: Send a notification message
-
If:
-
Action: If-then
-
For each item in list:
YAML example
alias: "Notify about low batteries"
triggers:
- trigger: time
at: "20:00:00"
actions:
- repeat:
for_each: "{{ label_entities('Battery check') }}"
sequence:
- if:
- "{{ states(repeat.item) | float(100) < 20 }}"
then:
- action: notify.send_message
target:
entity_id: notify.my_device
data:
message: >
{{ state_attr(repeat.item, 'friendly_name') }} has
{{ states(repeat.item) }}% battery left.