Get previous zone for device tracker when not trigger - possible?

I’m finding the zone detection - entering/leaving - is really unreliable on iPhones. I’ve checked all the setting are correct (always use location, background activity enabled, etc) but despite this my automations to to track when people (devices) enter or leave a zone never seem to work. Searching the web it seems many others have issues with this too.

So I’ve decided to take an alternative approach. My automation is triggered every 3 minutes and checks if the state of a device tracker changed within the last 3 minutes. If it did, I want to notify that someone has arrived at a zone (if the state is a known zone) or left a zone (if the state is “Away” and they’re travelling between zones).

The automation trigger and conditions are set as follows:

  - platform: time_pattern
    minutes: /3
  - alias: Check if location changed in last 3 minutes
    condition: template
    value_template: >-
      {{ ((as_timestamp(now()) -
      as_timestamp(states.device_tracker.xyz_iphone.last_changed)) / 60) |
      round(0) =< 3 }}
  - alias: Check location is not Home
    condition: template
    value_template: "{{ states('device_tracker.xyz_iphone') != 'Home' }}"

Seems pretty simple to get the zone friendly name if they ENTER a zone. But if they LEAVE a zone I need to get the previous value (i.e. the zone they were in).

I can see there’s a “from_state” keyword but it seems this only applies to triggers and in this instance the device tracker isn’t the trigger (because of the issues around it not triggering!).

So is there a way to get the previous state / zone for a device tracker?

I think I’ve cracked this so thought I’d post my solution here in case others find it useful…

I created a trigger sensor as follows:

- trigger:
    - platform: event
      event_type: set_xyz_location
    - name: xyz_location
      state: "{{ states('device_tracker.xyz_iphone') }}"

And then in the automation I show above I set the action to notify me, using this sensor value if the device tracker state is “not_home” otherwise they’ve ARRIVED somewhere and it can use the actual device tracker state. After notifying me it then updates the value of the trigger sensor.

  - service: notify.my_mobile_app
      message: "{% if (states('device_tracker.xyz_iphone')=='not_home') %}
        XYZ has left {{kb_location}} {% else %} XYZ has arrived at {{ state_attr('zone.'
        + states('device_tracker.xyz_iphone'), 'friendly_name') }} {% endif
  - alias: Update location
    event: set_xyz_location
    event_data: {}

Fingers crossed, its more reliable than the iPhone zone tracking!

I’m not sure that will work as it’s still relying on the iPhone Companion App to report the devices GPS location, and I don’t think you can force that update to happen from within HA.

I’m using Node Red to track “people” as they enter/leave zones, and the device trackers assigned to those people are what determines their locations. The trigger in Node Red is any change in the status of the ‘person’, and the zone they left is and the zone they entered is All of my iPhone zone changes are reported within about 30 seconds of the zone change.

One thing you can do to improve location data is to use more than one device tracker per person. The HA Companion App includes the default (i.e. device_tracker.nickp_iphone), but you can add others. I’ve added the iPhone Device Tracker from HACS, which detects if the phones are connected to your WiFi network to know if they are home or away. There are other Integrations that report the WiFi connection based on the router being used, such as Netgear, Unifi, etc.

Those won’t help with any zones other than Home though. For other zones you’d still need an app on the phone that can report GPS coordinates, such as Life360 or OwnTracks.

Yep - you’re right! I initially thought it was still reporting the zone the iPhone user was in but just not triggering the automation - hence the approach to poll on a regular basis. However it seems the iPhone privacy settings just stop this working without regularly re-enabling it or responding to the “HA is tracking you” prompt.

Never mind, partner is upgrading phone and has opted to switch to an Android phone so hopefully that will sort things as my (Android) phone seems to work just fine.