Template Trigger not work as I expect

This has got to be something stupid/simple I am just missing.
This is my template trigger:

  - trigger:
      - id: "Open"
        platform: state
        entity_id: binary_sensor.new_front_door_sensor_contact
        from: "off"
        to: "on"
      - id: "Closed"
        platform: state
        entity_id: binary_sensor.new_front_door_sensor_contact
        from: "on"
        to: "off"
    sensor:
      - unique_id: sensor.new_front_door_last_open
        device_class: timestamp
        state: "{{ now() }}"
        attributes:
          open_close: "{{ trigger.id }}"

I want it to record the time the door was last opened or closed - which it does.
However, when I reboot HA, it resets that time to that startup for the ‘Closed’ id. But I have it qualified to only trigger if it goes from ‘on’ to ‘off’ and ‘off’ to ‘on’. And I can see in the history that it goes ‘unknow’ during the reboot and then directly to ‘off’ (Closed).

The Tile Card:
image

The Visual Editor for the Tile Card:

What dumb thing am I doing wrong?

Thanks!

I’m not sure a sensor survives a restart.
You could make it set a date time helper and that will survive restarts. But not sure why this happens…

I see what you’re trying to do. My contact sensors are Zigbee devices and do the same ‘unknown’ during a restart.

However, maybe I’m missing something in your screenshots, but, it looks like your Template Sensor is working, no? Looks like you are comparing the device “last changed” to your Template Sensor “last changed”, which are not the same; and your template sensor is retaining the information through a restart.


You have a template sensor with a state that records the time and date it last open/closed or closed/open, and then a tile card using that template sensor with the “last changed” as part of the attribute content.

While the contact sensor device reports unknown, and upon restart reports it is closed 6 minutes ago (after the restart), your Tile Card utilizing your Template Sensor still shows state changed 8 minutes ago, which I presume is before the restart you did; is this not what you want?

So while the sensor state change says 6 minutes ago, post-restart, your template sensor is still showing 8 minutes ago.

that or I haven’t had enough coffee yet this morning heh

EDIT: can you confirm the state (i.e. date/time) of your Template Sensor before and after a restart

2 Likes

OMG. I knew I was doing something stupid!
Yes! I wanted ‘State’, not ‘Last Changed’ or ‘Last Updated’. Argh
Thank you!
I apparently need A LOT more Coffee!!!

1 Like