How to say who is home right now?

Hi.
I would like to say hello for person who enters home. So for a part of my automation I need to send tts with template in it which would say who is currently at home. That template is what I can not found how to do.

Love your help :slight_smile:

How do you figure out who’s home?
Once you have worked out who’s home, you can use something like this in automation.

  action:
  - data_template:
      entity_id: media_player.your_media_player
      message: Hello {{state_attr("person.person_name","friendly_name")}}.
    service: tts.google_say

yes, it all depends on how the automation is triggered

I believe you can figure out who triggered the automation by using {{ trigger.entity_id.attributes.friendly_name }}

Actually, I can not use automation trigger. Look, if i came home with my father, my device tracker (or his) would update first. So there can be only one trigger and one person.
Here is what I have come up with:

3 automations with 3 triggers (3 device trackers)
Example:

  alias: Hi Argo
  initial_state: 'on'
  trigger:
    platform: state
    entity_id: device_tracker.iphone_2
    to: home
  condition:
  - condition: template
    value_template: "{{ states('device_tracker.iphone_gaia_2') != 'home' }}"
  - condition: template
    value_template: "{{ states('device_tracker.iphonearm') != 'home' }}"
  action:
  - entity_id: script.15216502425310
    service: script.turn_off
  - service: script.15216502425310

Then script will wait till main door is opened. So, somebody came home. Now Home Assistant should check who is home and say hello.
So my automation will work only if there was nobody home first (seems to be right)

Any ideas?
And yes, I use gps tracker (ios app) and it knows that we are home 1 minute before we open the door

To be clear: I have no idea how to check who is at home now. That is exactly what I want to know

Take a look at the Person integration page, that should get you started in the right direction.

I define a group:

group:
  residents:
    name: Residents
    entities:
      - person.john
      - person.doe

Then I define a script that does a bunch of other stuff as well but you could do something like the following (I use pushover for notifications, you should make any adjustments to your notify service as required):

  - alias: Welcome Home
    initial_state: true
    trigger:
      - platform: state
        entity_id: group.residents
        to: 'home'
    action:
      - service: notify.all
        data_template:
          message: >-
            Welcome home
            {%- for entity in trigger.to_state.attributes.entity_id -%}
              {%- if states(entity) == 'home' -%}
                {{ ' ' + state_attr(entity, 'friendly_name') + ', ' }}
              {%- endif -%}
            {%- endfor -%}
          data:
            sound: bugle

Usually its a good idea to setup multiple device trackers and associate those with the Person integration. As an example, I have a UniFi controller at home and use that to determine when my phone is connected to my WiFi, I also use GPS coordinates sent to a MQTT server. I have everyone else in my house set up the same way.

