Google Maps location sharing when mobile phone is turned off

Hello community,

that´s my first post in this forum, so I first want to thank all the awesome people who work active on that project. I´ve set up Home Assistant two month ago and my environment continues to grow thanks to you. I´ve try to research in the www, especially here but didn´t find a solution for my issue yet. I´ve set up Google Maps location successfully with my smartphone and tracks my position very reliable leaving home / work etc. But because I want to use it as a presence tracker to set the status of alarm_control_panel.home_alarm to disarm / armed_home / armed_away it would be very helpful, if Google Map tracker would set my presence status to “not_home” after turning off my mobile phone. But when I turn it off (f.e. when I go to sleep) I´m still at home in Home Assistant. Yes it´s truth, I am still at home, but I want arm the alarm anyhow.

I assume it´s not an issue of the tracking itself, it´s more about to tell HA to arm the alarm if GPS status is not changing for a defined time

Does anyone has a clue how realize that?

Regards
Martin

Yes, in general, GPS-based device_trackers only change when they get updates. If the device is off then there aren’t any updates, so the device_tracker entity won’t change.

But, google_maps device_trackers do have a last_seen attribute (which indicates when the last update came from the device.) You could use that in an automation trigger, or maybe create a binary_sensor from it that indicates whether the device is “on” (that you then use in an automation trigger, etc.). E.g.:

binary_sensor:
  - platform: template
    sensors:
      phone_xxx:
        value_template: >
          {{ as_timestamp(states('sensor.date__time').replace(',','')) -
             as_timestamp(state_attr('device_tracker.google_maps_xxx', 'last_seen'))|float
             < 3600 }}

binary_sensor.phone_xxx_off will be ‘on’ whenever the last update is less than one hour old, and will change to ‘off’ whenever the last update is an hour or older.

The threshold time you would use depends on the behavior of your device. I have found that it is not uncommon in some circumstances for there to be no updates for quite some time (half hour, hour, more, …) even when the phone is on. Depends on a lot of factors.

To be honest I already thought about using the last_seen attribute in that case but I didn´t (and still don´t) know a way to use it in a value to work further with because I´m not very familiar with Python and programming in general :slight_smile:

i know that your example is not a complete code ready to use, but anyhow I need to start. I´ve tried your code with my correct entity and get the following message:

TypeError: unsupported operand type(s) for -: ‘NoneType’ and ‘float’

Maybe there is something wrong with the data type? If I understand you correctly you want to calculate the current timestamp - the value of the content of last seen attribute and if it´s higer than X seconds, set the status to off.Right?

I´m wondering how to get the current time stamp, the sensor sensor.date__time is not available in my home assistant.

To enable thistime sensor in your installation, add the following to your configuration.yaml file:

# Example configuration.yaml entry
sensor:
  - platform: time_date
    display_options:
      - 'time'
      - 'date'
      - 'date_time'
      - 'time_date'

Each configuration option will create a sensor - 'time' will be sensor.time etc.

Yep, as @anilet points out above. Also, I believe in new versions the name of the sensor might be sensor.date_time (i.e., one underscore as opposed to two underscores as it was in older versions like I’m still running.)

After I created the sensor @anilet mentioned I got each sensor for time, date etc.Then I replaced sensor.date__time with sensor.date_time in your code and that did trick for me. I changed 3600 to 360 seconds and set my mobile phone to airplane mode for 10 minutes for testing. The state of phone_xxx changed from true to false. With that I can continue to set up the automation trigger. Thanks a lot to both of you for your assistance and pushing me intro the right direction.

1 Like