How to make variables common to 2 templates from the same automation?

Hello :wave:
I would like to make an automation to notify the last person to arrive home that they can lock the door.
To achieve this, I start the automation when 5 people are at home, then I compare their arrival time to get the name of the last person arrived home.

However, I’ve run into a problem : my template variables are not global.
For the moment, I’ve used a workaround and added my template twice, but is there a way to store the name of the last person arrived home ?
I’ve tried using variables, but I couldn’t adapt my template to this.

Thanks for your help!
Here’s my code:

alias: "[Divers] Notification de fermeture du volet"
description: >-
  Envoie une notification à la dernière personne rentrée à la maison pour
  qu'elle ferme le volet.
trigger:
  - [...]
condition:
  - [...]
action:
  - service: >
      {% set sensors_to_monitor = ['person.person1', 'person.person2', 'person.person3', 'person.person4', 'person.person5'] %}
      {% set last_changed_states = states[sensors_to_monitor[0]] %}
      {% set last_changed_time = last_changed_states.last_changed %}

      {% for sensor_name in sensors_to_monitor[1:] %}
        {% if states[sensor_name].last_changed > last_changed_time %}
          {% set last_changed_states = states[sensor_name] %}
          {% set last_changed_time = last_changed_states.last_changed %}
        {% endif %}
      {% endfor %}

      notify.mobile_app_smartphone_{{last_changed_states.name | lower}}
    continue_on_error: true
    data:
      title: Dernière personne à la maison
      message: Tu peux refermer le volet.
  - [...]
  - service: tts.google_translate_say
    data:
      cache: true
      entity_id: media_player.cuisine
      message: >
        {% set sensors_to_monitor = ['person.person1', 'person.person2', 'person.person3', 'person.person4', 'person.person5'] %}
        {% set last_changed_states = states[sensors_to_monitor[0]] %}
        {% set last_changed_time = last_changed_states.last_changed %}

        {% for sensor_name in sensors_to_monitor[1:] %}
          {% if states[sensor_name].last_changed > last_changed_time %}
            {% set last_changed_states = states[sensor_name] %}
            {% set last_changed_time = last_changed_states.last_changed %}
          {% endif %}
        {% endfor %}

        Bonsoir {{last_changed_states.name}} ! Tu es la dernière personne à
        rentrer à la maison. Tu peux refermer le volet.
  - [...]
mode: single
alias: "[Divers] Notification de fermeture du volet"
description: >-
  Envoie une notification à la dernière personne rentrée à la maison pour
  qu'elle ferme le volet.
trigger:
  - [...]
condition:
  - [...]
action:
  - variables:
      last_person: >
        {% set sensors_to_monitor = ['person.person1', 'person.person2', 'person.person3', 'person.person4', 'person.person5'] %}
        {% set last_changed_states = states[sensors_to_monitor[0]] %}
        {% set last_changed_time = last_changed_states.last_changed %}
        {% for sensor_name in sensors_to_monitor[1:] %}
          {% if states[sensor_name].last_changed > last_changed_time %}
            {% set last_changed_states = states[sensor_name] %}
            {% set last_changed_time = last_changed_states.last_changed %}
          {% endif %}
        {% endfor %}
        {{ last_changed_states.name }}
  - service: notify.mobile_app_smartphone_{{last_person | lower}}
    continue_on_error: true
    data:
      title: Dernière personne à la maison
      message: Tu peux refermer le volet.
  - [...]
  - service: tts.google_translate_say
    data:
      cache: true
      entity_id: media_player.cuisine
      message: >
        Bonsoir {{last_person}} ! Tu es la dernière personne à
        rentrer à la maison. Tu peux refermer le volet.
  - [...]
mode: single

I’ve just faced the same sort of thing… wanting to save a variable that can be used across automations, etc. It seems the way to sort this is via a trigger template sensor.

Basically, you’d create something like this:

template:
- trigger:
  - platform: event
    event_type: set_last_person_home
  sensor:
  - name: last_person_home
    state: "{{ trigger.event.data.state }}"

And then in your automation, when someone gets home update its value:

action:
- alias: Update last person home
  event: set_last_person_home
  event_data:
    state: name_or_reference_to_last_person_home

You can then when you want to tell the last person to lock the door just use the value of “sensor.last_person_home”

Also possible to create a single Trigger-based Template Sensor that can store multiple values.

To store a string value (not exceeding 255 characters), an Input Text also does the job.

However, for this application, the request is for a means of sharing a value within the same automation so a script variable does the job with the least amount of effort.

I can’t believe the solution was that simple !
I had tried many other ways to set variables but I didn’t think about this one !

I have read both of your other propositions and this could come handy in the future, I’m taking notes !
Thank you both for your help :+1:

1 Like