Detect open cover after restart of HA

Hi Community,

I’m running an automation to remind me in case of an open garage door:

- id: alarm_garage
  alias: Alarm Garage
  trigger:
  - platform: state
    entity_id: binary_sensor.garage
    to: 'on'
    for: 00:10:00
  condition: []
  action:
  - service: camera.snapshot
    data:
      entity_id: camera.garage
      filename: /media/garage.jpg
  - service: notify.garage
    data:
      title: 'Alarm: Garage'
      message: Garage is open for 10 minutes
      target:
      - [email protected]
      data:
        images:
        - /media/garage.jpg

Its working fine but doesn’t react when the cover was opened during restart of HA (e.g. during an update). So I guess it doesn’t care when HA starts and the garage status is already “on”.
Any simple solution to remind me of an open garage that was opened during restart of HA?

Thank you a lot in advance and kind regards
Benedikt

Use this as a trigger:

But doesn’t this trigger the automation regardless the cover was opened or not during the off time off HA during a restart?
I only want it to be triggered when the state says it’s open after restart.

There’s really no easy way to do this. On startup entities begin in unknown state and then transition to their current state. That’s it. HA has no idea what state they were in last time it was running, it doesn’t even know if they existed then. HA basically only keeps track of the state entities are in right now. It can fire an event when they change with the old and new state but if that transition was missed then there’s no way to get it back.

The only exception is integrations that support restoring state. But this is usually for things which involve user input or entities that can’t be easily reconstituted by asking the service/device for its current state. I’m guessing that doesn’t apply here or you wouldn’t have posted this.

Do you use a notification service that supports replacing existing notifications via an ID? Like the companion app for example, when you send a notification to your phone you can replace existing notifications with tag. This is the easy solution here. Then you can do what finity is suggesting and basically have two triggers:

  1. Trigger 1 - when the door was opened and left open for 10 minutes
  2. Trigger 2 - when HA starts, if door is open and remains open for 10 minutes

You then use tag to ensure that even if you get the same notification twice one just replaces the other so you don’t really notice.

If you use a notification service that doesn’t support this (like telegram or a text message or something) then it gets trickier. I guess you’d need to wrap the entity in another one that supports restoring state, like a trigger template entity. Could make it like this:

template:
  - trigger:
      - platform: state
        entity_id: binary_sensor.garage
        to:
      - platform: homeassistant
        event: start
    binary_sensor:
      name: Garage Restore
      state: "{{ is_state('binary_sensor.garage', 'on') }}"

And then change your automation by replacing binary_sensor.garage with binary_sensor.garage_restore.

This should work because most of the time the two are exactly identical. But when you restart HA trigger template binary sensors restore state. So if the garage was closed when HA went down then this will restore to off. And then if it was opened while HA was off it will transition to on with the HA started event and at that point your automation is listening and will see the state change.

Then just add a condition that says the cover is open.

Thank you @finity @CentralCommand for your help.

I guess I need to go the way suggested by @CentralCommand. Will report how it’s working.

@finity can you help me with this try? How to write the condition that does not react to a change but a current state existing at startup?

Thank you a lot and all the best
Benedikt

- id: alarm_garage
  alias: Alarm Garage
  trigger:
  - platform: state
    entity_id: binary_sensor.garage
    to: 'on'
    for: 00:10:00
  - platform: homeassistant
    event: start
    id: start
  condition:
    - condition: state
      entity_id: binary_sensor.garage
      state: 'on'
  action:
  - service: camera.snapshot
    data:
      entity_id: camera.garage
      filename: /media/garage.jpg
  - service: notify.garage
    data:
      title: 'Alarm: Garage'
      message: Garage  {{ 'opened during restart' if trigger.id =='start' else 'is open for 10 minutes' }}
      target:
      - [email protected]
      data:
        images:
        - /media/garage.jpg

They want to know when the garage was opened during the restart of HA. Your automation will trigger for that but it will also trigger if the garage was already opened and then HA was restarted.

Also if the garage was opened during restart of HA the notification will say the garage had been open for 10 minutes when really it might’ve been like 30 seconds.

That’s why I laid out the alternate approach above to specifically trigger when the garage was opened while HA was down.

Actually I’m pretty sure it was both. But your right the message was inaccurate.

I fixed it

Thanks a lot the both of you. Very, very kind! Will try this weekend!