Trigger after some time has passed, even after reboot

This might be a weird Q, i’m not even sure where this belongs, and if its possible at all, but ill just “put it out there”, hopping someone in this wonderful community might have an idea.

can i make a trigger out of “passing time”, regardless of ‘state changing’?

I have a smart switch on my water-heater (boiler) that is automated to turn off, 30 min after i turn it on, with a simple automation - if state changes to “ON”, wait 30m, turn back “OFF”.

However, if within that 30m time period i restart HA, all automations resets and since the switch is already “ON” when system comes back, nothing triggers it back to “OFF”, since the automation triggers only after “state is change” to “ON”.

So, is it possible to make a sensor that will track the “time on” state of that switch?

the way i figure it, in that case, even when HA is restarted, when it come back online - it knows the state of that switch is “ON”, and start showing the “time on” from zero. that way, 30 min later, it will trigger it back “OFF”.

worst case scenario, it will be on for little less than a full hour (unless i keep restarting the system of course)

hope this make sense to someone.
cheers.

This automation will be triggered on each HA restart, will check if the boiler is on, and if so turn it off after 30 min.

It could likely be merged into your existing automation.

automation:
  trigger:
    - platform: homeassistant
      event: start
  condition:
    condition: state
    entity_id: switch.the_boiler
    state: "on"
  action:
    - delay: 0:30
    - service: switch.turn_off
  	  target:
  	    entity_id: switch.the_boiler  

Koying’s suggestion should do the trick and ensure a running boiler is turned off within 30 minutes of a restart.

Another option is to employ a 30-minute timer. However, all countdown-based facilities in Home Assistant (timer, delay, for, wait_for_template, etc) are subject to the same deficiency and are reset on startup (i.e. remaining countdown duration is lost). If you are interested, I created a means of restoring interrupted active/paused timers:

Basically, if an active 30-minute timer is interrupted 10 minutes into its countdown, and Home Assistant is offline for 15 minutes, when Home Assistant restarts, the 30-minute timer will be automatically restarted with the remaining duration: 30 - 10 - 15 = 5 minutes.
However, if you want a simple and effective backstop then use koying’s suggestion (implementing a timer-based solution is more work).

Thanks guys!

i love "123"s implementation, and if i understood correctly, as long as HA wasn’t down long enough to excide the original timer - it will work fine. however (correct me if I’m wrong), i.e. if the timer was for 10 min, and i restarted HA on the last 10s of that timer, it will not work, as it will take HA over 10s to restart. is that correct?
haven’t tested it yet, but this “work around” is brilliant!

koying suggestion is to create another automation (to the same boiler switch) by using HA “start” as the starting trigger, and the state of the switch as “on” for conditioning to actually run it and turn it of after 30 min - (easy and elegant!) hope i understood it correctly.

thank you guys for the ideas.
i appreciate the fast answering!
:slight_smile:

Correct. If Home Assistant is disabled for longer than a timer’s duration, the expired timer is not restored when Home Assistant is restarted.

HOWEVER, two users wanted to have even expired timers restarted with a brief duration, perhaps a second or two, just enough for the timer to finish counting down and generating a “finished” event (thereby triggering any automations that listened for the timer’s “finished” event). I provided an example of how to modify the code to support this behavior.

You don’t really need a new automation.

Assuming you already have something like

automation:
  trigger:
    - platform: state
      entity_id: switch.the_boiler
      to: "on"  
  action:
    - delay: 0:30
    - service: switch.turn_off
  	  target:
  	    entity_id: switch.the_boiler  		

You can combine bothe cases:

automation:
  trigger:
    - platform: homeassistant
      event: start
    - platform: state
      entity_id: switch.the_boiler
      to: "on"  
  condition:
    condition: state
    entity_id: switch.the_boiler
    state: "on"
  action:
    - delay: 0:30
    - service: switch.turn_off
  	  target:
  	    entity_id: switch.the_boiler  		

got it.
neat

i kind of feel like I’m learning to bend “Skynet” to my own wishes, using its own “rules”…

:slight_smile:

Here’s something that counts down from 30 and resumes its countdown when Home Assistant is restarted or automations are reloaded (picking up the count from where it left off).

You need to create a counter with an initial value of 30. That’s what retains the countdown value on restart/reload. You can define it via the UI (Configuration > Helpers > Counter) or manually:

Here’s the automation:

- alias: Boiler Auto Shutoff
  trigger:
    - platform: homeassistant
      event: start
    - platform: event
      event_type: automation_reloaded
    - platform: state
      entity_id: switch.the_boiler
  condition:
  action:
    - choose:
        - conditions: "{{ is_state('switch.the_boiler', 'on') }}"
          sequence:
            - repeat:
                sequence:
                  - delay: '00:01:00'
                  - service: counter.decrement
                    target:
                      entity_id: counter.boiler
                until: >
                  {{ states('counter.boiler') | int == 0 or is_state('switch.the_boiler', 'off') }}
            - service: switch.turn_off
              target:
                entity_id: switch.the_boiler
    - service: counter.reset
      target:
        entity_id: counter.boiler
  mode: single		
  max_exceeded: silent

How it works

  • The automation is triggered by restarts, automation reloads, and when the boiler switch is turned on or off.

  • If the boiler switch is on, a repeat decrements the counter every minute until the counter’s value is 0 or if the boiler switch is turned off.

  • After exiting the repeat it turns off the boiler switch and resets the counter to its initial value (30).

  • If the automation is triggered but the boiler switch is off, it simply resets the counter.

2 Likes

nicely written man!
so… this comes as an addition (backup plan…) to the original 30m timer from the original automatization, and also cover any case of automatization reload regardless to HA restart.

correct?
if so…
NOOOICE!

It’s a replacement for whatever you may be currently using to turn off the boiler after 30 minutes.


EDIT

A small side-benefit of using the suggested approach (repeat with a decremented counter) is that, while the boiler switch is on, you can check the counter’s value to see how much time remains before the switch is turned off.

ohhhhhh… i c now

so its all based on a 30 min timer “helper”…

gotcha!

nice work man. impressive indeed.