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.