Need help with Home Arrival Automation

Hi all, I am using the following automation and experiencing issues.

alias: Security - Family Arrival
description: Announces arrivals and disarms alarm.
trigger:
  - platform: state
    entity_id:
      - device_tracker.ashleys_iphone
    for:
      hours: 0
      minutes: 0
      seconds: 15
    to: home
    id: ashley_tracker
  - platform: state
    entity_id:
      - device_tracker.sm_g998b
    for:
      hours: 0
      minutes: 0
      seconds: 15
    to: home
    id: andrew_tracker
  - platform: state
    entity_id:
      - group.family_all
    id: Family_Arrived
    from: not_home
    to: home
    for:
      hours: 0
      minutes: 0
      seconds: 15
condition:
  - condition: state
    entity_id: input_boolean.vacation_mode
    state: "off"
  - condition: state
    entity_id: input_boolean.guest_mode
    state: "off"
action:
  - choose:
      - conditions:
          - condition: template
            value_template: >-
              {{
              (as_timestamp(now())-as_timestamp(states.device_tracker.sm_g998b.last_changed))
              < 300 }}
          - condition: trigger
            id:
              - andrew_tracker
          - condition: state
            entity_id: binary_sensor.sm_g998b_android_auto
            state: "on"
          - condition: not
            conditions:
              - condition: state
                entity_id: group.family_all
                state: home
                for:
                  hours: 0
                  minutes: 10
                  seconds: 0
        sequence:
          - service: alarmo.disarm
            data:
              entity_id: alarm_control_panel.alarmo
              code: "0000"
          - service: script.family_arrival_tv_notification
            data: {}
          - if:
              - condition: template
                value_template: " {{ (as_timestamp(now()) - as_timestamp(states('sensor.uptime'))) // 60| round (0) < 5 }}"
            then:
              - stop: Home Assistant started/re-started too recently.
            else:
              - service: cover.open_cover
                data: {}
                enabled: true
                target:
                  entity_id: cover.garage_door_2
            enabled: true
          - wait_for_trigger:
              - platform: state
                entity_id:
                  - binary_sensor.garage_entry_door_iaszone
                from: "off"
                to: "on"
            continue_on_timeout: true
            timeout:
              hours: 0
              minutes: 3
              seconds: 0
              milliseconds: 0
          - service: tts.google_cloud
            data:
              cache: false
              entity_id: media_player.family_speakers
              message: "Welcome home, Andrew. "
          - stop: ""
      - conditions:
          - condition: template
            value_template: >-
              {{
              (as_timestamp(now())-as_timestamp(states.device_tracker.ashleys_iphone.last_changed))
              < 300 }}
          - condition: trigger
            id:
              - ashley_tracker
          - condition: not
            conditions:
              - condition: state
                entity_id: group.family_all
                state: home
                for:
                  hours: 0
                  minutes: 10
                  seconds: 0
          - condition: state
            entity_id: device_tracker.ashley_iphone_trk
            state: home
        sequence:
          - service: alarmo.disarm
            data:
              entity_id: alarm_control_panel.alarmo
              code: "0000"
          - service: script.family_arrival_tv_notification
            data: {}
          - if:
              - condition: template
                value_template: " {{ (as_timestamp(now()) - as_timestamp(states('sensor.uptime'))) // 60| round (0) < 5 }}"
            then:
              - stop: Home Assistant started/re-started too recently.
            else:
              - service: cover.open_cover
                data: {}
                enabled: true
                target:
                  entity_id: cover.garage_door_2
            enabled: true
          - wait_for_trigger:
              - platform: state
                entity_id:
                  - binary_sensor.garage_entry_door_iaszone
                from: "off"
                to: "on"
            continue_on_timeout: true
            timeout:
              hours: 0
              minutes: 3
              seconds: 0
              milliseconds: 0
          - service: tts.google_cloud
            data:
              cache: false
              entity_id: media_player.family_speakers
              message: Welcome home, Ashley.
          - stop: ""
      - conditions:
          - condition: template
            value_template: >-
              {{
              (as_timestamp(now())-as_timestamp(states.group.family_all.last_changed))
              < 300 }}
          - condition: trigger
            id:
              - Family_Arrived
          - condition: not
            conditions:
              - condition: state
                entity_id: group.family
                for:
                  hours: 0
                  minutes: 10
                  seconds: 0
                state: home
        sequence:
          - service: alarmo.disarm
            data:
              entity_id: alarm_control_panel.alarmo
              code: "0000"
          - if:
              - condition: template
                value_template: " {{ (as_timestamp(now()) - as_timestamp(states('sensor.uptime'))) // 60| round (0) < 5 }}"
            then:
              - stop: Home Assistant started/re-started too recently.
            else:
              - service: cover.open_cover
                data: {}
                enabled: true
                target:
                  entity_id: cover.garage_door_2
            enabled: true
          - wait_for_trigger:
              - platform: state
                entity_id:
                  - binary_sensor.garage_entry_door_iaszone
                from: "off"
                to: "on"
            continue_on_timeout: true
            timeout:
              hours: 0
              minutes: 3
              seconds: 0
              milliseconds: 0
          - service: tts.google_cloud
            data:
              cache: false
              entity_id: media_player.unispeak
              message: Welcome home Ashley and Andrew.
          - stop: ""
    default:
      - stop: No one home.
mode: single

I don’t know if this is the correct or most efficient way to run such an automation but basically the automation is meant to determine when me or my partner come home and disarm the alarm, open the garage door and make an announcement when we trigger a door sensor. Importantly, if we both arrive at the same time, we want this to announce both of our arrival together rather than individually or only one of us.

The issue can be either:

A) The wrong “choose” option is run.
B) The automation doesn’t choose an option at all.
C) The automation runs at unexpected times. IE: If my partners iphone wigs out and the GPS puts her in another location, it can sometimes cause the automation to think she just arrived home when she never left at all. For me, an android user, thats never happened but her iphone has caused this twice.

I setup the iphonedetect tracker because I was told this should help iphones have better tracking reliability but I dont know if this is working.

Is there a better and more reliable way to run such an automation? The biggest issue of all is the false positives when HA thinks her iphone has come home when she has been home all along.

Please help, this is driving me mad.

You might try a different approach that takes into account more factors.

I use the Bayesian integration to work out the probability of someone being home based on about a dozen “observations” (things like time of day, movement, various devices being switched on) as well as phone location. It’s extremely reliable and will compensate for tracker failures.

There’s a good howto in this post.

Thanks, I have Baysian sensors already for specific room presense but I’m not sure how to reliably do this for a family-arrival scenario. One of the issues is that there aren’t enough factors that validate the process of my partner just arriving home. Many of the sensors required to determine if someone is home is inside the house - not outside.

If this were about “Are there people home” in a more broad sense, then I would be fine doing that.

Anyone got any other ideas?

I have recently added a BLE tag to my keyring to deal with similar problems, so now I have multiple indicators that I have arrived home - tracker on phone (if it works), tracker on keys (more reliable), phone joining wi-fi and of course movement. I’ve also been thinking about a contact switch on the door, but not tried it yet.

I wonder if you could create a group or template sensor out of things like this to give a single trigger for each of you showing you were home?

This can’t work, as you have some constructional flaw. :slight_smile: Your sensors, whatever they are, will never report all at the same microsecond. This is something, that can’t happen. One of your two phones is always reporting first. That’s when your automation is triggered. So you will never have the group going “true”. And you’ll never have both phones report at the exact same time.

So it is always, one phone starts the automation, the second one starts a second run of the automation, and your group is never “true”, so it won’t work anyway.

To work around this, you will need some more devices that report, and you’d need some time in between the report of “I’m here” and a check, to see, if the other phone has arrived as well. This brings up the next question, how long is “arriving together”? A difference of what, seconds, minutes, hours?

This is not a trivial thing to solve, and you’d need to give that a lot more thought, before you will be able to solve it, sorry! :slight_smile: Might be a good idea, to rethink… :slight_smile:

EDIT: Don’t get me wrong, it can be done, others and I have something like that running, but you’ll have to give that way more thought and you’ll have to invest more time into planning it! :slight_smile:

Let us know, what you come up with! :slight_smile:

I do think you are right here. I’m just not sure what else I can do.

In terms of arrival time, im not sure i mean we’d be in the same car and I dont expect them to occur at the exact wait same second. I do expect some delay.

I’ve tried swapping trigger condtions from state change to device tracker change, tried adding wifi as a condition, etc.

But I do agree that part of the issue is one device reporting first, but the other i think is my partners iphone wandering even though we have relatively good signal.

This is, what you wrote in your first post. :wink:

If I may give a recommendation: divide these things into parts. For example, don’t handle the whole home not home process outside of specific automations. I for one have a “residence_state” where I decide, what automations and so on should run, when the “residence_state” changes. The “residence_state” depends on “people_states” (home, just arrived, not home) and will get changed, when something in the peoples status changes.

Nonetheless, you will need some “physical” breakpoints, like a door sensor, to get what you want. Let’s say, your “people_state” changes from “away” to “home”. This is, where your “residence_state” get’s updated, and the automations associated with it, run, eg. the garage door for you opens. If your wife comes home at the same time, the “people_state” will change and you can open her garage door. But to announce a welcome for both, there needs to be some physical thing, that triggers it. The idea here is, that you now have at least the time from driving in the garage up to the door opening, to check, if both are home, or only one. I hope it’s understandable where I’m going with this… :slight_smile:

And beside from all that, it will give you a much better presence detection, if you use more markers. The phone should already have two of them, wifi and BT. Add something else to the math, like the above mentioned BT-beacons on the keychain. Rely on as many receivers, as you can get. One ESPHome-BT right next to the door, one a few meters behind in the hallway. The more, the better, because you reduce your false positives dramatically. If your cars have AndroidAuto or AppleCarPlay, try running the HA companion app on the car media center, it will as well give a state to your HA instance and so on. That’s what I meant be re-thinking this. If you want to make that even more reliable, add some sensors, that check for the license plate (camera solution) or add some weight sensors (easy solution), to really know, if a car is at home.

If you then start combining these sensor values in a “person-state” that directly influences your “residence_state”, you will be a big step further! :slight_smile:

EDIT: here is a good read for presence detection, should give you even more ideas :rofl: :rofl:

Sorry just saw this now. Will read more in depth shortly.

1 Like