Best wait to turn off a device for X hour and then turn on again in one automation?

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):

alias: Estufa - 20h On, 4 off
description: ''
trigger:
  - platform: time
    at: '12:00:00'
condition: []
action:
  - type: turn_off
    device_id: 8f9f5693248ae20b9caff75a965f90de
    entity_id: switch.0x847127fffeae874f
    domain: switch
  - delay:
      hours: 4
      minutes: 0
      seconds: 0
      milliseconds: 0
  - type: turn_on
    device_id: 8f9f5693248ae20b9caff75a965f90de
    entity_id: switch.0x847127fffeae874f
    domain: switch
mode: single

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!

easier with separate dedicated operations.

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 it’s only a 4 hours on, 4 hours off cycle, then you could use multiple TIME triggers and just toggle the switch in the action section.

IE. Trigger at 00:00:00, 04:00:00, 08:00:00, 12:00:00, 16:00:00, 20:00:00
Action switch.toggle

This should survive restarts unless the restart is exactly over the triggered time I would think.

1 Like

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
1 Like

No, it’s once per day. But thank you!

Thank you all for the responses!

Hey!

I tried implementing this but HA gives me the below error

Invalid config for [automation]: required key not provided @ data[‘trigger’][0][‘platform’]. Got None

Here’s the code I pasted

- id: "1657199282681"
  alias: 4x4 Lights Schedule
  description: ""
  trigger:
    - id: 'off'
    - platform: time
      at: 'input_datetime.4x4_lights_off_time'
    - id: 'on'
    - platform: time
      at: 'input_datetime.4x4_lights_on_time'
  condition: []
  action:
    - service: switch.turn_{{ trigger.id }}'
      data: {}
      target:
        entity_id: switch.4x4_lights

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).

Hey!

Thanks, I copy pasted your code as is and now it doesn’t give me the same error, however it gives me this now

Error: Template rendered invalid service: switch.turn_

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.

This is most reliable way

Using a timer its pretty easy.

description: ""
mode: single
trigger:
  - platform: time
    at: "12:00:00"
    id: Lamp On
  - platform: event
    event_type: timer.finished
    event_data: entity_id: timer.loft_lamp_timer
    id: Lamp Off
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: Lamp On
        sequence:
          - service: light.turn_on
            data: {}
            target:
              entity_id: light.loft_lamp
          - service: timer.start
            data:
              duration: "04:00"
            target:
              entity_id: timer.loft_lamp_timer
      - conditions:
          - condition: trigger
            id: Lamp Off
        sequence:
          - service: light.turn_off
            data: {}
            target:
              entity_id: light.loft_lamp