Set SleepTimer with the possibility to abort

243 views
Skip to first unread message

Christoph Wempe

unread,
Jul 13, 2013, 8:45:53 PM7/13/13
to ope...@googlegroups.com
I want to creat a switch which activates a rule to send an Power OFF command to my avr after some minutes.

I used this rule from the samples-wiki.

I testet it with 10 seconds and the mute-command and it works.

var Timer timer
rule "Onkyo Zone2 go to sleep"
    when
        Item onkyoZ2GoToSleep changed from OFF to ON
    then
        //Time in Seconds
        var Number sleepTimer = 10
        logInfo("onkyoZone2", "Zone2 goes to sleep in " + sleepTimer + " seconds ...")
         if(onkyoZ2GoToSleep.state==ON) {
            timer = createTimer(now.plusSeconds(sleepTimer)) [|
                logInfo("onkyoZone2", "Zone2 goes now to sleep.")
                sendCommand(onkyoMute2, ON)
                logInfo("onkyoZone2", "Set onkyoZ2GoToSleep back to OFF.")
                sendCommand(onkyoZ2GoToSleep, OFF)
            ]
        } else {
            if(timer!=null) {
                logInfo("onkyoZone2", "Zone2 does NOT go to sleep.")
                timer.cancel
                timer = null
                logInfo("onkyoZone2", "Set onkyoZ2GoToSleep back to OFF.")
                sendCommand(onkyoZ2GoToSleep, OFF)
                }
        }
end

After two seconds I changed the onkyoZ2GoToSleep back to OFF.
My understanding was this would abort the rule and prevent openhab from sending the mute-command.
But it didn't.

Log:
02:41:38.453 DEBUG o.o.i.r.i.r.ItemResource[:200]- Received HTTP POST request at 'items/onkyoZ2GoToSleep' with value 'ON'.
02:41:38.457 DEBUG o.o.m.r.i.engine.RuleEngine[:305]- Executing rule 'Onkyo Zone2 go to sleep'
02:41:38.507 INFO  o.o.model.script.onkyoZone2[:73]- Zone2 goes to sleep in 10 seconds ...
02:41:41.038 DEBUG o.o.i.r.i.r.ItemResource[:200]- Received HTTP POST request at 'items/onkyoZ2GoToSleep' with value 'OFF'.
02:41:48.550 INFO  o.o.model.script.onkyoZone2[:73]- Zone2 goes now to sleep.
02:41:48.576 DEBUG o.o.b.o.internal.OnkyoBinding[:135]- Received command (item='onkyoMute2', state='ON', class='class org.openhab.core.library.types.OnOffType')
02:41:48.585 INFO  o.o.model.script.onkyoZone2[:73]- Set onkyoZ2GoToSleep back to OFF.




dong...@gmail.com

unread,
Jul 14, 2013, 12:47:27 PM7/14/13
to ope...@googlegroups.com
You need to check inside your timer if the Switch is still ON or OFF.


dong...@gmail.com

unread,
Jul 14, 2013, 12:58:26 PM7/14/13
to ope...@googlegroups.com, dong...@gmail.com
And for the sake of completeness, better try something like this (not tested but it should hopefully work):

var Timer timer
rule "Onkyo Zone2 go to sleep"
    when
        Item onkyoZ2GoToSleep changed
    then
      switch(onkyoZ2GoToSleep.state)
      {
case ON:
 var Number sleepTimer = 10
                timer = createTimer(now.plusSeconds(sleepTimer)) [|
                  logInfo("onkyoZone2", "Zone2 goes now to sleep.")
                  sendCommand(onkyoMute2, ON)
                  logInfo("onkyoZone2", "Set onkyoZ2GoToSleep back to OFF.")
                  sendCommand(onkyoZ2GoToSleep, OFF)
                ]
case OFF:

Christoph Wempe

unread,
Jul 14, 2013, 1:58:12 PM7/14/13
to ope...@googlegroups.com, dong...@gmail.com
Ahh.
Now I understand the timer command. :)
"timer.cancel" cancels the running timer which may still be running in the background.

Thanks

Thomas Eichstädt-Engelen

unread,
Jul 14, 2013, 3:52:22 PM7/14/13
to ope...@googlegroups.com
so your problem is solved?

--
You received this message because you are subscribed to the Google Groups "openhab" group.
To unsubscribe from this group and stop receiving emails from it, send an email to openhab+u...@googlegroups.com.
To post to this group, send email to ope...@googlegroups.com.
Visit this group at http://groups.google.com/group/openhab.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Christoph Wempe

unread,
Jul 15, 2013, 3:31:25 PM7/15/13
to ope...@googlegroups.com
Yes.
I finally got it working.

But I had to use the "if(onkyoZ2GoToSleep.state==ON)" like in the example.

The "switch(onkyoZ2GoToSleep.state)"-command didn't work.
Every change of state resulted in executing the "case ON:" commands.
But maybe I did something wrong. :)
Reply all
Reply to author
Forward
0 new messages