Use AI Task Data to greet someone

I get how to create the basic automation but what I want to do is use it to greet whoever opens the door. This is easy for one entity but if there are 2 who open the door I don’t see how to do it. Tried creating a sensor that had both device trackers in it but this would immediately break if only 1 person comes home.

I’ve tried finding info but it seems so scarce and spread out I would have thought someone would have done this before

When a zone changes it updates state to count the person objects in zone. That person object should also switch to home from not_home (state change)

You can look back at the person object(s) state collect them on a template and sort by last changed and see if it/they changed within the last X minutes and make an assumption on person and announce.

I have a group that is updated on the fly as people enter the house. I then use a bit of Jinja to extract the names of the people who have come home and use those in my welcome greeting.

So the automation runs this action:

- action: group.set
  data:
    object_id: arriving
    add_entities: "{{ trigger.to_state.entity_id }}"        

Then:

message: >
   {% set arriving = expand('group.arriving') %} {% set people =
      arriving | map(attribute='name') | join(' and ') %} {% set count
      = arriving | count %}

   {% if count == 1 %}
   {% set is_are = 'is' %}
   {% set has_have = 'has' %}
   {% else %}
   {% set is_are = 'are' %}
   {% set has_have = 'have' %}
   {% endif %}

   {%- macro greeting_sentence(person, is_are, has_have) -%}
     {{ [ person ~ ' ' ~ has_have ~ ' arrived.',
          'My sensors are picking up the presence of additional humans — ' ~ person ~ ' ' ~ has_have ~ ' been identified as home.',
          'Guess who is in the house? ' ~ person ~ ' ' ~ is_are ~ '!',
          'I sense a disturbance in the Force — ' ~ person ~ ' must be back!',
           person ~ ' ' ~ is_are ~ ' now in the house.',
           person ~ ' ' ~ is_are ~ ' now here. Welcome home!',
          'Just a quick announcement: ' ~ person ~ ' ' ~ has_have ~ ' arrived!',
           person ~ ' ' ~ has_have ~ ' finally made it home.'
         ] | random }}
    {%- endmacro -%}

      {{ greeting_sentence(people, is_are, has_have) }}

Wait a couple of minutes then clear the group with

action: group.set
data:
  object_id: arriving
  entities: []        

Someone had just asked a similar question:

Slightly altered to remove a condition for the other poster:

alias: Welcome message
description: ""
triggers:
  - trigger: state
    entity_id: zone.home
    not_from:
      - unavailable
      - unknown
    not_to:
      - unavailable
      - unknown
    variables:
      arrived: |
        {{ difference(trigger.to_state.attributes.persons,
                      trigger.from_state.attributes.persons)
         | map('replace','person.jack_jones','Hi Jack')
         | map('replace','person.jill_jones','Welcome Jill')
         | join(', ') }}
conditions:
  - condition: template
    value_template: |
      {{ arrived != '' }} 
actions:
  - action: tts.speak
    metadata: {}
    data:
      cache: true
      media_player_entity_id: media_player.frontdoor
      message: "{{ arrived }}. Nice to see you!"
mode: single

Note that two people arriving might not be seen coming home at exactly the same time

For leaving, swap to_state and from_state.

PS. Using AI for such a simple task burns tons of energy, heating the earth for no good use.

1 Like

That’s one thing I’d need. Me and the wife usually come home at the same time

1 Like