If have read in some messages of this forum that apparently variables used within an IF statement are not visible outside the IF statement.
I would like to create a script where I would initialise a string with a given value via such a statement:
variable:
mystring: toto
Then with several IF statements I would like to add some text to mystring based on the tests made by IF statements.
But I have seen that I cannot modify my string mystring within the IF statement.
Is there an other mechanism of is it possible to modify the default scope of a variable to achieve what I would like to so ?
If it works like variable scope does in most other languages, just define the variable outside the if, then it should have global scope in the script, then just assign it inside your IF statement.
The variable inside the if is local to the if even if it has the same name as a variable defined outside the if. Therefore changes to the value of variable x defined inside the if donāt appear in variable x defined outside the if.
Or at least thatās how it worked the last time I checked ā¦
I hope there is a plan to update this in the future. To adhere to this logic will just require more actions within a Script.
The simple below does not work because the IF statement does not change the variable value.
alias: TTS - Washing Machine
sequence:
- variables:
ttsm: Placeholder
ttsm1: >-
Good morning team, the clothes washer is complete.
ttsm2: >-
The clothes washer is complete. Have a great day!
- if:
- condition: time
before: "12:00:00"
then:
- variables:
ttsm: "{{ ttsm1 }}"
else:
- variables:
ttsm: "{{ ttsm2 }}"
- service: tts.google_translate_say
data:
cache: false
entity_id: media_player.mpd3
message: "{{ ttsm }}"
- wait_for_trigger:
- platform: state
entity_id:
- media_player.mpd3
to: "off"
continue_on_timeout: false
mode: single
For me, Iād rather not install a custom component to work around this BUT you can just use the built in service input_text.set_value for an input_text helper. The variable then will be GLOBAL to your entire app though.
For example in your config
input_text:
#tts washing machine global message
ttsm:
name: TTS Message
Your new Script would be
alias: TTS - Washing Machine
sequence:
- if:
- condition: time
before: "12:00:00"
then:
- service: input_text.set_value
target:
entity_id: input_text.ttsm
data:
value: "Good morning team, the clothes washer is complete."
else:
- service: input_text.set_value
target:
entity_id: input_text.ttsm
data:
value: "The clothes washer is complete. Have a great day!"
- service: tts.google_translate_say
data:
cache: false
entity_id: media_player.mpd3
message: "{{ states('input_text.ttsm') }}"
- wait_for_trigger:
- platform: state
entity_id:
- media_player.mpd3
to: "off"
continue_on_timeout: false
mode: single
Thereās no obligation to compute a variableās value via a scriptās if - then. It can be done when the variable is defined, using a template. It requires less code and no custom integration.
Your first example revised:
alias: TTS - Washing Machine
sequence:
- variables:
ttsm1: >-
Good morning team, the clothes washer is complete.
ttsm2: >-
The clothes washer is complete. Have a great day!
ttsm: "{{ iif(now().hour < 12, ttsm1, ttsm2) }}"
- service: tts.google_translate_say
data:
cache: false
entity_id: media_player.mpd3
message: "{{ ttsm }}"
- wait_for_trigger:
- platform: state
entity_id:
- media_player.mpd3
to: "off"
continue_on_timeout: false
mode: single
Amazing?
Thatās still a if statement but a more complicated and much less intuitive one. I really donāt understand why I canāt define a variable in a in normal if-then block. I lost almost an hour trying to figure why my automation was not working. There is maybe a ātechnicalā reason but thatās not a good reason from a userfriendlyness standpoint, which should always be the mindset to adopt.
Yeah I saw that. By that I meant āImmediate Ifā vs āIf-then blockā. In the end Iāll get the same result but I will have had to take the complicated way.
Again, I read that and thatās what I meant by ācanāt defineā, or should I have said ācanāt define in way so itās really usefulā. The whole point of defining the variable was to be used in some later part of the automation to avoid duplication of code. As many people mentioned, here variables are more constants.