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

Hello,

I am new to Home Assistant and just trying to build the view for my likings. But my problem is the Xiaomis Door/Windows sensor. The sensor is showing in Home Assistant fine, but I am unable to get the time when states has been changed to show in HA. These can be seen from Xiaomis Mi Home APP but is there way to get this data to show in HA?

Thanks in advance.

You can use the states.sensor.xiaomi_sensor_name.attributes.last_changed in a template sensor if you want to display this, or you can use custom_cards to have ti displayed automatically as I’ve done:
image

Check my config to help you get started if you want to go to custom UI way:

2 Likes

your custom cards look super nice, I need to look into that !
Thanks for sharing your config :slight_smile:

1 Like

Thanks for the reply.

I will try the template sensor method first! So basically it should work if I follow the template sensor guide and just put the “states.sensor.xiaomi_sensor_name.attributes.last_changed” in it? Modified with the sensor name obviously.

Yes it should work as long as the status of your entity has changed indeed.

1 Like

For some reason it does not work. I only get it to show “Closed”. When I click the icon open, it says there that “31 minutes ago” but I am not able to get this information to show in the card instead or with the state “Closed”/“Open”.

Any ideas how I can get the text changed to the time?

Can you share your code please?

1 Like

I have this in configuration.yaml

 binary_sensor:
  - platform: template
    sensors:
      door:
        friendly_name: "Door last opened:"
        value_template: states.binary_sensor.NAME.attributes.last_changed

And this I have in Groups.yaml

Door:
  name: Door
  entities:
    - binary_sensor.door

I also tried to add “.attributes.last_changed” to groups.yaml but it did not work. It only shows “Off” in the GUI.

You should probably change NAME with the entity of your sensor.

1 Like

Can you format your code (check the blue bar on top of this page)
Your value template line is missing the curly braces
value_template: {{states.binary_sensor.NAME.attributes.last_changed}}

1 Like

I added the {{ }} but for some reason that seems to kill my installation. I tried two times and on both times I have to reformat my SD-card and start on fresh installation and restore snapshot. Might be coincidence but this has only happened when I reboot after adding the {{ }}.

That’s a coincidence.
Whenever you make a change in config and save, you should check your config from the front end as it’ll tell you if something is wrong before you restart ha and potentially block it from starting.
Worst case scenario ha won’t start but it should not freeze your OS.
If you need to get data from any ha entity you need the curly braces.

1 Like

Besides the curly braces you also need to put the entire expression in quotes. And obviously change NAME to the actually object_id of the binary_sensor you’re using. Lastly, last_changed is not under attributes:

value_template: "{{ states.binary_sensor.NAME.last_changed }}"
1 Like

My code now looks like this

binary_sensor:
 - platform: template
   sensors:
     door:
       friendly_name: "Door last opened:"
       value_template: "{{ states.binary_sensor.door_window_sensor.last_changed }}"

and groups:

Door:
  name: door
  entities:
    - binary_sensor.door

But still in the frontend the card is door

Because you’re using a binary sensor. Change it to a regular sensor. Sorry, I should have noticed that before.

1 Like

So it is not possible with this sensor? Even when I open the card it shows the last change:

door2

I think you misunderstood me. Sorry. To be clear, I mean change binary_sensor.door to sensor.door:

sensor:
 - platform: template
   sensors:
     door:
       friendly_name: "Door last opened:"
       value_template: "{{ states.binary_sensor.door_window_sensor.last_changed }}"
1 Like

YES, it works perfectly now! Just need to format the timestamp to look better but the information is there :slight_smile:

Thank you all for your help!

Working config:

Groups.yaml

Door:
  name: door
  entities:
    - sensor.door

Configuration.yaml

sensor:
 - platform: template
   sensors:
     door:
       friendly_name: "Door last opened:"
       value_template: "{{ states.binary_sensor.door_window_sensor.last_changed }}"

No problem. Just tack on strftime on the end, something like:

value_template: >
  {{ states.binary_sensor.door_window_sensor.last_changed
     .strftime('%d/%m/%Y %H:%M:%S') }}
1 Like

I use this same topic to ask another question.

I have sensor that has last.updated time in format “2018-07-23,08:55:39” and when I use this attribute in frontend it shows as [‘2018-07-23,08:55:39’].

I would like to it be in the format 23/07/2018 08:55:39 but when I use “.strftime(‘%d/%m/%Y %H:%M:%S’)” it changes the timestamp in frontend to “Unknown”.

This is in template sensor. The code is:

kitchen_movement:
  friendly_name: 'Last movement in kitchen'
  value_template: "{{ states.sensor.kitchen_motion_sensor.attributes.last_updated }}"

but if I change it to

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

it shows “Unknown” for some reason.

The device is sensor also and it has timestamp in its attributes.

Does anyone have any idea why it does this?

EDIT: Also the time zone is wrong in the front panel, it shows everything in UTC eventhough I have configurated time_zone in homeassistant: