Router device tracking with timestamp

Is there a way to get a timestamp for tracked devices?

Example:
Device1 away since 08.00 a.m
Device2 at home since 7.00 p.a.

why not make a mqtt device tracker and update this state using node red?

do you have an example for this case?

You can access the last_changed on most things this way:

{{states.device_tracker.xxx.last_changed}}

I tested this in the developer/template panel. You can then add some more formatting around it to put in the format you want. For example:

Xxx has been {{states('device_tracker.xxx')}} since {{states.device_tracker.xxx.last_changed.strftime("%b %d, %Y at %I:%m %p")}}

results in:

Xxx has been home since Jan 11, 2021 at 08:01 PM

I included the date because, well, during these times we find we don’t leave the house all that often…

If you want to have these be a virtual sensor state, then you can create a sensor template with this as the value. Then you can add the sensor to your front end.

EDIT: Just realized the last changed is in UTC time, which probably isn’t what you are looking for. Change that portion to:

{{as_timestamp(states.device_tracker.xxx.last_changed) | timestamp_custom("%b %d, %Y at %I:%m %p")}}

as_timestamp puts converts the timestamp to local time.

For completeness, the sensor will be:

- platform: template
  sensors:
    tracker_xxx_last_changed:  <--- name this how you want it
      value_template: >
         KP115-2 has been {{states('device_tracker.xxx')}} since {{as_timestamp(states.device_tracker.xxx.last_changed) | timestamp_custom("%b %d, %Y at %I:%m %p")}}

The sensor state then looks like the output listed above

wow, I used in sensor.yaml:

- platform: template
   sensors:
   tracker_maxi_last_changed:
     value_template: >
        maxi has been {{states('device_tracker.maxi')}} since {{as_timestamp(states.device_tracker.maxi.last_changed) | timestamp_custom("%b %d, %Y at %I:%m %p")}}
    
     

but I got


Invalid config for [sensor.template]: expected dictionary for dictionary value @ data['sensors']. Got None
extra keys not allowed @ data['tracker_maxi_last_changed']. Got OrderedDict([('value_template', 'maxi has been {{states(\'device_tracker.maxi\')}} since {{as_timestamp(states.device_tracker.maxi.last_changed) | timestamp_custom("%b %d, %Y at %I:%m %p")}}\n')]). (See ?, line ?).

Your yaml syntax is wrong. You have given space in front of sensors in the second line. Try without that like this.

- platform: template
  sensors:
    tracker_xxx_last_changed:
     value_template: >
        maxi has been {{states('device_tracker.maxi')}} since {{as_timestamp(states.device_tracker.maxi.last_changed) | timestamp_custom("%b %d, %Y at %I:%m %p")}}

Yep - yaml is ultra sensitive to spacing. The standard indent is 2 spaces.

Looks the line starting with value_template needs to move to the right one more space.

@nuki47 - copy what I listed exactly and change the names to yours being careful to not change the spacing. Yaml spacing is unforgiving. I have this working on my system.