Trigger when any entity_id device_tracker changes state to 'home'?

I recently installed Home Assistant and I’m learning the ropes with my first automation. The ultimate goal is that when any recognized/tracked device connects to our Unifi wifi, their arrival is announced through our Google Home using TTS (if a family member is home).

The automation I’ve come up with and (currently works) is as follows:

- alias: 'Name is Home'
  trigger:
    platform: state
    entity_id: device_tracker.galaxynote8, device_tracker.pixel
    from: 'not_home'
    to: 'home'
  condition:
    - condition: state
      entity_id: group.family
      state: 'home'
  action:
    - service: tts.google_say
      data_template:
        entity_id: media_player.home
        language: en
        message: > 
          {{ trigger.to_state.attributes.friendly_name }} has arrived.

Unfortunately, as it stands, I have to add each unique device_tracker manually after marking the device as tracked in known_devices.yaml. Is there a way to take this a step further and have the trigger happen on any tracked device? I assume there’s some way to do it with templates and the trigger’s entity_id, but after a few hours researching I’ve come up without a solution.

So this doesn’t become an XKCD reference here’s the solution I was able to come up with thanks to the excellent people on the Discord channel.

I changed the trigger’s entity_id to read as follows:

    entity_id: !include devices.yaml

I also created a devices.yaml file which is formatted like so:

    - device_tracker.galaxynote8
    - device_tracker.pixel

This ultimately accomplishes the simplification that I want going forward. I can just edit the devices.yaml file and any automation that includes it will update, no need to search for every device_tracker automation and change each one manually.

3 Likes

I like that approach, really smart.

Question on your condition though - as the trigger is a device changing to home, I think the condition will always be true, so it has no value. Or am I missing something here?

1 Like

Or am I missing something here?

Turns out I was missing something. You are correct, I just tested it with no one else home and the condition does trigger as true and essentially has no function.

When I think about it now, my logic makes no sense, but the thought process was that it was checking if someone was home before the trigger happened, that group.family’s status updates after the device_tracker does, not simultaneously.

Still, worse case scenario is the Google Home announces to only the cat that one of us has arrived.

:slight_smile: I’m sure the cat appreciates that

1 Like