Why, in HA automations and scripts etc. are Variables not updateable?
I’m sure there must be a good, deep reason, and I’d like to understand it.
In every other programming language/environment I’ve used, there has always been the ability to both declare and update a variable with a (new) value. I.e. there’s an assignment operator, usually = or :=, but in HA not so.
In HA scripts as far as I understand, e.g. “people: value”, creates a new variable (with its own local scope) each time it’s used.
Example:
example_script:
variables:
people: 9 # Var1
sequence:
- variables:
people: 0 # Var2
- if: "{{ some_condition }}"
then:
# At this scope and this point of the sequence, people == 0 ( Var2 is seen)
- variables:
people: "{{ people + 1 }}" # Var3 is created, NOT Var2 updated.
# At this scope, people will now be 1 ... but that's Var3.
- action: notify.notify
data:
message: "There are {{ people }} people home" # "There are 1 people home"
# ... but at this scope 'people' (Var2) will still be 0
- action: notify.notify
data:
message: "There are {{ people }} people home" # "There are 0 people home"
# people (Var1) is still 9, but ?unreachable?
1 post - 1 participant