Reliable automation start stop

Hello all,

Is there a reliable, restart resistant method to make sure that my automations will start, or at least stop when they are supposed to?

I just woke up with the ac running all night because the automation that was supposed to turn it off after 3 hours did not fire. I suspect it was because there was a restart at some point during those 3 hours.

I would be glad if there was a sort of check to see if after restart there are things that are running and are not supposed to. Can this be done?

We need to see the automation to help you

1 Like

Here you go. I have a few that are similar to this. The idea is: if device state changes then keep it on for 3 hours the turn off. Or at set time turn device on, keep it on for x time then turn it off.

alias: AC automatic off
description: ""
trigger:
  - platform: state
    entity_id:
      - climate.birou
    from: "off"
    for:
      hours: 3
      minutes: 0
      seconds: 0
  - platform: state
    entity_id:
      - climate.living_room
    from: "off"
    for:
      hours: 3
      minutes: 0
      seconds: 0
condition: []
action:
  - service: climate.set_hvac_mode
    data:
      hvac_mode: "off"
    target:
      entity_id:
        - climate.birou
        - climate.living_room
mode: single

Just remove from: off and replace with to: on

Have a look at the history of the climate entities: did one of them go unavailable at some point during the three hours?

Do you not have control over that? Why would there be a restart in the middle of the night? If you’re auto-updating, turn that off.

You could have an automation triggered by the climate turning on that sets an input_datetime 3 hours in the future, then use that to trigger turning it off. Create input_datetime.climate_birou_off, then:

alias: "Record time of climate birou plus 3h"
trigger:
  - platform: state
    entity_id: climate.birou
    to: 'on'
action:
  - service: input_datetime.set_datetime
    target:
      entity_id: input_datetime.climate_birou_off
    data:
      timestamp: "{{ now()|as_timestamp + (3 * 3600) }}"

Thank you for your help!

The reason it restarted was because I did something to the server and had to restart. But in general I get power cuts where I live and I noticed that these completely mess up the automations. Also in the case of the ac there are issues with the wifi connection sometimes. the units just disconnect randomly sometimes. I am wondering if there are methods to include in the automations to make them more robust.

That’s why I was thinking about a sort of check after restart and just shut down everything that is running. I prefer my yard to not get water.

I’ll look into the example you provided but I am not sure I understand it. How do I use this to trigger the ac off?

Would strongly recommend a UPS.

You might need to change the trigger of the automation I provided: you might need to trigger off the hvac_mode going to cool or something like that.

All it’s doing is saying “the AC has turned on, so let’s make a note of when it should go off, in three hours’ time”. Then you trigger off that “note”:

trigger:
  - platform: time
    at: input_datetime.climate_birou_off
action:
  - service: climate.set_hvac_mode
    data:
      hvac_mode: "off"
    target:
      entity_id:
        - climate.birou

…and duplicate for the living room, or keep one timer for both: whichever you prefer.

If your Home Assistant server is subject to frequent unplanned restarts, it will require you to avoid using things in your automations and scripts that can’t resume operation after a restart.

For example, the for in your State Trigger waits for the state to be from off for 3 hours. A restart occurring during the 3 hours will terminate all active and in-progress automations/scripts and restart them from scratch on startup. Whatever time had elapsed during the 3 hour countdown is lost.

You would also have to minimize your use of delay, wait_for_trigger, wait_template, repeat, etc or at least avoid having them spend long periods of time waiting (i.e. 3 hours is long). For long periods, you would need to use Timers (whenever possible) because an active timer entity can survive a restart. However, working with Timers is more complex.

Although it’s always a good idea to design with restarts in mind, a UPS helps your system ride out short interruptions thereby giving you more latitude in designing your automations/scripts.

Without a UPS, you may wish to consider creating an automation, triggered on startup, that sets various things to a “safe” state. For example, it might turn off various appliances, like your AC unit.

tl;dr
Without a UPS, it becomes challenging to create automations and scripts that survive interruptions.


EDIT

Clarified the state-change monitored by the State Trigger’s for (in response to Hellis81’s post below).

?
It waits for the state to not be off for three hours and the previous state be off. Right?

And that is the issue here, it can only be off when you just turned it on, when HA restarts it’s unavailable then on.
That is why my suggestion is to use to: on.
But that is also not perfect. If HA restarts after 2:59 then your AC will be on for 5:59 before it shuts off.

What Troon suggests is far better but also more complex.

You’re right; I missed a word there, from off for 3 hours.

The upshot is that for depends on Home Assistant being alive and uninterrupted throughout the duration of the for’s countdown, otherwise its elapsed time is lost and reset on startup.

thank you everyone! I will start using timers! I’ll come back for help if I can’t manage it!

Thank you all for getting me to start using timers! Much better now!

I have one other question about timers:

Is it possible to setup a slider in the frontend that will go from 1 to 4 in steps of 0.5 hours, so that I can modify the value of the timer?

For example if I want the ac running for 1 hour I set the slider to 1. If I want it to run for 2.5 hours then I set it there and start the ac.

I have seen the timer.change service call but that appears to add or subtract time from a current timer.