Running automation every 2 hours for 30 minutes

I’m trying to create an automation for a fan to run for 30 minutes every 2 hours. I referenced a similar question below before posting (for due diligence), but HA is still generating errors.

When I add the code below, it generates the error “Message malformed: extra keys not allowed @ data[‘alias’]”.

alias: Grow Tent Fan Schedule
description: ''
mode: single
trigger:
  - alias: Grow Tent Fan On
    - platform: time
      at: '00:00:00'
    - platform: time
      at: '02:00:00'
    - platform: time
      at: '04:00:00'
    - platform: time
      at: '06:00:00'
    - platform: time
      at: '08:00:00'
    - platform: time
      at: '10:00:00'
    - platform: time
      at: '12:00:00'
    - platform: time
      at: '14:00:00'
    - platform: time
      at: '16:00:00'
    - platform: time
      at: '18:00:00'
    - platform: time
      at: '20:00:00'
    - platform: time
      at: '22:00:00'
action: switch.turn_on
condition: []
action:
  - type: turn_on
    device_id: c08444c0640cea2370e079d4a99be825
    entity_id: switch.grow_tent_fan
    domain: switch

  - alias: Grow Tent Fan Off
    trigger:
    - platform: state
      device_id: c08444c0640cea2370e079d4a99be825
      entity_id: switch.grow_tent_fan
      from: 'off'
      to: 'on'
      for: '00:30:00'
    action:

This ^^
And because of this it seems you have indentation problems also. I believe “platform” and “at” needs to go two steps left.

EDIT; I see now there is another one in the actions.

EDIT 2. The post you linked to has two automations in the same code block as Taras stated. That is probably why it’s confusing.

alias: Grow Tent Fan Schedule
description: ''
mode: single
trigger:
  - platform: time
    at: '00:00:00'
  - platform: time
    at: '02:00:00'
  - platform: time
    at: '04:00:00'
  - platform: time
    at: '06:00:00'
  - platform: time
    at: '08:00:00'
  - platform: time
    at: '10:00:00'
  - platform: time
    at: '12:00:00'
  - platform: time
    at: '14:00:00'
  - platform: time
    at: '16:00:00'
  - platform: time
    at: '18:00:00'
  - platform: time
    at: '20:00:00'
  - platform: time
    at: '22:00:00'
condition: []
action:
  - type: turn_on
    device_id: c08444c0640cea2370e079d4a99be825
    entity_id: switch.grow_tent_fan
    domain: switch

Next automation:

  - alias: Grow Tent Fan Off
    trigger:
    - platform: state
      device_id: c08444c0640cea2370e079d4a99be825
      entity_id: switch.grow_tent_fan
      from: 'off'
      to: 'on'
      for: '00:30:00'
    action:

And there is something missing there.
One option is to skip the second automation and instead add this below the first:

  - delay:
      hours: 0
      minutes: 30
      seconds: 0
      milliseconds: 0
  - type: turn_on
    device_id: c08444c0640cea2370e079d4a99be825
    entity_id: switch.grow_tent_fan
    domain: switch

You could also replace all your triggers with:

trigger:
  - platform: time_pattern
    hours: '/2'
    minutes: 0
    seconds: 0

https://www.home-assistant.io/docs/automation/trigger/#time-pattern-trigger

The linked post is over two years old. The drawback of referring to techniques described in old posts is that they’re often superseded by more modern techniques and/or have been deprecated.

For example, if I wanted a Time Trigger with multiple times, it can be written like this:

trigger:
  - platform: time
    at:
      - '00:00:00'
      - '01:35:00'
      - '03:15:00'
      - '06:10:00'

However, your application requires triggering at regular intervals (every 2 hours) so that’s best served by what tom_I has recommended: Time Pattern Trigger.

BTW, there’s a mistake in your automation’s action. The “Grow Tent Fan Off” is effectively a separate automation but its faulty indentation makes it part of the previous automation’s action. That’s invalid and should be corrected.

