What is the best way to get the update device tracker timestamp

I would like every time a device_tracker update a timestamp on my frontend.

So far I have:

- alias: Update time philippe smartphone
  trigger:
    platform: state
    entity_id: device_tracker.philippe_smartphone
  action:
  - service: template
      entity_id: sensor.last_update_philippe_smartphone
      value_template: '{{now().strftime("%Y%m%d-%H%M%S")}}'
- platform:
  sensors:
    last_update_philippe_smartphone:
      friendly_name: 'Last updated'

but I receive the following error:

2017-05-08 21:09:40 ERROR (Thread-1) [homeassistant.util.yaml] mapping values are not allowed here
in “/config/automation/last-updated-device-tracker.yaml”, line 7, column 16
2017-05-08 21:09:40 ERROR (MainThread) [homeassistant.bootstrap] Error loading /config/configuration.yaml: mapping values are not allowed here
in “/config/automation/last-updated-device-tracker.yaml”, line 7, column 16

Is there a different approche of doing it?

You can use only one Template Sensor for this:

sensor:
  - platform: template
    sensors:
      last_update_philippe_smartphone:
        value_template: '{{ states.device_tracker.philippe_smartphone.last_changed.strftime('%Y%m%d-%H%M%S') }}'

should work.
You can test your templates in Developer Tools/Templates.

Edit: Sorry, should be:

'{{ states.device_tracker.philippe_smartphone.last_changed.strftime("%Y%m%d-%H%M%S") }}'

Thanks, this works.

But I do get the UTC time, could this be since i’m using docker? Or do i need to change something?

Im using -v /etc/localtime:/etc/localtime:ro in my docker run script, running on Debian.

I have done the same, in the configuration.yaml i have this timezone:

time_zone: Europe/Brussels

When I test the docker date and host date, I get the same:

➜ sudo docker exec -it home-assistant /bin/bash           
root@7a8b0d5efdfd:/usr/src/app# date                      
Tue May  9 15:07:09 CEST 2017                             
root@7a8b0d5efdfd:/usr/src/app# exit                      
[x@x]-(/srv/docker/config/home-assistant)   
➜ date                                                    
Tue May  9 15:07:12 CEST 2017    

But the home assistant frontend does give an UTC time stamp.

When using the dev-templates:

{{ states.device_tracker.philippe_smartphone.last_changed.strftime('%H:%M:%S %d/%m/%Y') }}

{{ states.device_tracker.philippe_smartphone}}

{{ (states.device_tracker.philippe_smartphone.last_changed |  timestamp_utc) }}
{{ (states.device_tracker.philippe_smartphone.last_changed |  timestamp_local) }}

results in:

14:02:40 09/05/2017

<state device_tracker.philippe_smartphone=not_home; battery=74, icon=mdi:cellphone-android, gps_accuracy=18.691, latitude=0, source_type=gps, longitude=0, friendly_name=Philippe Smartphone @ 2017-05-09T16:02:40.645998+02:00>

2017-05-09 14:02:40.645998+00:00
2017-05-09 14:02:40.645998+00:00

You’re right, something strange with the times. If you search the forum there are several topics about this.
Found a workaround here, so

{{ as_timestamp(states.device_tracker.philippe_smartphone.last_changed)  | timestamp_custom("%Y%m%d-%H%M%S") }}
1 Like

Thanks this solved the problem