Know when was the time the phone app has reported successfully?

Hi all,

This is something I have been struggling on for ages despite lots of searches and experiments. I have decided to finally create a post on this forum.

I am using my families phones to automate various things, including security (cameras, alarms etc) schedules.

As I am trying to build bulletproof logic around it I have 1 main issue : I can’t know when was the last time a phone has successfully reported its location to HA. There is no entity/state called “last_successful_reporting” and workarounds like looking at last_changed/last_update of their location/other sensors won’t work because they are updated only when something is changing which obviously doesn’t happen if the mobile app has an issue (happened to me a few times, for various reasons), or connectivity issues.

Am I missing something obvious? A magical entity/state? How do you guys build a non flawed logic for your home security without knowning if the devices are actually up and running? Ie: assuming a phone/person is home while its sensor has not been refreshed for a month is ‘dangerous’ …

1 Like

The greatest power of home assistant is it’s flexibility. Just set up a text of datetime helper for each phone, and have an automation for each phone which will update that variable every time the phone reports. Then you can use that in your logic - you have right at your fingertips exactly when each phone last reported.

I agree it is more elegant if the data was already buried somewhere and in many cases it may be, but maybe not.

I do something similar by recording the exact timestamp whenever a light is turned off MANUALLY (not by an automation or any other means) by a person, so I can use that datetime in another automation. Consider the use case scenario where motion in a room turns the lights on if they are not already on. If that was the only logic applied - then someone turning the lights off in a room while walking out of it - would be annoyed to discover they go back on when then walking out of the room! Therefore my motion logic includes turning the lights back on only if it had not been MANUALLY turned off within the last 5 minutes.

My answer is a little long-winded to say it is extra work to program the exact logic needed but that gives you much better control. Use helpers as variables to store that you would use in later automations.

In my case it appears that how a light was turned off (manually or by automation most recently) is not stored anywhere, but for a very short time when the person does something manually, if you have an automation triggered by that action, there is additional data available within the trigger itself. Here is the logic I use when someone manually turns off the chandelier light switch in my liging room, so you can see how I am using the trigger variables (there was an old bug in HA which has probably been long ago fixed for input_datetime helpers so at the time I wrote this I was storing the timestamp using an input_text, but you get the idea):

alias: >-
  Dining Room Chandelier Turned Off -> If Done Manually -> Set Last Manual Off
  Timestamp
description: >-
  If the dining room chandelier is turned off (manually),  then make sure the
  "last_manual_off-dining_room" timestamp is updated
trigger:
  - platform: state
    entity_id:
      - light.dining_room_chandelier
    to: "off"
condition:
  - "{{ trigger.to_state.context.id != none }}"
  - "{{ trigger.to_state.context.parent_id == none }}"
  - "{{ trigger.to_state.context.user_id == none }}"
action:
  - service: input_text.set_value
    data:
      value: "{{ as_timestamp(now()) }}"
    target:
      entity_id: input_text.last_manual_off_dining_room
mode: parallel
max: 1000

then for example when checking if it was done within the lst 5 minutes in motiuon sensor logic used so that I do not turn it back on:

Hope that helps -

You can only go by time. The mobile app integration waits for updates from the device so it’s completely dependent on that. You need to automate based on how long you didn’t receive an update for versus something to tell you the device is down. There is no easy and battery friendly way to query the mobile device. It does not have a constant connection to home.

@dshokouhi I think you are missing the point that there is apparently no way to detect in HA when phone last reported. It’s something that I can:t find a way to track reliably.

@KruseLuds proposes that I make my own client/use third party programs to ping a we hook that will upgrade a Helper datetime. That would work but it’s piling workaround over workarounds where the piece information I am looking for seems to be the most basic requirement to actually trust if someone is really home or not. All other device_tracker classes do have that implemented as you can define a timeout after which a device is not considered to be at home

I already explained to you how updates from the mobile app integration work and what you need to do to detect when that happens. The way data is reported from Mobile App is not going to change. The Last Changed data is the data you have to go by so you should be using it. That is how you tell when it last reported because the data was last changed…

I don’t really understand your complaint here

You explained exactly WHY you need to use Last Changed attribute here. The device was unable to send data back home (for whatever reason) and hence you have your “last successful report” because well if it didnt report successfully the last changed date will not have changed right? So how is that not exactly what you are looking for? Do not think of Last Changed as a workaround, the entire reason why it exists is for the very reason you are trying to automate! What other purpose do you think Last Changed data has?

Last_changed will not be updated if it has not changed. So the change could have happened 10 days ago and you would have no way to know if it’s still valid because it won’t tracking if device was seen recently.

For such data which is meant to drive security automation, not knowing when was the last refresh time from device make it totally insecure to use.

