Overriding entity states with REST command

Hi there,

I’m trying to manually set the state of an IOS Device Tracker to “home” (with a REST command), when a device is detected with NMAP, and state is still not_home.

I expected that this state “home” would stay until iOS gave another location update, but this doesn’t seem to be the case.
After a minute the state is changed back to “away”, but from all the iOS infomation and timestamp it seems that HA didn’t receive an update from the device.

Is there any way to set the state persisting until the next real update from the device ?

More information why:
My wife likes to have her iPhone at only a few % battery and probably battery saving mode, and apparently iOS doesn’t send location updates in that case.
Or, sometimes last location update is sent close to home (with cellular antenna switch ?) but not at home itself. The nearby-but-not-home-location can persist for several hours, resulting in heating turned off etc while she is at home…
I thought I had a brilliant idea to update the state when I detected her phone on the network. Another iOS location update would overwrite this value, but actually that’s no problem. The NMAP override should only be active when I don’t have a recent location anyway.
But now it seems that this brilliant idea does not work…

For the record: trying to changing my wife’s behavior can have very unexpected results, or no results at all, so I prefer not going that route;

The problem, I believe, is that the iOS Device Tracker updates its state periodically, even if it doesn’t change. That’s causing the state to go back to not_home.

What does the device_tracker entity look like? Specifically, what attributes does it have? E.g., can you enter this in the Template Editor (adjusting the device_tracker entity_id as appropriate) and tell me what you get (and, of course, redact any sensitive information)?

{{ states.device_tracker.xxx.attributes }}

It’s possible my custom Composite Tracker Platform might help. It looks at multiple device_trackers for a given person and uses their last_seen attributes to determine which updates to use and which to ignore. But it depends on the attributes available with the iOS Device Tracker (which I’m not very familiar with.)

Hi pnbruckner,

Thanks for your answer.

This is the device_tracker entity:

{
‘source_type’: ‘gps’,
‘latitude’: xx.xxxxxxxxxxxxxxx,
‘longitude’: x.xxxxxxxxxxxxxxx,
‘gps_accuracy’: 65,
‘battery’: 95,
‘vertical_accuracy’: 10,
‘timestamp’: ‘2019-02-27T08:51:28.171+0100’,
‘course’: -1,
‘trigger’: ‘Geographic Region Exited’,
‘altitude’: xx.xxxxxxxxxxxxxx,
‘floor’: xxxxxxxxxx,
‘speed’: -1,
‘friendly_name’: ‘Mama’,
‘entity_picture’: ‘/local/img/icon-face-woman.png’
}

I took a look at your composite tracker. Sounds really nice, but I guess it will not be useable with NMAP tracker, since the smartphones toggle their wifi to save battery. That way I will have a flapping tracker, like I have now.
I guess I’ll have to define a switch that is updated by an automation using iOS and NMAP states…

That behavior is taken into account. Whenever you’re using a combination of GPS-based and non-GPS-based trackers (such as the ones you’re using), only the home states for the non-GPS-based trackers (like NMAP) are used. (I.e., the not_home states are ignored.) That way they can cause the composite tracker to change state to home, but they can’t cause it to change to not_home. It takes a new update from a GPS-based tracker for that.

So I see the tracker has a timestamp attribute. That sounds promising. Have you checked to see if this timestamp only changes when there’s a new position update, or if it changes with every entity update? If you’re not sure how to check I can certainly help. Feel free to private message me if you’d rather.

EDIT:

I wrote the following Python script to extract state changes for an entity from home-assistant.log. It only requires that the logger setting for homeassistant.core be set to debug (which should be the default.)

You use it by specifying the filename & entity_id followed by an optional list of attributes. So, e.g.:

python3 state_updates.py home-assistant.log device_tracker.mama timestamp latitude longitude gps_accuracy

You could then use the output to see if the timestamp attribute is changing for every update, or sometimes stays the same. Hopefully it will be the latter. The composite tracker could then use this to ignore state changes where the timestamp doesn’t change. (It already does this for the last_seen attribute of some other trackers like life360 and google_maps.) I would just need to make a simple change for it to also use the timestamp attribute for the iOS tracker.

You can get the script from here:

EDIT 2:

I updated the script so it now accepts multiple entity_id’s, each with its own attributes. So something like this I think would be very helpful:

python3 state_updates.py home-assistant.log device_tracker.mama timestamp device_tracker.nmap

This will show state changes for both in chronological order, and will show what the timestamp attribute is for the iOS tracker. (Of course, update the entity_id’s accordingly - I just took a guess.)