Add for each item support to repeat action (#22400)

This commit is contained in:
Franck Nijhof 2022-04-16 13:34:18 +02:00 committed by GitHub
parent f36774bfc9
commit 6f92bbdcb9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -411,6 +411,53 @@ script:
{% endraw %}
### For each
This repeat form accepts a list of items to iterate over. The list of items
can be a pre-defined list, or a list created by a template.
The sequence is ran for each item in the list, and current item in the
iteration is available as `repeat.item`.
The following example will turn a list of lights:
{% raw %}
```yaml
repeat:
for_each:
- "living_room"
- "kitchen"
- "office"
sequence:
- service: light.turn_off
target:
entity_id: "light.{{ repeat.item }}"
```
{% endraw %}
Other types are accepted as list items, for example, each item can be a
template, or even an mapping of key/value pairs.
{% raw %}
```yaml
repeat:
for_each:
- language: English
message: Hello World
- language: Dutch
hello: Hallo Wereld
sequence:
- service: notify.phone
data:
title: "Message in {{ repeat.item.language }}"
message: "{{ repeat.item.message }}!"
```
{% endraw %}
### While Loop
This form accepts a list of conditions (see [conditions page] for available options) that are evaluated _before_ each time the sequence