I’m wondering because this sequence doesn’t seem to work with this automation :
- alias: 'Force restart'
trigger:
platform: state
entity_id: switch.vmc
to: 'off'
for:
hours: 36
action:
- service: switch.turn_on
entity_id: switch.vmc
Turn the switch off
wait a while
Restart HA
Wait… the automation never start, neither after 36 hours after switch off, nor 36 hours after restart ?
Have you made sure that HA has taken note of the state change (to: ‘off’)? Otherwise the trigger won’t work. If you say that you restarted HA, I guess that HA has not been notified about any state change so the automation won’t run.
In the log, I have :
11:47 Home Assistant restarted
...
10:23 VMC turned off
Then several other restarts since, latest is 14:23 (none after that). This on Feb 28. I expected the switch to turn on at 22:23 on Mar 1st, or at least at 23:47 or 2:23 march 2nd but nothing…
I think that every restart resets any (triggered) automation and that there’s no “stickiness” between HA restarts.
But one step backwards: If I understand you correctly, you want to hard-restart HA every 36 hours. Why?
I’m afraid it is, I will do some further tests to ensure.
I want to force the switch for my extractor to start every 36 hours. I’ve planned several conditions (but not yet implemented) using hours of the day, humidity interior/exterior, humidity increase (start after shower), but at the end, I want to be sure it runs after a while whatever the conditions have been.
I have an idea: If you want to make sure that the extractor runs AT LEAST once every 36 hours, you could link the action to a script.
Then, in an automation rule, you can trigger the script if there has been no execution for 36 hours. Therefore you could set up a condition that compares the value provided by {{states.script.vmc_run.attributes.last_triggered}}
with the actual time via now()
. If more than 36 hours have passed, the script will execute.
If other automations have used this script in the meantime, this will be reflected in the last_triggered
attribute, which will be updated accordingly to the latest point of execution.
Likewise you keep track of the last execution of the script and create interdependencies between several automation rules, because they all automatically update the last_triggered
attribute.
Keep in mind though that this idea surely adds some complexity and I cannot tell if it’s really worth it. Up to you to decide
Of course, there will be a vmc_on automation, that I could use (last triggered) as a condition for my force start. This will cover the case where there was no automatic start during the past 36 hours. But it won’t cover the case where someone press the button manually (broadlink sc1 device).
I didn’t think of a script. Great idea, as it covers the case where the frontend UI is used to turn on.
Anyway this is needed only if HA restarts, so maybe just a switch.turn_on command during the startup process could be enough !
Just found another approach for you, see this post: Motion detection does not trigger fast enough - #8 by touliloup
The idea of working with timers could be a viable solution for you. In addition, it wouldn’t require any value_templates as per my initial idea.
“When calling the start service on a timer that is already running, it resets the duration it will need to finish and restart the timer without triggering any events”
That could work yes.
I’ve found another workaround. At first, I added a switch.turn_on in the HA restart automation.
Now, I changed the “force restart” automation to be triggered with 2 events : the automation that turns off the extractor has been triggered over than 36 hours ago, or switch is off for more than 36 hours. The later is only needed because switch can be turned off, either manually (Broadlink SC1) or via the frontend.
The former trigger makes a condition necessary, that the switch is actually off. Looks like :
- alias: 'A_ VMC force restart'
trigger:
- platform: state
entity_id: switch.vmc
to: 'off'
for:
hours: 36
- platform: template
value_template: "{{ ((as_timestamp(now()) - as_timestamp(states.automation.a__vmc_stop.attributes['last_triggered'])) / 3600) > 36 }}"
condition:
- condition: state
entity_id: switch.vmc
state: 'off'
action:
- service: switch.turn_on
entity_id: switch.vmc