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