So I want to develop a routine a rule to basically turn on and off a glade connected to a smart plug. Basically when i toggle it on, i want it to turn on and off every two hours. I dont need the code per se to turn it off every two hours, but i do need the code to add the rule to a site map.
Ive looked all over the site and i am not finding it. maybe not using the right search words.
ill put in one of my existing rules here:
rule “Turn on Jenn’s Office”
when
Time cron “0 0 7 ? * MON,TUE,WED,THU,FRI *”
// Time cron “0 0/1 * 1/1 * ? *”
then
Jenns_Devices.sendCommand(ON)
Thread::sleep(5000)
Jenns_Work_Devices.sendCommand(ON)
end
ok say I want to turn on and off the plug every two hours rather than what I have above. i know i can run a timer to do this, what im afraid of is wont this stop the rule from executing again? is there a way to do this:
Toggle on
Rule Executes, turn device on for two hours, then turn device off for two hours, then back on for two hours, and off for two hours, until I stop it completely.
If rossko57 did the right suggestion, the code is simple:
var Timer tToggle = null
rule "toggle frequently"
when
Item myTriggerItem changed
then
tToggle?.cancel
if(myTriggerItem.state == ON)
tToggle = createTimer(now.plusMillis(200),[ |
myTogglingItem.sendCommand(if(myTogglingItem.state != ON) ON else OFF)
tToggle.reschedule(now.plusHours(2))
])
end
When the rule gets triggered (because the Item myTriggerItem changed its state), the first thing is, an existing timer is killed. The second step is, to create a new timer, but only if the current state is ON.
The timer will expire almost immediately and will toggle the Item myTogglingItem. Then the timer will reschedule itself two hours later.
Please be aware that myTogglingItem will keep its state when myTriggerItem is switched OFF, so the only thing which is switched, is the toggling!
something isnt clicking for me, how do i provide an enable switch that isnt tied to a device? I have searched the forums before i even asked the original question.