Smart arrival home sensor implementation

Hello all,

I am looking for an advise on the best way(s) to implement and arrival home sensor. Every arrival sensor I was able to find is based on “device_tracker” being detected as “home” and then based on a small timer delay an “action” is being fired.

This really does not work well as a person may take their time to actually “arrive” inside the house while already being detected as “home” by a router or other means. What I would like to have is a sensor that is being set to a temporary state of “arriving” for a small set of time. And then have an automation triggered by two or more conditions. (Say: front door has been opened and the person is “arriving” ).

To me the person really arrives, when they open the front door , not when their tracker was picked up by the router. And the issue is that once tracker is set to “home” it will remain in that state. So when a second person arrives 5 minutes later, the second person is the ONLY one arriving. The person who came 5 mins ago is alreay home… So arrival status does not apply to them any more and arriving script should not be executred for them.

So want i want is this: (sudo code)

WHEN “front door” is OPEN (state of a sensor)
AND “person-A JUST arrived” (state of another sensor)
THEN execture arrival_script
(JUST ARRIVED sensor should be reset asyncronosly by the timer)

I am thinking some sort of binary template sensor would work? But not sure how to make it rest on a timer. ?
Maybe there are other easy solutions here, which I am not aware of.

Thank you in advance for your ideas.

-D.

For the “first person home” piece - put all the device_trackers in a group, and use the status of the group. The group will be home when the first person arrives home, and will remain home as long as any member of the group is home.

Now, the way I’d suggest you tackle it is to use a wait condition. Then you can write something like:

initial_state: 'on'
alias: 'Welcome home'
trigger:
  - platform: state
    entity_id: group.all_people
    from: 'not_home'
    to: 'home'
action:
  - action: wait_template "{{ states('sensor.front_door') == 'open' }}"
    timeout: 00:05:00 
  - action: script.turn_on
    entity_id: script.welcome_home

This will run when the first person comes home, it will then wait up to 5 minutes for the sensor sensor.front_door to become open. If that happens within 5 minutes, it’ll run the script script.welcome_home, otherwise the automation will stop.

There are many other ways you could do this. You could have one that runs welcome_home when the front door is opened, and then turns itself off with:

  - service: homeassistant.turn_off
    entity_id: automation.welcome_home

Then you have another automation that when group.all_people becomes not_home turns that back on.

5 Likes

Thank you very much for your ideas! I will look into wait_template idea.

But it seems you have read my post as wanting to do “welcome script” just once for the first person arriving. it is not the case.

My actual use case is “welcome voice greeting on speaker” for each person arriving. script.welcome_home should run for each person arriving home, but not repeat for people who have previously arrived and had their greeting played to them.

If all trackers are put in the group, the state will be “home” once first person arrived.

I also would like the ability to trigger the action by the action of the “door opening” , not tracker changing state to “home”. So that greeting is played exactly at the moment of a person entering the house. But, I think you code will actually do that (play exactly on door opening), even though it is being kicked off by state change in person tracker. But, it would only work for the first person.

I guess I could have separate automations for each person in the house without puting trackers in the group… I will try your sample tonight…

Regads,
-D.

I do similar and use a input_boolean I set to on for each person in the household then they come home; and off when they leave. I also use the status of this boolean to trigger one shot automations like you want.

For example when my son comes home from school it senses is iPhone; turns on a boolean that he is home; runs a script telling him to unpack his backpack (he likes to leave his left over lunch in it…); waits 15 minutes; and then tells him again.

It’s also handy to have a boolean to check to see if someone is home for their unique room automations.

That’s easy then, you can have multiple triggers, and use templates to tell which device triggered it.

1 Like

