Hi,
What is the best way to turn off a device, wait for X hours and then turn it on again? I want to use it under a hydroponic system where I need to leave the lights off for X hours.
I have the following automation but today it fails to turn on again (it keeps executing):
I want to avoid creating a new one (to avoid too many automation), but if splitting automation will be a better approach let me know. Looks like keeping an automation running is not a good idea as the restart some way like my case can skip the off. Not sure if is a bug in the code or is the way that is expected but Wait for time to pass didnât work here.
Thanks!
I advise you to not use a long delay in any automation (your example uses a 4 hour delay).
If you execute Reload Automations, or restart Home Assistant, while the automation is executing the delay, it will cancel the delay and the switch will not be turned off.
If you use the Automation Editor, to create or modify an automation, every time you click the Save button, it performs a Reload Automations (and cancels any automation that is executing a delay or for or wait_template, etc).
The following example turns off the switch at 12:00:00 and on at 16:00:00. It is unaffected by Reload Automations or a restart (unless the restart occurs at the either of the two scheduled times).
alias: Estufa - 20h On, 4 off
description: ''
trigger:
- id: 'off'
platform: time
at: '12:00:00'
- id: 'on'
platform: time
at: '16:00:00'
condition: []
action:
- service: 'switch.turn_{{ trigger.id }}'
target:
entity_id: switch.0x847127fffeae874f
mode: single
If you need a 4-hour on/off cycle, like Markus99 described, the example I posted above can be easily adapted to meet that requirement (or other scheduling pattern).
alias: Estufa - 20h On, 4 off
description: ''
trigger:
- id: 'off'
platform: time
at:
- '12:00:00'
- '20:00:00'
- '04:00:00'
- id: 'on'
platform: time
at:
- '16:00:00'
- '00:00:00'
- '08:00:00'
condition: []
action:
- service: 'switch.turn_{{ trigger.id }}'
target:
entity_id: switch.0x847127fffeae874f
mode: single
Look carefully at how you defined the triggers and compare them to the example I posted above. Pay special attention to how hyphens are used for each trigger (your example uses them incorrectly).
I think itâs because I ran the automation after the âonâ time, so it could not find the correct state, it works perfectly if I change the value of the âonâ time and then run the automation
If you mean you triggered the automation manually, then itâs to be expected that trigger.id will be undefined and so the service call will be a truncated switch.turn_ (without a terminating on or off). For more information, refer to: Testing your automation.