Device tracker last change

Is there an easy way to access (programmaticly) at what time a device tracker sensor has changed status?
I know I can set an event and write somewhere when status change, but I hope there is a sort of attribute. Thx!

What type of tracker are you using? If you are using the TrackR devices, you might want to look at the last seen attribute

device_tracker.sam	home	
last_updated: Tue Jan 24 23:26:37 UTC 2017
gps_accuracy: 0
battery_level: -1
latitude: xx.xxxxx
trackr_id: aaaaaaaa-bbbbbbbbbb
id: yyyyyyyyyyyyyyyyyyyyyyy
last_seen: Tue Feb 14 15:02:22 UTC 2017
source_type: gps
friendly_name: sam
longitude: -xx.xxxxxxxxx
lost: false


go to the dev-template and throw this in:

{{ states.device_tracker.pixel.attributes }}

update the tracker name.

This will show the available attributes. Then you can append the “last_seen” attribute (if it exists) to the end, like so:

{{ states.device_tracker.pixel.attributes.last_seen }}

1 Like

thx, but only “friendly_name” and “source_type” are available. So I guess I have to find a workaround.

LastScanTIme would be a nice enhancement to the nmap component though.

Not only lastScanTime but also LastChangeStateTime.

Try this, it should give you the time in seconds from how long ago it has changed. Depending on your use case, you may need to do some manipulation of the output to have it return an actual time. I haven’t used this on a device tracker but I have on other entities.

{{ (as_timestamp (now()) -as_timestamp (states.device_tracker.pixel.last_updated)) }}

Wonderful, it is the solution! Thx :smiley:

heya, i know this is an old topic, but i’ve been using this but noticed it doesn’t work for all known devices…

i’m using nmap, but only like half of my devices will give a result, the others return an error that they don’ have the parameter last_updated.

is there a way to fix this?

I am going to jump in here and resurrect this. Ok so seems the presence detection attributes reset on every restart of Hass. Which I understand why. The way i ended up using it was a combination of custom ui card and then implementing momentjs for a easy way to show last seen (when they leave) and associated time (when they arrive). No matter what means of presence detection you have these attributes are present.

I am working on getting my github fully live with all my customizations but here is a quick howto to get it up and running so you get something like this.


  • add the section comment device tracker to your customize_blob:
  • copy all files in www/custom-ui
  • add the customizer section and frontend sections to your configuration.yaml file

I’m not a web developer, so I can’t really figure out how you’re doing this by looking at your code. Can you briefly describe how you’re determining when the last actual change was to a device_tracker entity? Thx!

Yep i am getting it from assoc_time attribute and last_seen attribute depneding on what state the device_tracker is in.

  extra_data_template: >
    ${ entity.state == 'not_home' ? (attributes.last_seen > 0 ? moment.unix(attributes.last_seen).fromNow() : '' ) : (attributes.assoc_time > 0 ? moment.unix(attributes.assoc_time).fromNow() : '' ) }

So to break it down

if entity.state == ‘not_home’ and attributes.last_seen > 0 fomat it to a unix timestamp and display it in a friendly manner which is what momentjs does moment.unix(attributes.last_seen).fromNow()

if the entity.state is home read the assoc_time meaning when it was considered home and display it in a friendly format with momentjs moment.unix(attributes.assoc_time).fromNow()

Hope that helps

What type of device_tracker are you using that has these attributes?

I am using unifi but these attributes are present will all device trackers. keep in mind if you restart HASS it wipes out all the values. So in order to truly see the attributes you may have to have your device go from home to not home to then see values.

By attributes, do you mean attributes of the entity’s state object (as in If so, then no, they’re not. They certainly aren’t on mine, but then again, mine is from a custom platform I wrote. Is it possible these are new since 0.69.1 (which is the version I’m still on)? FWIW, I grepped all the homeassistant source, and I don’t see assoc_time anywhere, and last_seen only seems to be an attribute defined by device_tracker/ and device_tracker/, and maybe device_tracker/

Not sure on versions i am pretty new to HASS however if you wrote it why not just add them in to the system and make the persistent ? nmap has em too.

Well, I could, but I’m really trying to understand where your code is able to get this information from, and why you say all device trackers have these attributes, especially since I don’t see that in the (backend) code. Like I said, I’m not a web developer so I probably can’t follow your (frontend) code. Is it possible this information is derived by the web server or code that runs in the web browser from other information?

Case in point. I’m looking here and here, and I see no evidence of either last_seen or assoc_time. (I do see last_seen in device_tracker/, but it’s not exposed as a state attribute.)

Even with that thought process unifi

Only references that attribute, no where does it set it. Same with assoc_time its not even in Unless i am missing something.

Im not wasting a ton of time but what i can see is last_seen is set by main device tracker initialization. But if your writing your own custom device tracker why dicker and just set it with your custom code?