GPS enabled device tracker is coming home tracking

All my family members phones are being tracked using Google Maps Location Sharing. However, there is no way to tell when they are on the way home. To overcome this, I created this package that will “guess” when any of us are heading home.

The concept is to track the distance between the device current GPS location and my home. This distance calculation is straight line calculation, not the actual “on the road” distance. If the sensor updates show the distance is getting nearer to home consecutively 3 times within a set duration, then it will confidently guess the device is on its way home.

From there, I can do any automation I want such as turning on the A/C (if it’s too hot) before I arrive home.

Here is the package:

##########################################################
# Distance Sensor
# Formula taken from https://www.researchgate.net/post/What_is_the_best_formula_in_finding_the_distances_between_one_coordinate_in_earth_to_another_given_the_longitude_and_latitude
##########################################################
sensor:
  - platform: template
    sensors:
      ben_to_home_distance:
        friendly_name: 'Ben to Home Distance'
        value_template: >-
          {%- set lat1 = state_attr("zone.home","latitude") -%}
          {%- set lon1 = state_attr("zone.home","longitude") -%}
          {%- set lat2 = state_attr("device_tracker.google_maps_123456789012345678901","latitude") -%}
          {%- set lon2 = state_attr("device_tracker.google_maps_123456789012345678901","longitude") -%}
          
          {%- set distance = sqrt((lat2-lat1)**2+(lon2-lon1)**2)*111.32 -%}
          
          {{- distance|round(1) -}}

        unit_of_measurement: 'km'
        entity_id: device_tracker.google_maps_123456789012345678901



##########################################################
# Timer to reset the counter
##########################################################
timer:
  ben_to_home_timer:
    name: Ben to Home Timer
    duration: '00:05:00'



##########################################################
# Counter
##########################################################
counter:
  ben_to_home_counter:
    name: Ben to Home Counter
    initial: 0
    step: 1



##########################################################
# Automations
##########################################################
automation:
  - alias: 'Ben to Home'
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: sensor.ben_to_home_distance
    condition:
      - condition: template
        value_template: "{{ trigger.to_state.state|float < trigger.from_state.state|float }}"

      # this is to limit the automation range to 8km
      - condition: template
        value_template: "{{ trigger.to_state.state|float < 8 }}"
    action:
      - service: counter.increment
        entity_id: counter.ben_to_home_counter
      - service: timer.cancel
        entity_id: timer.ben_to_home_timer
      - service: timer.start
        entity_id: timer.ben_to_home_timer
      
  - alias: 'Timer Finish, Reset Ben to Home Counter'
    initial_state: 'on'
    trigger:
      - platform: event
        event_type: timer.finished
        event_data:
          entity_id: timer.ben_to_home_timer
    action:
      - service: counter.reset
        entity_id: counter.ben_to_home_counter
      
  - alias: 'Distance Increase, Reset Ben to Home Counter'
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: sensor.ben_to_home_distance
    condition:
      - condition: template
        value_template: "{{ trigger.to_state.state > trigger.from_state.state }}"
    action:
      - service: timer.cancel
        entity_id: timer.ben_to_home_timer
      - service: counter.reset
        entity_id: counter.ben_to_home_counter
      
  - alias: 'Activate Ben is Coming Home'
    initial_state: 'on'
    trigger:
      - platform: numeric_state
        entity_id: counter.ben_to_home_counter
        above: 2
    action:
      # Modify this for your own automation
      - service: notify.telegram
        data_template:
          message: "Ben is coming home."
          title: ""
      - condition: state
        entity_id: group.occupants
        state: 'not_home'
      - service: script.comfort_zone

You can customize this for other purposes such as notify your wife/kid automatically when you are on your way to meet them. It’s up to your creativity!

Any suggestion to improve it will be greatly appreciated. Thank you.

2 Likes

Thanks for sharing your project, good stuff!

The only downside I can see so far is that you need to repeat ALL that for EVERY person in your household.
So apparently creating a custom component is the way to go, but I’m not that advanced in that department yet so cannot tell more… :wink:

that’s right. I have to duplicate this for every person in my home. but that’s ok because it’s still manageable for 3 to 4 person.

Yes it is, but it would be MUCH easier AND foolproof to have that all hidden somewhere as if it requires any changes along the road (because of HA/Google/your requirements), it it would be a serious pita…
Let’s hope there is a way to do that.

1 Like

Have you checked the proximity component? I’ve been using it with Owntracks and it’s pretty solid.

2 Likes

Proximity component? What proximity component??? :astonished:

Argh… I spent whole day coming up with this. How could I not realized the existence of such component after so long??? :sweat_smile:

2 Likes

I didn’t know either this proximity component , and it was released in 0.13 (!)
Anyway @masterkenobi thanks for sharing; look it like this: without your post I wouldn’t have found that component :smile:

1 Like

With the proximity component, everything is so much simpler now. Here is my updated automation…

##########################################################
# Proximity
##########################################################
proximity:
  ben_to_home_distance:
    zone: home
    devices:
      - device_tracker.google_maps_123456789012345678901
    tolerance: 50
    unit_of_measurement: km


##########################################################
# Sensor
##########################################################
sensor:
  - platform: template
    sensors:
      ben_to_home_dir_of_travel:
        friendly_name: 'Ben to Home Dir of Travel'
        value_template: '{{ state_attr("proximity.ben_to_home_distance","dir_of_travel") }}'
        entity_id: proximity.ben_to_home_distance


##########################################################
# Automation
##########################################################
automation:
  - alias: 'Ben to Home'
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: sensor.ben_to_home_dir_of_travel
        to: 'towards'
        for: '00:10:00'
    condition:
      - condition: numeric_state
        entity_id: proximity.ben_to_home_distance
        below: 8
    action:
      # Modify this for your own automation
      - service: notify.telegram
        data_template:
          message: "Ben is coming home."
          title: ""
      - condition: state
        entity_id: group.occupants
        state: 'not_home'
      - service: script.comfort_zone
4 Likes

This sounds like something I would do… Spend a day coming up with something I thought was really cool only to find there’s a component that already does it. I feel for you! But hey, think how much you learned along the way!! :+1::+1:

Based on this post, I, too, just found the proximity component. That looks really nice! Implementing it now.

1 Like

Have you updated this to accommodate all four people in your home Ben? Curious if you did how you configured it. I thought I could create a single proximity sensor and put all the device_trackers in it but doesn’t seem to work the way I expected.

Thanks.

JR

I just duplicates the package for each person.

Gotcha. I was looking more for something that based on proximity would trigger something the first time someone got close to the location. So if any of the device trackers were going home and were a mile from home ]let me know. Just not sure how to combine multiple device_tracker’s as a single one. If I add multiple trackers to the list of devices under the proximity: it always says zero miles.

JR

I believe this will help you out! I use it and it works great! Combine your trackers with this then use the resulting tracker in your proximity sensor.

it may calm you after that long time that I didn’t know anything about this component either, until now! I didn’t build an automation but spent a whole day planning how i would, with zones and math and all that :crazy_face:

Thank you for posting back then. :+1: