Document scope of script variables (#19240)

This commit is contained in:
Michael Chisholm 2021-10-12 00:29:08 +11:00 committed by GitHub
parent 9e0ac02e63
commit ab6adc1db0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -75,6 +75,39 @@ The variables action allows you to set/override variables that will be accessibl
{% endraw %} {% endraw %}
### Scope of Variables
Variables have local scope. This means that if a variable is changed in a nested sequence block, that change will not be visible in an outer sequence block.
The following example will always say "There are 0 people home". Inside the `choose` sequence the `variables` action will only alter the `people` variable for that sequence.
{% raw %}
```yaml
sequence:
# Set the people variable to a default value
- variables:
people: 0
# Try to increment people if Paulus is home
- choose:
- conditions:
- condition: state
entity_id: device_tracker.paulus
state: "home"
sequence:
# At this scope and this point of the sequence, people == 0
- variables:
people: "{{ people + 1 }}"
# At this scope, people will now be 1 ...
# ... but at this scope it will still be 0
# Announce the count of people home
- service: notify.notify
data:
message: "There are {{ people }} people home"
```
{% endraw %}
## Test a Condition ## Test a Condition
While executing a script you can add a condition in the main sequence to stop further execution. When a condition does not return `true`, the script will stop executing. There are many different conditions which are documented at the [conditions page]. While executing a script you can add a condition in the main sequence to stop further execution. When a condition does not return `true`, the script will stop executing. There are many different conditions which are documented at the [conditions page].