I personally use the Google Maps HA component that sets up device trackers for both my wife and I (https://www.home-assistant.io/components/google_maps/).

I setup zones in HA (https://www.home-assistant.io/components/zone/) which tells the tracker where we are (home, work, church, away). Can also use these for proximity (https://www.home-assistant.io/components/proximity/)

zone:
  - name: home
    latitude: 32.#######
    longitude: -97.#######
    radius: 100
    icon: mdi:home
  - name: work
    latitude: 32.#######
    longitude: -97.#######
    radius: 250
    icon: mdi:briefcase
  - name: church
    latitude: 32.#######
    longitude: -97.#######
    radius: 200
    icon: mdi:church

I then setup custom sensors to track us and fire automations based on these values.

    google_maps_wife_ishome:
      friendly_name: "Wife Google Maps Home Sensor"
      value_template: >-
       {% if is_state('device_tracker.google_maps_######', 'home') %}
         home
       {% else %}
         not_home
       {% endif %}

    google_maps_me_ishome:
      friendly_name: "Me Google Maps Home Sensor"
      value_template: >-
       {% if is_state('device_tracker.google_maps_######', 'home') %}
         home
       {% else %}
         not_home
       {% endif %}

    google_maps_wife_me_ishome:
      friendly_name: "Wife & Me Google Maps Home Sensor"
      value_template: >-
        {% if is_state('device_tracker.google_maps_######', 'home') and is_state('device_tracker.google_maps_######', 'home') %}
          home
        {% elif is_state('device_tracker.google_maps_######', 'home') %}
          wife_home
        {% elif is_state('device_tracker.google_maps_######', 'home') %}
          me_home
        {% else %}
          not_home
        {% endif %}

Been pretty happy with it and definitely trigger tons of automations based upon it. I also setup similar custom sensors for _iswork, is_church for other triggers. Hope this helps!

It looks like you want to play a greeting every time someone comes home to that particular person.

To do that usiong your device trackers as listed in the example you gave then try this (incorporating the template provided by @junja430 above) :

alias: Hi
  initial_state: 'on'
  trigger:
    - platform: state
      entity_id: device_tracker.iphone_2
      to: home
    - platform: state
      entity_id: device_tracker.iphone_gaia_2
      to: home
    - platform: state
      entity_id: device_tracker.iphonearm
      to: home
  action:
    service: service.whatever_your_tts_service_is
    data_template:
      message: "Hello {{ trigger.entity_id.attributes.friendly_name }}"

Ok guys, one more time. Device tracker knows that someone is home before 1-2 minutes the door is opened. I want the speaker to say hello when the door is opened. I can not use simple automation with

{{ trigger.entity_id.attributes.friendly_name }}

When I come home with my father - I can be a trigger, or him, but we are opening the door together, so speaker should say hello to my father and me.
That is why I want to use 3 automations with 3 different triggers (me, mother, father) and after opening the door - check who is home and say hello.

I am struggling with last part where I should send tts like: welcome home {{ who is at home now}} !

Ok then try this for the message template part:

{% set people_home =  states.device_tracker|selectattr('state','eq','home') | map(attribute='attributes.friendly_name')|join(',') %}
{% if states.device_tracker | selectattr('state', 'eq', 'home') | list | count > 1 %}
  {{ people_home[1:] | replace(',' , " & ") }}
{% else %}
  {{ people_home }}
{% endif %}
1 Like

I took the liberty of doing a little nip and tuck on the template:

{% set people_home =  states.device_tracker | selectattr('state','eq','home') | map(attribute='attributes.friendly_name') | list %}
{% if people_home | count > 1 %}
  {{ people_home[1:] | join(" & ") }}
{% else %}
  {{ people_home[0] }}
{% endif %}
4 Likes

That was what I thought I need. But, as I found out now, tts is pronouncing our names terribly wrong :slight_smile:
So no I think I need something like:

If there is person_1 - play person_1.mp3
if there is person_1 and person_2 - play person_1_2.mp3
If there is person_1 and person_3 - play person_1_3.mp3

And so on. Seems to work:

'15216502425310':
  alias: Hello everyone
  sequence:
  - wait_template: "{{ is_state('binary_sensor.door_window_sensor_158d0001dcb0e3', 'on') }}"
  - service: script.turn_on
    entity_id: script.15216502425311
  - service: script.turn_on
    entity_id: script.15216502425312
  - service: script.turn_on
    entity_id: script.15216502425313
  - service: script.turn_on
    entity_id: script.15216502425314
  - service: script.turn_on
    entity_id: script.15216502425315
  - service: script.turn_on
    entity_id: script.15216502425316
  - condition: state
    entity_id: person.armen
    state: 'home'
  - condition: state
    entity_id: person.gayane
    state: 'home'
  - condition: state
    entity_id: person.argo
    state: 'home'
  - service: media_player.play_media
    data:
      entity_id: media_player.kabin
      media_content_id: ...hello_all.mp3
      media_content_type: music



'15216502425311':
  alias: Hello Argo
  sequence:
  - condition: state
    entity_id: person.argo
    state: 'home'
  - condition: template
    value_template: "{{ states('person.gayane') != 'home' }}"
  - condition: template
    value_template: "{{ states('person.armen') != 'home' }}"
  - service: media_player.play_media
    data:
      entity_id: media_player.kabin
      media_content_id: ...hello_argo.mp3
      media_content_type: music


'15216502425312':
  alias: Hello Gayane
  sequence:
  - condition: state
    entity_id: person.gayane
    state: 'home'
  - condition: template
    value_template: "{{ states('person.argo') != 'home' }}"
  - condition: template
    value_template: "{{ states('person.armen') != 'home' }}"
  - service: media_player.play_media
    data:
      entity_id: media_player.kabin
      media_content_id: ...hello_gayane.mp3
      media_content_type: music


'15216502425313':
  alias: Hello Armen
  sequence:
  - condition: state
    entity_id: person.armen
    state: 'home'
  - condition: template
    value_template: "{{ states('person.argo') != 'home' }}"
  - condition: template
    value_template: "{{ states('person.gayane') != 'home' }}"
  - service: media_player.play_media
    data:
      entity_id: media_player.kabin
      media_content_id: ...hello_armen.mp3
      media_content_type: music


'15216502425314':
  alias: Hello Armen and Gayane
  sequence:
  - condition: template
    value_template: "{{ states('person.argo') != 'home' }}"
  - condition: state
    entity_id: person.gayane
    state: 'home'
  - condition: state
    entity_id: person.armen
    state: 'home'
  - service: media_player.play_media
    data:
      entity_id: media_player.kabin
      media_content_id: ...hello_armen_and_gayane.mp3
      media_content_type: music


'15216502425315':
  alias: Hello Argo and Gayane
  sequence:
  - condition: template
    value_template: "{{ states('person.armen') != 'home' }}"
  - condition: state
    entity_id: person.gayane
    state: 'home'
  - condition: state
    entity_id: person.argo
    state: 'home'
  - service: media_player.play_media
    data:
      entity_id: media_player.kabin
      media_content_id: ...hello_argo_and_gayane.mp3
      media_content_type: music


'15216502425316':
  alias: Hello Armen and Argo
  sequence:
  - condition: template
    value_template: "{{ states('person.gayane') != 'home' }}"
  - condition: state
    entity_id: person.agro
    state: 'home'
  - condition: state
    entity_id: person.armen
    state: 'home'
  - service: media_player.play_media
    data:
      entity_id: media_player.kabin
      media_content_id: ...hello_armen_and_argo.mp3
      media_content_type: music

And 3 automations like:

- id: '1530394114232'
  alias: Hi Argo
  initial_state: 'on'
  trigger:
    platform: state
    entity_id: device_tracker.iphone_2
    to: home
  condition:
  - condition: template
    value_template: "{{ states('device_tracker.iphone_gaia_2') != 'home' }}"
  - condition: template
    value_template: "{{ states('device_tracker.iphonearm') != 'home' }}"
  action:
  - entity_id: script.15216502425310
    service: script.turn_off
  - service: script.15216502425310

Am I doing bad script? Is there a nicer way or this is ok?

There are three people whose names are:

  1. Argo
  2. Armen
  3. Gayan

That means there are 23 possible combinations of people at home. Using your approach, you will need 2 x 2 x 2 = 8 sound files and automations.

Here’s a different approach:

If the text-to-speech (TTS) engine mispronounces the names then I would suggest you supply it with an alias for each person to help it improve its pronunciation. For example, if it mispronounces Gayan then maybe it does better with Guyahn (or some other spelling of the name).

You don’t have to change the friendly_name of each device_tracker. Simply create a dictionary, within the template, containing the aliases. It will be used to replace a person’s name with their alias.

{% set alias = {"Argo":"Arrgoh", "Armen":"Arehmin", "Gayan":"Guyahn"} %}
{% set people_home = states.device_tracker | selectattr('state','eq','home') | map(attribute='attributes.friendly_name') | list %}
{% set ns = namespace(occupants = '') %}
{% for i in people_home %}
  {% set ns.occupants = ns.occupants ~ ' & ' ~ alias.get(i,i) if loop.index > 1 else alias.get(i,i) %}
{% endfor %}
{{ ns.occupants }}

Here are the results of a simulation with two people at home:

1 Like

That is nice solution. Can I change the stress? So not to be A ́rgo, but Argo ́

upd:
that worked ’ ́ ’

1 Like

Depending on which TTS engine you are using, some allow for ‘hints’ to direct the engine’s pronunciation. For example, the engine I use allows to inline XML-style hints allowing me to add pauses, adjust pronunciation, etc. When speaking a weather report, the engine preferred to pronounce wind not as the movement of air but the action of winding a clock. I inserted the appropriate hint to change the pronunciation.

hi!
maybe do you like thise:

  test_ingresso:
    alias: test ingresso
    sequence:
    - service: notify.alexa_media
      data_template:
        target: 
          - media_player.echo_living
        data:
          type: tts
        message: >-
         {%- set family = state_attr(('group.family'), 'entity_id') %}
         {%- set t_family = states.device_tracker | selectattr('entity_id', 'in', family) | list | count %}
         {%- set n_home = states.device_tracker   | selectattr('entity_id', 'in', family) | selectattr('state','eq','home') | list | count %}
         {%- set person = states.device_tracker   | selectattr('entity_id', 'in', family) | selectattr('state','eq','home') | list | map(attribute='name') |join(', ' 'e ' ) %}
         {%- macro benvenuto(person) -%}
         {% set benvenuto = [
            "Bentornato a casa " ~person~" ",
             "Indovina chi è a casa? è " ~person~" ",
             person + " è ora in casa. ",
             "Benvenuto a casa " ~person~". Ci sei mancato. ",
             "La nostra casa è ora completa, riposati e rilassati!. Bentornato " ~person~" ",
             "Ehilà!. " ~person~ " Benvenuto a casa!. ",
             "Toc toc. Chi c'è qui? è " ~person~" ",
             person~ "!. Tu sei a casa!.",
             "Conosco un segreto!. " ~person~ " è a casa!. ",
             "Sto percependo un disturbo nella forza.. " ~person~ " deve essere a casa!. ",
             "E la casa diventa una casa. Bentornato. " ~person~" ",
             person~ " è qui ora. Bentornato a casa. ",
             person~ " è ora qui. ",
             "Solo un piccolo annuncio. " ~person~ " è qui!. ",
             "Perdonate l'interruzione, ma " ~person~ " è a casa!. ",
             "I miei sistemi stanno rilevando la presenza di ulteriori umani. " ~person~ " è in casa. ",
             "Bentornato "~person~"!. La casa si sta attivando!. ",
             "Benvenuto a casa "~person~"!. È bello rivederti!. Lasciami accendere la casa. ",
             "Beh, sembra che "~person~" sia finalmente a casa!. Preparerò la casa per te. ",
             "È bello vedere che sei tornato sano e salvo "~person~"!. Vuoi che preparari la casa? ",
             "Fantastico, "~person~" è tornato!. Permettimi di preparare la casa per te. "
         ] %}
         {{ benvenuto(person) | random }}
         {%- endmacro -%}
        
         {%- macro benvenuti(person) -%}
         {% set benvenuti = [
            "Bentornati a casa " ~person~" ",
             person + " sono ora in casa. ",
             "Bentornati a casa " ~person~". Mi siete mancati. ",
             "La nostra casa è ora completa, riposatevi e rilassatevi!. Bentornati " ~person~" ",
             "Ehilà!. " ~person~ " Bentornati a casa!. ",
             "Toc toc. Chi c'è qui? è " ~person~" ",
             person~ "!. Tu sei a casa!.",
             "Conosco un segreto!. " ~person~ " sono a casa! ",
             "E la casa diventa una casa. Bentornati. " ~person~" ",
             person~ " sono ora qui. ",
             "Solo un piccolo annuncio. " ~person~ " sono arrivati! ",
             "I miei sistemi stanno rilevando la presenza di ulteriori umani. " ~person~ " sono in casa.",
              " ~person~ " sono stati identificati in casa. ",
             "Bentornati "~person~"!. La casa si sta attivando!. ",
             "Benvenuti a casa "~person~"!. È bello rivedervi!. Lasciate che accenda la casa. ",
             "Beh, sembra che "~person~" siano finalmente a casa!. Preparo la casa per voi?"
         ] %}
         {{ benvenuti | random }}
         {%- endmacro -%}

         {% if n_home == 0 %}
          Attenzione !!!
          Nessun familiare risulta in casa!!!
          
         {% elif n_home == 1 %}
          {{ benvenuto(person) }}
         {% elif  n_home > 1 and n_home < t_family %}
          {{ benvenuti(person) }}   
          
         {% elif  n_home == t_family  %}
           Wow! Che evento! Tutta la famiglia è in casa!
         {% endif %}  
2 Likes