Timestamp Attribute

I’m trying to run an automation at a certain time if a door sensor has not fired for 60 seconds or longer. However, as coded, it runs all of the time, even if the sensor has fired in the last 60 seconds. Is this not the correct use of the timestamp attribute (or is it coded wrong)?

alias: Zooz Door Sensor Test Duplicate5
description: >-
  Turn Light Red If Drawer Sensor Not Opened For 60 Seconds
trigger:
  - platform: time
    at: '20:35:00'
condition:
  - condition: template
    value_template: >-
      {{ ( as_timestamp(now()) -
      as_timestamp(state_attr('binary_sensor.open_close_xs_sensor_access_control_window_door_is_open',
      'last_triggered')) |int(0) ) > 60 }}
action:
  - service: light.turn_on
    target:
      entity_id: light.desk_lamp
    data:
      color_name: red
      flash: short
mode: single

last_triggered doesn’t exist for a binary_sensor (it exists for an automation).

For more information, refer to State Objects.

Try this version:

condition:
  - condition: template
    value_template: >-
      {{ now().timestamp() - states.binary_sensor.open_close_xs_sensor_access_control_window_door_is_open.last_changed.timestamp() > 60 }}

Thank you @123 I should have known that, I’ve read it in your previous posts. And thank you for posting code that works perfectly.

1 Like