How to restart a delay from the beginning when an entity changes state from on to off and back

Hello all–
I am a newb, and 3 weeks ago had no idea what HA was. With that said I’ve made some great progress except for this one concept. I’ve read at least 25 related posts, and numerous docs and still can’t wrap my head around the correct way to do this. Any pointers would be greatly appreciated.

I have an alarm panel, with internal relays that close when in alarm. One is for critical alarms, and one for non-critical. I will soon have a Shelly Uni monitoring those 2 relays, so will have HA entities defined with on and off states.

I want the following to occur. When an entity state changes from off to on, a delay begins (30 seconds for critical alarms, 5 minutes for non). I want to create a couple automations that, after the delay, I receive an email and a mobile notification. Those actions are defined and work well. The issue is, if an entity turns off and then back on during the delay period, I want the delay to restart from the beginning. Obviously if the entity stays off the automation exits and waits for a new trigger. And because the delays are so short, I’d prefer simplicity over worrying whether they get reset during a system restart, save, or something.

This is where I’m at, which is a basic starting point. I’ve tried a dozen iterations of this without the desired result. This is my first post here so I’m sure I’m posting the code incorrectly.

alias: Alarm Notification - Critical
description: Sends email and text when a critical alarm is detected after 15 seconds
  - platform: state
      - switch.critical_alarm
    to: "on"
    from: "off"
      hours: 0
      minutes: 0
      seconds: 15
condition: []
  - service: notify.email_notification
      message: "Alert: Critical alarm has been triggered"
      title: Home Assistant - 11223 Critical Alarm
  - service: notify.mobile_app_xxxxxxx
      message: "Alert: Critical alarm has been triggered"
      title: Home Assistant - 11223 Critical Alarm
        visibility: public
mode: single

There will also be a third automation that looks and both entities, and when they both go to off for 30 seconds, I will receive notifications that all has been reset. I assume that the same solution here will apply. Like I’ve said, I’ve read all of the docs on delays, triggers, mode single vs restart, etc. and nothing will restart the delay–the intervening state change from on to off and back to on is ignored.

Thanks, I really appreciate any advice.

Create a boolean helper variable that gets set by an automation when the required conditions (trigger) are met. Then use this variable as a condition in the final automation to restrict it from proceeding if the condition is not met.

change this to:

mode: restart

1 Like

These entities should not be switches. They should be binary sensors. Unless you can actually control the relay state as well from the switch?

How did you create them?

As for your trigger, it should do exactly what you describe (except you have the delay set to 15 sec).

Thanks. I had tried this, but the result I was seeing was–
Once triggered, the delay would begin. If the trigger went to off for say, 10 seconds, and back on, the original time would just run out and the action would occur. As opposed to the desired action of the delay resetting and starting from the beginning. I’ll try it again. I appreciate it.

Thank you, Tom. I do not yet have the Shelly Uni device in my hands so I don’t know quite how it will appear in HA. I was using a Kasa switch as a stand in. I don’t believe that would affect what I’m trying to do, although being the newb I am, I could be wrong. The time is also correct in the YAML, should have had 15 seconds in the explanation.

As it’s written, I’m only seeing what I described in my reply to Dave. The trigger starts the delay. If the trigger goes back to off during the delay there is no action, but it seems the delay keeps running to 0. If the trigger goes to off and then back on in the original 15 seconds, the action fires when that time runs out, not 15 seconds from the point the trigger went back to on the second time. Sorry if that’s confusing…

Thanks, pcwii. Unfortunately, this is likely beyond my level of expertise, for now. Would you have an example, by chance? One thing I was looking at was using a timer. This was interesting to me, from the Timer docs:
When calling the start service on a timer that is already running, it resets the duration it will need to finish and restarts the timer without triggering a canceled or finished event.
I’ll keep reading, for sure.