Binary Sensor Template - last_tripped_time attribute

I am working on making a sensor template for when my gun safe door is open based on luminance and motion (eventually) from a zwave multi sensor. Everything works as expected but i wanted to have the last time it was opened like i do for my security system contacts which are binary sensors via Envisalink 4 to Honeywell panel.

Am i missing something on how to get this sensor template to register an attribute for last_tripped_time?

  - platform: template
        value_template: >-
          {{ states.sensor.zooz_zse40_4in1_sensor_luminance.state > 0 }}
        friendly_name: "Door"
        device_class: door

I think maybe an automation would work better?
If I understand well you want the last time your door was open and the way you check if the door was open is when luminance was more than 0?
I think the issue with a template sensor is that it will change its state whenever your monitored sensor changes.
I’d base my automation on your luminance sensor and whenever it’s more than 0, I’d save the last updated date to a Input Datetime sensor.
Let me know your thoughts…

Every entity’s state object has two attributes that are automatically updated:

last_changed is the last time the state of the entity changed
last_updated is the last time anything about the entity (state or attributes) changed

So, states.sensor.tpl_binsen_gunsafe_door.last_changed will be when the sensor changed to its current state. But, if you want to know the last time it changed to open, then you’d need an automation to record the time into another entity (along the lines of what @lolouk44 said.)

1 Like