I thought this would be a simple automation. I want to turn a smart switch on and off for set a duration. Say, one hour off and a half-hour on. This would repeat or loop indefinitely as long as the automation is enabled.
I thought I had it figured out; two automations. Each would have a state trigger with a to: 'off' or to: 'on'. It would also have a for: '1:00:00' or for: '0:30:00' to establish the duration. A state change either way would trigger one or the other, but only after waiting the for: duration.
Sometimes it works, sometimes the automations toggle the switch twice or three times.
Backstory:
I bought one of those plug-in air fresheners. It’s overpowering! I had a couple of Sengled smart plugs I’ve been testing and figured I’d use one of those and let HA cycle the air freshener on and off to lower the intensity. Obviously this isn’t critical. But I’d like to learn why it’s not working.
Here’s an example of toggling the switch three times:
id: id_air_freshener_on
alias: Air Freshener On
description: Air Freshener On
trigger:
- platform: state
entity_id: switch.sengled_e1c_nb7_e08a6b03_on_off
for: '1:00:00'
to: 'off'
condition: []
action:
- service: switch.turn_on
target:
entity_id: switch.sengled_e1c_nb7_e08a6b03_on_off
mode: single
Maybe I’m missing something. I want this automation to turn the switch on after it’s been off for one hour, and leave it that way.
There’s a second automation which is supposed to turn the switch off after its been on for one half-hour. It’s identical except swap “off” and “on,” and change 1:00:00 to “0:30:00”
They both run when expected, except they sometimes turn the switch on/off repeatedly, as shown in the OP, instead of just turning it on (or off) once, as I expected.
I do like the solution @petro posted. Much simpler. But it forces me to keep it on 50% of the time. I could probably live with that, but now that I’ve bumped into this problem where the action takes place two or three times, I’m curious what’s going on.
I’m still getting multiple triggers in my automation.
Maybe I need to explain the problem better. My automation calls the switch.turn_on service, but when it’s triggered, the switch is sometimes turned on, then immediately off. Sometimes it’s turned on, then off, then back on. Other times it’s only turned on once, as expected.
Likewise, an almost identical automation calls the switch.turn_off service. Again, it sometimes turns the switch off then on, or off then on then off.
My question is, what is causing one automation to toggle the switch more than once, instead of just turning it on or off once (depending on which service I call)?
I gave up on my original code. I tried deleting and re-adding the automation. Changed the duration. Stopped and restarted HA. Even updated the HA version. I’d still get apparently random cases when the automation would get triggered once, but the action wold run two or three times.
For my purposes, a half-hour on and a half-hour off works OK, so I’m going with the solution @petro posted (thank you!!)
I confirmed that minutes: 30 means “on the hour and half-hour,” not “every thirty minutes.” The documentation says you can use minutes: '/30' and it will run every 30 minutes, but when I did that it’s still triggering on the hour and half-hour.
Some day I’ll come back to this, and if I can’t solve it myself I’ll define the problem better in my thread title.
It’s a common misconception that using /5 implies every 5 minutes, starting from the moment the automation is created/started. That’s false.
If the current hour is 13 then it will trigger at 13:05, 13:10, 13:15, etc. If the automation is created at 13:02, it will trigger at 13:05 (not 13:07).
Yes, I discovered that, thank you for confirming it’s not just me.
For the record, this is what the documentation says:
automation:
trigger:
- platform: time_pattern
# Matches every hour at 5 minutes past whole
minutes: 5
automation 3:
trigger:
- platform: time_pattern
**# You can also match on interval. This will match every 5 minutes**
minutes: **"/5**"
Here’s the line of code that’s responsible for computing a Time Pattern’s repeating interval:
When you use this:
- platform: time_pattern
minutes: '/5'
that line of python code (line 223) does this with it:
[x for x in range(0, 59 + 1) if x % 5 == 0]
which produces this:
[0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55]
That list contains the minutes in an hour when the Time Pattern Trigger will trigger. Basically, it uses the 5 minutes you supplied to calculate 5-minute intervals in an hour (starting at the beginning of the hour).
To gain a better understanding of what this means, I used python3 to produce results for 5, 10, 15, 20, 25, 30, 35, 40, and 45 minutes.
I tested it with a shorter cycle and confirmed it works.
During testing, the stumbling block I encountered was the value used in the timedelta. It needs to be just under the desired time period in order for the time comparison to work properly.
So if the desired period is in minutes, make the value in timedelta one or two minutes less than the desired time period (if in seconds then reduce by a second or two). You wanted a period of 30 minutes ON and 60 minutes OFF so the values are 29 and 59, respectively.
Reminder: this Time Pattern Trigger is using /30 minutes so it will trigger on the hour (like 12:00) and the half-hour (12:30).
Thank you for this, it saved me from headache. After trying to get along with two separate automation rules for on and off, this seems to work perfectly.
I modified it to run every 1 minute and check if time has come to toggle the device. The duty cycle is controlled by a slider (from 0 to 100). I also added a variable to set full length of the cycle.