If you track teh date and time you will know that is waht we are telling you to do. You need to track it and automate it based on your needs. You know when it updates, you know when you consider it stop reporting so you need to put some effort in for your own use case. The data is there you just need to use it to your own preference.

you do know the last time it was updated, the date for the attribute.

I think you are totally missing the point : if you don’t know if the phone is still reporting (and confirming that it has really not changed place) then knowing it has changed 8 hours ago is totally useless for decision making. And again all other device_tracker classes have this info. Only the phone one doesn’t. That’s why I am asking here if I missed anything obvious as the phone device tracker makes 0 sense without this critical piece of info that all other trackers have

But if you track the date like we are telling you then you will know when this happens. There are indeed ways to track date and time and to see how long something has taken. Lets say you track the date and want 3 hours to notify if the tracker has not updated. Well if you track the date and have your automation setup to watch for it to not change for 3 hours then you can have it notify when your specified time limit is up.

It makes perfect sense the entity only gets updated when the app sends in an update, its as simple as that. There is no polling of devices done like the other device trackers you are probably using. Updates ONLY come from your phone and no where else.

You still don’t get it : even if the phone is reporting, the date will not change. I am already tracking the date and I am posting here because the date remains the same whatever the phone status. If you don’t move, leave phone a table for example the location won’t change, phone will report same location to HA which in turn will decide to not update the date because as per its definition last_change and last_update will be refresh only if state has changes and/or sub attributes.

That’s why all other trackers will change he location invalid after X missed reports/timeout delay. All trackers (like PING or ROUTER) … but the phone one which will just fail on you unless I missed an obvious setting

this is my phone (sitting on a table, no refresh for 2 hours while form HTTP logs I can tell it has reported to HA):

{
	"topic": "device_tracker.chris_phone",
	"payload": "home",
	"data": {
		"entity_id": "device_tracker.chris_phone",
		"state": "home",
		"attributes": {
			"source_type": "gps",
			"latitude": x,
			"longitude": x,
			"gps_accuracy": 13,
			"altitude": 79,
			"course": 0,
			"speed": 0,
			"vertical_accuracy": 1,
			"friendly_name": "chris_phone"
		},
		"last_changed": "2023-03-04T13:49:34.643Z",
		"last_updated": "2023-03-04T13:49:34.643Z",
		"context": {
			"parent_id": null,
			"user_id": null
		},
		"timeSinceChanged": "4 hours ago",
		"original_state": "home"
	},
}

and my wife phone, turned off just for the purpose:

{
	"topic": "device_tracker.tine_phone",
	"payload": "home",
	"data": {
		"entity_id": "device_tracker.tine_phone",
		"state": "home",
		"attributes": {
			"source_type": "gps",
			"latitude": x,
			"longitude": x,
			"gps_accuracy": 13,
			"altitude": 71,
			"course": 0,
			"speed": 0,
			"vertical_accuracy": 1,
			"friendly_name": "tine_phone"
		},
		"last_changed": "2023-03-04T13:55:22.789Z",
		"last_updated": "2023-03-04T13:55:22.789Z",
		"context": {
			"parent_id": null,
			"user_id": null
		},
		"timeSinceChanged": "4 hours ago",
		"original_state": "home"
	},
}

That may not be right or there may be a bug then because my phone has been sitting on my desk for hours and when I glance at it on the dashboard I see it tell me the last update got in 7 minutes ago. If you check HA core debug logs you’ll indeed see updates coming in and being posted, even if the device is stationary.

image

The value from this template also corresponds to the above screenshot

{{ states.device_tracker.dannys_pixel_7_pro.last_changed }} 
1 Like

It is not a bug : if GPS coordinates don’t change at all then as per docs the updated and changed fields will remain the same, this behavior is system wide. Your GPS may be more sensitive than mine, actually just moving around the device without touching it seems to create slight variations.

This is why other device_trackers will report a device as ‘not home’ if device was not seen for X seconds. With the phone tracker, it will keep saying ‘home’ forever.

Sitting here staring at all attributes and I still see the last changed date and time update when there has been no data changes at all to anything on the entity. It simply updated because it received an update from the app. It is not just GPS coordinates that it considers but all attributes that it will look at.

https://data.home-assistant.io/docs/states#database

Yes and here the attributed are for most part the coordinates. You can see the full Json structure I pasted. When sitting on a table my phone don’t seem to have any change of these attributes. I did log the request with traces and Wireshark, they just don’t change and as per the docs it will not trigger a change or update timestamp refresh

One thing we could possibly do then in this case is to supply a new attribute for the time of the location report. That should help keep it unique enough to do that then in those cases.

How would you do that? Can you set custom attributes in the client?

we would need to update the code for the android app by adding the new attribute :slight_smile:

Every location report we get has a reported time attribute that we actually use to evaluate if a location is considered new enough to send as Google will sometimes send an old report to the app (weeks old at times!).

1 Like