Global variable in automation

Hey there,

I know this topic has been discussed many times (thus I am very sorry for asking again) but I was unable to find the solution in the forum.
I’d like to set the value of a variable depending on a condition and use the variable afterwards (outside of the IF block). As stated in the docs variables have their own scope and are reset after the if.
Take the example from the docs

sequence:
  # Set the people variable to a default value
  - variables:
      people: 0
  # Try to increment people if Paulus is home
  - if:
      - condition: state
        entity_id: device_tracker.paulus
        state: "home"
    then:
      # At this scope and this point of the sequence, people == 0
      - variables:
          people: "{{ people + 1 }}"
      # At this scope, people will now be 1 ...
      - action: notify.notify
        data:
          message: "There are {{ people }} people home" # "There are 1 people home"
  # ... but at this scope it will still be 0
  - action: notify.notify
    data:
      message: "There are {{ people }} people home" # "There are 0 people home"

How can I achieve that there are 1 people home in the last action?

The only solution I found was to create helpers for the variables, however as I want to define multiple variables it does not seem appropriate/efficient to define so many helpers that are only used in one automation.
It has to be possible to define global variables, right?


Background (not that important)
I want to create a notification with different content and to different users depending on a condition. Currently I have all steps after the variable definition duplicated in each branch of the condition which creates a whole lot of code duplication.

alias: "Benachrichtigung: Wenn Felicitas auf dem Heimweg ist"
description: ""
triggers:
  - alias: Wenn Felicitas sich an zuhause annähert
    trigger: state
    entity_id:
      - sensor.home_bewegung_von_felicitas
    for:
      hours: 0
      minutes: 2
      seconds: 0
    to: towards
    id: felicitas_heimweg
  - alias: Wenn Felix an zuhause annähert
    trigger: state
    entity_id:
      - sensor.home_bewegung_von_felix
    for:
      hours: 0
      minutes: 2
      seconds: 0
    to: towards
    id: felix_heimweg
conditions: []
actions:
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ trigger.id == 'felicitas_heimweg' }}"
        sequence:
          - variables:
              person_coming: seine Freundin
              user: den Nutzer
              distance: "{{ states('sensor.home_entfernung_von_felicitas') }}"
              unit_of_measurement: >-
                {{ state_attr('sensor.home_entfernung_von_felicitas',
                'unit_of_measurement') }}
              phone_to_notify: mobile_app_felix_handy
              notify_title: Felicitas ist auf dem Heimweg
          - variables:
              main_prompt: >-
                Benachrichtige {{ user }} darüber, dass {{ person_coming }} auf
                dem Heimweg ist und aktuell {{ distance }}{{ unit_of_measurement
                }} entfernt ist. Sei glücklich und enthusiastisch darüber.
                Schreibe genau einen kurzen Satz. Benutze ein glückliches Emoji
                am Ende.
          - action: google_generative_ai_conversation.generate_content
            metadata: {}
            data:
              prompt: "{{ main_prompt }}"
            response_variable: response
          - action: notify.{{ phone_to_notify }}
            metadata: {}
            data:
              message: "{{ response.text }}"
        alias: Wenn Felicitas auf dem Heimweg ist
      - conditions: []
        sequence:
          - variables:
              person_coming: ihr Freund
              user: die Nutzerin
              distance: "{{ states('sensor.home_entfernung_von_felix') }}"
              unit_of_measurement: >-
                {{ state_attr('sensor.home_entfernung_von_felix',
                'unit_of_measurement') }}
              phone_to_notify: mobile_app_felicitas_handy
              notify_title: Felix ist auf dem Heimweg
          - variables:
              main_prompt: >-
                Benachrichtige {{ user }} darüber, dass {{ person_coming }} auf
                dem Heimweg ist und aktuell {{ distance }}{{ unit_of_measurement
                }} entfernt ist. Sei glücklich und enthusiastisch darüber.
                Schreibe genau einen kurzen Satz. Benutze ein glückliches Emoji
                am Ende.
          - action: google_generative_ai_conversation.generate_content
            metadata: {}
            data:
              prompt: "{{ main_prompt }}"
            response_variable: response
          - action: notify.{{ phone_to_notify }}
            metadata: {}
            data:
              title: "{{ notify_title }}"
              message: "{{ response.text }}"
        alias: Wenn Felicitas auf dem Heimweg ist
mode: single

Thank you for any hints, I appreciate it! :slight_smile:

Cheers,
Felix

One option would be to assign the variables at the trigger and skip the Choose:

alias: "Benachrichtigung: Wenn Felicitas auf dem Heimweg ist"
description: ""
triggers:
  - alias: Wenn Felicitas sich an zuhause annähert
    trigger: state
    entity_id:
      - sensor.home_bewegung_von_felicitas
    for:
      hours: 0
      minutes: 2
      seconds: 0
    to: towards
    id: felicitas_heimweg
    variables:
      person_coming: seine Freundin
      user: den Nutzer
      distance: "{{ states('sensor.home_entfernung_von_felicitas') }}"
      unit_of_measurement: >-
        {{ state_attr('sensor.home_entfernung_von_felicitas',
        'unit_of_measurement') }}
      phone_to_notify: mobile_app_felix_handy
      notify_title: Felicitas ist auf dem Heimweg
  - alias: Wenn Felix an zuhause annähert
    trigger: state
    entity_id:
      - sensor.home_bewegung_von_felix
    for:
      hours: 0
      minutes: 2
      seconds: 0
    to: towards
    id: felix_heimweg
    variables:
      person_coming: ihr Freund
      user: die Nutzerin
      distance: "{{ states('sensor.home_entfernung_von_felix') }}"
      unit_of_measurement: >-
        {{ state_attr('sensor.home_entfernung_von_felix',
        'unit_of_measurement') }}
      phone_to_notify: mobile_app_felicitas_handy
      notify_title: Felix ist auf dem Heimweg
conditions: []
actions:
  - variables:
      main_prompt: >-
        Benachrichtige {{ user }} darüber, dass {{ person_coming }} auf
        dem Heimweg ist und aktuell {{ distance }}{{ unit_of_measurement
        }} entfernt ist. Sei glücklich und enthusiastisch darüber.
        Schreibe genau einen kurzen Satz. Benutze ein glückliches Emoji
        am Ende.
  - action: google_generative_ai_conversation.generate_content
    metadata: {}
    data:
      prompt: "{{ main_prompt }}"
    response_variable: response
  - action: notify.{{ phone_to_notify }}
    metadata: {}
    data:
      message: "{{ response.text }}"
        alias: Wenn Felicitas auf dem Heimweg ist
mode: single

Interesting, I didn’t know that was possible. Thanks for the idea! I will check it out :slight_smile:

Edit: Works, thanks!