Fix repeat & choose action descriptions (#14080)

* Fix repeat & choose action descriptions

* Change repeat & choose examples to complete scripts/automations to reduce confusion
This commit is contained in:
Phil Bruckner 2020-07-25 11:45:53 -05:00 committed by GitHub
parent 8539c6a32b
commit 1187058a22
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -206,7 +206,7 @@ The following automation shows how to capture the custom event `event_light_stat
### Repeat a Group of Actions ### Repeat a Group of Actions
This action allows you to repeat a sequence of other actions. Nesting is fully supported. This action allows you to repeat a sequence of other actions. Nesting is fully supported.
There are three ways to control how many times the sequence will be repeated. There are three ways to control how many times the sequence will be run.
#### Counted Repeat #### Counted Repeat
@ -215,57 +215,93 @@ the template is rendered when the repeat step is reached.
{% raw %} {% raw %}
```yaml ```yaml
- alias: Repeat the sequence the specified number of times script:
repeat: flash_light:
count: "{{ repeat_count }}" mode: restart
sequence: sequence:
- ... - service: light.turn_on
data_template:
entity_id: "light.{{ light }}"
- repeat:
count: "{{ count|int * 2 - 1 }}"
sequence:
- delay: 2
- service: light.toggle
data_template:
entity_id: "light.{{ light }}"
flash_hallway_light:
sequence:
- service: script.flash_light
data:
light: hallway
count: 3
``` ```
{% endraw %} {% endraw %}
#### While Loop #### While Loop
This form accepts a list of conditions that are evaluated _before_ each time the sequence This form accepts a list of conditions (see [conditions page] for available options) that are evaluated _before_ each time the sequence
is run. The sequence will be repeated _as long as_ the condition(s) evaluate to true. is run. The sequence will be run _as long as_ the condition(s) evaluate to true.
{% raw %} {% raw %}
```yaml ```yaml
- alias: Repeat the sequence AS LONG AS the conditions are true script:
repeat: do_something:
while:
- condition: state
entity_id: input_boolean.run_loop
state: 'on'
- condition: template
value_template: "{{ repeat.index <= 20 }}"
sequence: sequence:
- ... - service: script.get_ready_for_something
- alias: Repeat the sequence AS LONG AS the conditions are true
repeat:
while:
- condition: state
entity_id: input_boolean.do_something
state: 'on'
# Don't do it too many times
- condition: template
value_template: "{{ repeat.index <= 20 }}"
sequence:
- service: script.something
``` ```
{% endraw %} {% endraw %}
#### Repeat Until #### Repeat Until
This form accepts a list of conditions that are evaluated _after_ each time the sequence This form accepts a list of conditions that are evaluated _after_ each time the sequence
is run. Therefore the sequence will always run at least once. The sequence will be executed is run. Therefore the sequence will always run at least once. The sequence will be run
_until_ the condition(s) evaluate to true. _until_ the condition(s) evaluate to true.
{% raw %} {% raw %}
```yaml ```yaml
- alias: Repeat the sequence UNTIL the conditions are true automation:
repeat: - trigger:
sequence: - platform: state
- ... entity_id: binary_sensor.xyz
until: to: 'on'
condition:
- condition: state - condition: state
entity_id: binary_sensor.the_cows_have_come_home entity_id: binary_sensor.something
state: 'on' state: 'off'
mode: single
action:
- alias: Repeat the sequence UNTIL the conditions are true
repeat:
sequence:
# Run command that for some reason doesn't always work
- service: shell_command.turn_something_on
# Give it time to complete
- delay:
milliseconds: 200
until:
# Did it work?
- condition: state
entity_id: binary_sensor.something
state: 'on'
``` ```
{% endraw %} {% endraw %}
#### Repeat Loop Variable #### Repeat Loop Variable
A variable named `repeat` is defined within the repeat sequence. If repeat sequences are A variable named `repeat` is defined within the repeat action (i.e., it is available inside `sequence`, `while` & `until`.)
nested, it always applies to the inner-most loop. It contains the following fields: It contains the following fields:
field | description field | description
-|- -|-
@ -277,25 +313,31 @@ field | description
This action allows you to select a sequence of other actions from a list of sequences. This action allows you to select a sequence of other actions from a list of sequences.
Nesting is fully supported. Nesting is fully supported.
Each sequence is paired with a list of conditions (see [conditions page] for available options.) The first sequence whose conditions are all true will be run.
Each sequence is paired with a list of conditions. The first sequence whose conditions are all true will be run.
An optional `default` sequence can be included which will be run if none of the sequences from the list are run. An optional `default` sequence can be included which will be run if none of the sequences from the list are run.
{% raw %} {% raw %}
```yaml ```yaml
- alias: Choose a sequence to run automation:
choose: - trigger:
- conditions: - platform: state
- condition: ... entity_id: binary_sensor.motion
- condition: ... mode: queued
sequence: action:
- ... - choose:
- conditions: # IF motion detected
- condition: ... - conditions:
sequence: - condition: template
- ... value_template: "{{ trigger.to_state.state == 'on' }}"
default: sequence:
- ... - service: script.turn_on
entity_id:
- script.slowly_turn_on_front_lights
- script.announce_someone_at_door
# ELSE (i.e., motion stopped)
default:
- service: light.turn_off
entity_id: light.front_lights
``` ```
{% endraw %} {% endraw %}