[SOLVED] Toggle switch for rule for Glade Plugins

Tags: #<Tag:0x00007f2fbb3afad8>

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.

Rules do not go in the sitemap.

is there any way i can do what i need to do then?

Your question is not clearly stated and we have no configuration pieces on which to base any advice.

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.

Does that help?

Please use code fences. Spacing is important un rule configuration.

Perhaps we can rephrase that?

  1. I want a switch on my sitemap.
  2. When I turn it on, I want it to trigger a rule that turns on a smartplug AND starts 3) a timer to turn it off again in two hours.
  3. In two hours time, turn off the smartplug AND check if that switch is still on.
    If it is, start another two hour timer.
  4. In two hours time, check if that switch is still on and if it is do (2) again.
  5. if at any time I turn the switch off, I want it to turn the smartplug off and cancel any timer.
1 Like

thank you

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!

1 Like

thank you greatly for this, i will test it out :slight_smile:

Looking at the code, can you use the same item for myTriggerItem and myTogglingItem?

Not if you want to provide an “enable” switch on the UI and the other to flip-flip on and off under timer control.

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.

You could create a Switch type Item, and put it on your UI. That’s it. An Item without any connection to a real device.

You may have been creating Items so far with PaperUI or with files.

1 Like

i figured it out, thank you all for your time.