From 6f92bbdcb9a1a79c0ab9db560ecbd1db0578f6b8 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Sat, 16 Apr 2022 13:34:18 +0200 Subject: [PATCH] Add for each item support to repeat action (#22400) --- source/_docs/scripts.markdown | 47 +++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/source/_docs/scripts.markdown b/source/_docs/scripts.markdown index c5f2d1ddb20..af9d685fa9f 100644 --- a/source/_docs/scripts.markdown +++ b/source/_docs/scripts.markdown @@ -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