Hi Taras. Thanks for your input here. I took this into consideration when searching, and I figured that might have been part of the issue; but it was the closest topic I could find at the time that matched. The logic seemed correct, but it appears it’s the formatting that’s key here.

Thanks, Hellis81. Let me adjust the code here and retest.

What key words did you use when searching for an appropriate topic?

For example, if I search for “run automation every 2” the very first result (after your own) is a solved topic demonstrating the use of the Time Pattern Trigger.

For more information, refer to guideline 2 in the FAQ.

Thanks all for suggesting using a time pattern. I’ve set the time pattern for /2 hours (ignoring the 30-minute delay for now), and the config is valid. However, instead of turning the fan off after an hour the fan is now running constantly. I’ve restarted HA and the logs show that the automation is being triggered, but I’m not sure why the fan won’t turn off at say 13:00, 15:00, etc since those times aren’t divisible by 2.

That’s not how the time pattern trigger works. It does not stop your automation actions when not matched. It only starts them when it does match the pattern. You still need an “off” automation, like this one:

      - service: switch.turn_off
        target:
          entity_id: switch.grow_tent_fan

Thanks, Tom. That makes sense. Guess I made an assumption on my part.

How would I add this to the existing automation? Or would I need to create a separate automation to turn the fan off? Sorry, I’m new to HA so I’m still getting used to the nuances.

As you are new to this the easiest way is to create a new automation to turn the fan off.

However if you insist on making it one automation you could do it like this:

alias: Grow Tent Fan Schedule
description: ''
mode: single
trigger:
  - platform: time_pattern
    hours: '/2'
    minutes: 0
    seconds: 0
    id: 'on'
  - platform: state
    entity_id: switch.grow_tent_fan
    from: 'off'
    to: 'on'
    for: '00:30:00'
action:
  - service: >
      switch.turn_{{ 'on' if trigger.id == 'on' else 'off' }}
    target:
      entity_id: switch.grow_tent_fan

If…then…else makes sense here, thanks. When I added this to the YAML, HA returned this error:

Message malformed: required key not provided @ data['trigger'][0]['platform']

Can you share the config you are attempting to check please?

I copied and pasted what you had posted into the Triggers field.

alias: Grow Tent Fan Schedule
description: ''
mode: single
trigger:
  - platform: time_pattern
    hours: '/2'
    minutes: 0
    seconds: 0
    id: 'on'
  - platform: state
    entity_id: switch.grow_tent_fan
    from: 'off'
    to: 'on'
    for: '00:30:00'
action:
  - service: >
      switch.turn_{{ 'on' if trigger.id == 'on' else 'off' }}
    target:
      entity_id: switch.grow_tent_fan

What, all of it?

I’m not particularly familiar with the automation editor but you might have to manually enter the bits (platform, hours, etc…) separately into each field for the two triggers.

Or you can click on the three dots in the top right corner, select “Edit in YAML”, paste the whole thing and switch back to “Edit in visual editor”.

I tried that but “Edit in visual editor” is greyed out after you enter custom code. I ran into a similar error when adding custom code to another automation.

I guess the question here is what’s the standard way to enter code for automations in this scenario? Clearly it doesn’t seem to be the web interface.

It uses templates. You cant use the visual editor.

If you want to stick with the UI just create two separate automations, one for off and one for on:

alias: Grow Tent Fan Schedule On
description: ''
mode: single
trigger:
  - platform: time_pattern
    hours: '/2'
    minutes: 0
    seconds: 0
action:
  - service: switch.turn_on
    target:
      entity_id: switch.grow_tent_fan
alias: Grow Tent Fan Schedule Off
description: ''
mode: single
trigger:
  - platform: state
    entity_id: switch.grow_tent_fan
    from: 'off'
    to: 'on'
    for: '00:30:00'
action:
  - service: switch.turn_off
    target:
      entity_id: switch.grow_tent_fan

Thanks, Tom. I figured it out. I dropped the code into the automations.yaml file directly, replacing the existing automation entry, and adjusted the formatting. It’s working on the specified schedule now.

It appears that the UI won’t accept portions of the code, but it works just fine if you use Visual Studio code.