Just checking - I’m an automation newbie.
All I want is to turn on a device when a time is met - then turn off after two hours.
does the following yaml work?
It will work, but prolonged waiting is not efficient and can cause reliability issues since they do not survive restarts. A common method to accomplish this type of automation more efficiently and reliably is to have multiple triggers, then use an If/Then or Choose action to determine which service to call based on which trigger fired.
The following is a basic example. There are many other ways to accomplish the same goal using other methods such as schedule helpers, templating, or timer helpers.
Hey thanks for those hints. Drew you mention prolonged waits do not survive restarts? Not sure what you mean by this. Are you saying that a restart of HA will affect my automation timing?
Pat
Not timing but the automation that is set to wait will be terminated and when HA reboots it has no memory of anything from before the restart.
So that’s why you shouldn’t use long delays in automations.
I usually limit myself to a few minutes at the most.
But also depending on how important the automation is.
With the recently added options for choosing how YAML is reloaded, this is less of an issue now than it used to be. However, restarts and reboots essentially negating wait, delay, and for is a common source of frustration for newer users.
As @Hellis81 noted, how much you need to worry about an automation’s reliability really depends directly on how important its outcome is. Since excessive ozone can be harmful to people, pets, plants, and equipment you may want to consider extra protections to guarantee that your generator is turned off in a timely manor.
Drew,
Thanks for your help. the ozone generator is in my new cold plunge construction. The timer is set for midday so that when our normal plunge happens - about 6pm then there should have been dissipation of gas suitable for use. When we open the lid to the plunge it is open for about 10 minutes before use as well.
Pat
Yet another variation that ensures the ozone generator is set to the correct state on startup. It mitigates the possibility where Home Assistant may have been offline when it was scheduled to trigger at 12:00 or 14:00 (thereby failing to set the device in the correct state).
Taras,
indeed I had exactly that issue. A power cut about 12.15 stopped the automation form resuming. I then tried to put your example in but during run I get error
You can’t use an automation’s Run command to test it when it references triggers variables.
The Run command only executes an automation’s actions. It doesn’t exercise the automation’s trigger. A trigger variable is defined only if the trigger occurs (and the Run command doesn’t trigger anything).
OK - getting some understanding now. When I put in your code though I would have expected HA to very quickly recognise that the conditions set of time between 12 and 1400 hours was met and the switch should have been triggered? Or do I have to wait for a restart/refresh ???
Pat
Taras,
have repasted your code and will see what happens tomorrow then. Still learning about code - does your example only run the switch between the hours of 1200 - 1400? Or should the variables cmd line read
This means the current hour must be greater or equal to 12 and less than 14. The values 12 and 13 meet that requirement, which represent the hours when the device should be on.
12 <= now().hour < 14
This means the current hour must be greater or equal to 12 and greater than 14. Any value greater or equal to 15 meets the requirement but that’s not the hours when the device should be on.
An automation does nothing until it’s triggered. The example posted above is triggered at two times (12:00 and 14:00) and when Home Assistant restarts.
Taras,
I wanted to reduce the run time of the ozone generator and so I changed two values to reflect this so that the run time should have been only 30 minutes. But the generator was switched on and then just kept running?
Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions.