How to extract timestamp value for when door open/closed?

Hi - looking to extract this timestamp to know when my backdoor was last closed. I am struggling to find a way to get this timestamp as its not an attribute of the zwave sensors.

Is it sitting in the DB and I can pull using SQL or is there another way?

image

states.<sensor name>.last_changed

That will only work for the last state change, so is also valid for last opened, not just last closed.

If you want to specifically just know when it last closed, you can either pull it from SQL, or you could create an automation that records the timestamp when the door changes from open to closed in an input_text.

You can store it in an input text with an automation.

input_text:

time_back_door_closed:
  name: 'Time Back Door Closed'

automation:

trigger:
  platform: state
  entity_id: binary_sensor.back_door
  to: closed
action:
  service: input_text.set_value
  data_template:
    entity_id: input_text.time_back_door_closed
    value: "{{ states.binary_sensor.back_door.last_changed }}"
1 Like

Thanks both - so can pull out last_changed but not in local british summer time its showing

2020-05-11 11:03:56.940681+00:100:

When I want to get 12:03 (local time)

It show correctly as this on the UI. Is there a transformation I can apply?

Database stores in UTC and HA appears to then apply a timezone, unfortunately that doesnt seem to happen when accessing the sensor values. Probably a cleaner way but…

{{ as_timestamp(states.<sensor_name>.last_changed) | timestamp_custom('%H:%M', True) }}
1 Like

Thanks did the trick

Ok not there but close. I have the below configuration but it doesnt populate time nor throw any errors. Any idea what I am doing wrong?

- id: '1589198310461
  alias: 'Back Door: Set Time Closed'
  description: Sets the time for when the Back Door Was Last Closed
  trigger:
  - entity_id: binary_sensor.aeon_labs_zw112_door_window_sensor_6_sensor
    platform: state
    to: closed
  action:
    service: input_datetime.set_value
    data_template:
      entity_id: input_datetime.time_back_door_closed
      value: "{{as_timestamp(states.binary_sensor.aeon_labs_zw112_door_window_sensor_6_sensor.last_changed) | timestamp_custom('%H:%M', True) }}"

My mistake, it should be:

to: 'off'

Ahh yes - should have picked up on that. Thanks.