Xiaomi door/window sensor last opened time / Philips HUE motion sensor

last_updated and last_changed are not attributes. The are part of the state object, so the code to access them in your case is: states.sensor.kitchen_motion_sensor.last_updated or states.sensor.kitchen_motion_sensor.last_changed

kitchen_movement:
  friendly_name: 'Last movement in kitchen'
  value_template: "{{ states.sensor.kitchen_motion_sensor.last_updated.strftime('%d/%m/%Y %H:%M:%S') }}"
1 Like

Well that do work. I was confused since in the exmple they used the attribute last_updated.

Here: https://github.com/robmarkcole/Hue-sensors-HASS

But I guess since it does show in the frontend now and I can format the time it is OK. So Thank you.

Still having problems with the time zone because it shows UTC time and I have configured the time zone in configuration.yaml but it does nothing to these timestamps. Do I have to somehow manually add the hours to the sensors last_updated value?

ah for local timestamp:

kitchen_movement:
  friendly_name: 'Last movement in kitchen'
  value_template: "{{ as_timeastamp(states.sensor.kitchen_motion_sensor.last_updated) | timestamp_custom('%d/%m/%Y %H:%M:%S') }}"
1 Like

Thats it, thank you :slight_smile:

Actually that did not work. For some reason the motion sensors states change about every 5 minutes or so and so does the data in the frontend.

When looking at the last_updated field from ā€œStatesā€ it shows the correct timestamp.

So I should use the last_updated from sensors states but need somehow format the timestamp. Any ideas how I could do that?

states.sensor.kitchen_motion_sensor.last_updated is the last_updated from the sensors stateā€¦

1 Like

can you screenshot the 2 differences please?

1 Like

Here is what is seen in frontend and in States menu:

difference3

The frontend is in correct timezone and other one is UTC but minutes are wrong and frontend keeps changing every 5 minute or so. And if I reboot the Hass.io it will show the boot time as last_updated and not the actual last_update that is in States menu.

and here is the code

kitchen_movement:
  friendly_name: 'Last movement in kitchen'
  value_template: "{{ as_timestamp(states.sensor.kitchen_motion_sensor.last_updated) | timestamp_custom('%d.%m %H:%M') }}"

Are your sure, I think itā€™s the other way around.

1 Like

I think I solved it by changing the timestamp that HA receives. There was ā€˜Tā€™ in the middle of date and time and it was (python) split there but I just replaced ā€˜Tā€™ with ā€˜spaceā€™ so HA sees it as correct timestamp. And if I use ā€¦attributes.last_updated it shows the correct timestamp.

I will monitor situation :slight_smile:

EDIT: so the last_updated was like this:
2018-07-23, 09:55:01

and now it is
2018-07-23 09:55:01

EDIT2: Now it seems to be working but the time zone problem is here again and it is showing utc :confused:

This will never go away. Every time home assistant or hassio is restarted, the last_updated and last_changed objects are set to the time in which the system restarted. This cannot be avoided. You could use the SQL sensor to grab the information possibly.

You need to use the template editor to see the status. You are looking at a datetime object and the display to the user is just that, a display.

If you want to get the time to look like the last_updated, you need to use custom_timestamp on it with the correct syntax.

kitchen_movement:
  friendly_name: 'Last movement in kitchen'
  value_template: "{{ as_timestamp(states.sensor.kitchen_motion_sensor.last_updated) | timestamp_custom('%Y-%m-%d, %H:%M:%S') }}"
1 Like

What I donā€™t get it is that what is the last_updated field? Isnā€™t the last_updated field that is seen in States menu the attribute.last_updated? If I use the attribute.last_updated and try to change the way it looks it just shows ā€œUnknownā€ but if I use last_update I can change the way it looks in the GUI but it changes not only on when movement is seen but on other times alsoā€¦

Are you using Xiaomi motion sensor?

1 Like

No, Philips Hue

The changing of last_update in the frontend might have something to do with the as_timestamp() since I have two sensors and I changed one to show UTC time and other show correct time with as_timestamp() and the second one is updating itself but the one with UTC time notā€¦

The Xiaomi binary_sensor on door is with as_timestamp() too but it does not change itself.

The last_updated and last_changed fields are the 'Last time the sensor changed state this current run of home assistant". These values do not persist during shutdown.

As iā€™m looking at this, I donā€™t think it is. I think this is on your device only. Iā€™ve gone through all my sensors and none of them have a last_changed attribute that shows up in the states page. This may be why we are having the confusion.

Can you please go to the template editor and place the following code into it:

{{ states.sensor.kitchen_motion_sensor.last_updated }}
{{ states.sensor.kitchen_motion_sensor.attributes.last_updated }}

Take a screenshot of the output:

1 Like

Here is the screenshot:

Okay! We are getting somewhere.

So, now we need to find out if this is a datetime object or a string.

Do this in the template editor and take a screenshot:

{{ states.sensor.kitchen_motion_sensor.attributes.last_updated * 2 }}

1 Like

Ok, good news, its a string. That means we have to convert this to a datetime object so it can be formatted properly.

So is the format you want %m.%d %H:%M?

If so this is what you want:

kitchen_movement:
  friendly_name: 'Last movement in kitchen'
  value_template: "{{ as_timestamp(strptime(states.sensor.kitchen_motion_sensor.last_updated, '%Y-%m-%d %H:%M:%S')) | timestamp_custom('%m.%d %H:%M') }}"
2 Likes