A state (timestamp) that is not affected by HA restarts?

I store these timestamps in an input_datetime for this purpose - and use them straight from there for automations, etc.

As you can see below, I have a binary_sensor for the door and an input_datetime with the same name + “_last_opened” - this allows me to use a relatively simple template for all sensors of the same kind.

- alias: Last Door Opened Date and Time
  trigger:
    platform: state
    entity_id: binary_sensor.front_door, binary_sensor.back_door, binary_sensor.balcony_door, binary_sensor.downstairs_door, binary_sensor.living_room_motion
    from: 'off'
    to: 'on'
  action:
    - service: input_datetime.set_datetime
      data_template:
        entity_id: input_datetime.{{ trigger.entity_id[14:] }}_last_opened
        time: '{{ (as_timestamp(now()) | timestamp_custom("%H:%M:%S", true)) }}'
        date: '{{ (as_timestamp(now()) | timestamp_custom("%Y-%m-%d", true)) }}'
7 Likes