Hi all,
Wanted to share my final solution to my question.
This is what I have done:

  1. created two hidden input_boolen switches for me and my wife dmitry_arriving and eileen_arriving (I use them to set the “arriving” state which is a temporary state that lasts x number of minutes from the moment my tracker says the person is home. I have a separate script : set_arrive_sensor which flips the input_boolean on and then later after a delay turn it off.

  2. created two template sensors which mimic the state of the above input_boolean values. Those are display in HA and they are set to “arriving” when input_boolean above is “on”

  3. created “Arriving Home” automation which has multiple triggers and template based action. Basically, it figures out “who” came home and passes an appropriate input_boolean to the script that actually does the job of turning the “arrival” state on and off

  4. Created two automations for random greeting for me and my wife. I could have created just ONE automation with template again, but I wanted to have different greetings for each person. “Dmitry Arrival Greeting” and ‘Eileen Arrival Greeting’ (If somebody knows how could I use a template here to get different greetings while using ONE automation, please let me know.

  5. Created “arrive_greetings” script which is called from “Dmitry Arrival Greeting” automation.

  6. Created “sonos_say” script, which is called from “arrive_greetings”

What I have now is that

  1. Random greeting which is different for each person, plays when a person opens’ the front door (during the period of time while their state is “arrival”

  2. Because I use “template sensor” for arrival state, I can later modify it to evaluate multiple conditions. At the moment, it just checks the input_boolean , which is trigged by device_tracker (via router). Later I can make it more sophisticated and because it is all async and done separately, it does not complicate the random greeting code.

Enjoy and hopefully this will help somebody else later. I have learned all the tricks I use here and I want to thank this community for being so great!

in input_boolean.yaml

 dmitry_arriving:
    name: Dmitry Arriving
    initial: off
    icon: mdi:account-key

  eileen_arriving:
    name: Eileen Arriving
    initial: off
    icon: mdi:account-key

in template.yaml (my template sensors)

- platform: template
  sensors:

    dmitry_arriving:
        friendly_name: 'Dmitry Arriving'
        value_template: >-
          {%- if is_state('input_boolean.dmitry_arriving','on') -%}
            arriving
          {%- else -%}
            N/A
          {%- endif -%}

    eileen_arriving:
        friendly_name: 'Eileen Arriving'
        value_template: >-
          {%- if is_state('input_boolean.eileen_arriving','on') -%}
            arriving
          {%- else -%}
            N/A
          {%- endif -%}

in customize.yaml

input_boolean.dmitry_arriving:
  hidden: true
  homebridge_hidden: true

input_boolean.eileen_arriving:
  hidden: true
  homebridge_hidden: true

in automation.yaml

- alias: 'Arriving Home'
  trigger:
    - platform: state
      entity_id:
         - device_tracker.dmitry
         - device_tracker.eileen
      from: 'not_home'
      to: 'home'
  action:
     service: script.set_arrive_sensor
     data_template:
        tracker: >
           {% set person = trigger.entity_id.split('.')[1] %}
           {{  'input_boolean.' +  person + '_arriving' }}


- alias: 'Dmitry Arrival Greeting'
  trigger:
    platform: state
    entity_id: sensor.living_room_door_entry
    to: 'Open'
  condition:
    - condition: state
      entity_id: sensor.dmitry_arriving
      state: 'arriving'
  action:
     service: script.arrive_greetings
     data_template:
        speech_message: >
           {% set person = 'Dimitriy' %}
           {{ [
           "Welcome back home " + person ,
           "The king is back. Welcome " + person,
           "Are you drunk? " + person,
           "Why took so long daddy? Woof!",
           "There he is.." + person,
           person + "Life is like a song, you’re back where you belong.",
           "Welcome Home!" + person
           ] | random }}

In scripts.yaml

set_arrive_sensor:
  alias: "Set status of Arrive sensor"
  sequence:
   - service: input_boolean.turn_on
     data_template:
       entity_id: {{ tracker}}
   - delay:
         minutes: 3
   - service: input_boolean.turn_off
     data_template:
        entity_id: {{ tracker}}


arrive_greetings:
  alias: "Greetings on Arrival Home"
  sequence:
   - service: script.sonos_say
     data_template:
       sonos_entity: media_player.living_room
       volume: 0.4
       message: "{{ speech_message }}"
       delay: '00:00:08'
sonos_say:
  alias: "Sonos TTS script"
  sequence:
   - service: media_player.sonos_snapshot
     data_template:
       entity_id: "{{ sonos_entity }}"
   - service: media_player.sonos_unjoin
     data_template:
       entity_id: "{{ sonos_entity }}"
   - service: media_player.volume_set
     data_template:
       entity_id: "{{ sonos_entity }}"
       volume_level: "{{ volume }}"
   - service: tts.yandextts_say
   #- service: tts.google_say
     data_template:
       entity_id: "{{ sonos_entity }}"
       message: "{{ message }}"
   - delay: "{{ delay }}"
   - service: media_player.sonos_restore
     data_template:
       entity_id: "{{ sonos_entity }}"
3 Likes

For a similar situation I am using this automation. When I am detected to be home with the nmap device tracker, I am given a 1 minute grace period. If the door is opened in that time I am greeted with a random message (thanks for the random messages by the way).

- id: auto30
  alias: Welcome Home
  hide_entity: false
  trigger:
    platform: state
    entity_id: device_tracker.sendorm
    from: 'not_home'
    to: 'home'
  action:
  - wait_template: "{{ states.binary_sensor.door_window_sensor_158xxxxxxxx.state == 'on' }}"
    timeout: 00:01:00
  - delay: 00:00:01 
  - service: media_player.volume_set
    data:
      entity_id: media_player.home
      volume_level: "0.4"    
  - service: tts.yandextts_say
    data_template:
      entity_id: media_player.home
      message: >
         {% set person = 'Sendorm' %}
         {{ [
         "Hi" + person + ", welcome",
         "Hello" + person,
         "Greetings" + person
         ] | random }}
  - service: media_player.volume_set
    data:
      entity_id: media_player.home
      volume_level: "1"
3 Likes

wait template! Didn’t know about these thanks.

I also saw it in the forum. The guy who wrote it, did like it was nothing. Actually it was a game changer for me :smile:

fer sure! I’m gonna see how else I can use this! Right now I’m gonna use it to locate my vacuum after I walk in the front door. Was trying to figure out the best way to do this and this is definitely it. Before I just had it on a 1 minute delay upon arriving home, not very smart haha

Hi all, this is very useful.

Would I be able to change the “group.all_people” to just one tracker, replicate this for me and my partner and then it will work when either of us arrive home?

    initial_state: 'on'
    alias: 'Welcome home'
    trigger:
      - platform: state
        entity_id: group.all_people
        from: 'not_home'
        to: 'home'
    action:
      - action: wait_template "{{ states('sensor.front_door') == 'open' }}"
        timeout: 00:05:00 
      - action: script.turn_on
        entity_id: script.welcome_home

Also I am quite new to HA, would I place this in the automation file?

yes and yes

1 Like

Stumbled across this thread searching and had to reply here – my son does exactly the same thing and your post gave me an idea on how to make sure he takes it out of his backpack :slight_smile:

Looks awsome!
But since im not that good at programeing I think I need a little help with changeing the script from Sonos to Chromecast…

Thank you in advance for your help.

HI. I tried your code but I get error on line:

entity_id: {{ tracker}}

Can you post your updated code?

Try changing the error line with:

       entity_id: >
         {{ tracker }}