Using variable within scripts


This is my first post. Please apologize if I am not using the forum corectly.
I want to set the variable “result” in the following script but the statement “result: “hall”” is not working

How can I manipulate a script wide variable?

alias: Testscript
- variables:
    result: "asdf"
    test: 20

- if: "{{ test == 20 }}"
    result: "hall"

- service: notify.marty1945
    message: "{{ result }}"
mode: single

Read up on scope of variables:

You’ll need an if / else with a notify call in each section at the same scope level as the variable being set.

Also, you need a variables: declaration ahead of your line that changes result.

You absolutely are. Welcome! :slight_smile:

Thank you for your reply and your warn words.

I am still struggling with the principal functionality of Yaml and I am not sure whether I understand the concept correctly
The solution you are suggesting is working but seems rather limited and would not work if the user wants to calculated something.

The kind of “workaround” I found is the use this Jinja inline language.


alias: Test working
  - variables:
      yamlkey: |
        {% set result="hallo"                                    %}
        {% if (states("switch.living_room") == "off")  %}
        {%  set result="Hallo2" + "Test"                          %}
        {% else                                                  %}
        {%  set result="asdfasdf"                                %}
        {% endif                                                 %}
        {% print(result) %}
  - service: notify.marty1945
      message: "{{ yamlkey }}"
mode: single

Is my understanding correct?
Mixing this two different “languages” (Yaml and JInja) with pretty different syntax is a bit strange.
The alternative would be to use Phython as a scripting language but this seems not to be encouraged.

What have been the reason for this appraoch?
Has this historical reasons or is there a hidden concept with great benefits I do not understand?