Hello
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