I took a while to read the different posts about counters, timers and expire and ended up trying a variation of a solution @sam posted in Timer progress/remaining in sitemap.
I want to have it so If my Wemo Plug is Turned ON and the Office Light is OFF a countdown from 60min is started at the end of which the Wemo is Turned OFF (just in case I never end up going into the room ie got distracted) but if the Office Lights are turned on before the end of the countdown the countdown is to be canceled. Also When the Office Lights are Turned OFF if the Wemo Plug is ON a countdown from 60min is started at the end of which the Wemo is Turned OFF. I’d also like to display the countdown in basicUI when it is running.
Here’s what I wrote and thought should work:
Color Hue_Office_Light "Office Light" <slider> { channel="hue:0210:00178825acf3:9:color", autoupdate="false" }
Number Enail_Timer "Timer duration"
Number Enail_Timer_Remaining "Minutes Left till Powering Off [%s]"
Switch Wemo_Enail_Switch "Wemo E-nail Switch" <poweroutlet_us> { channel="wemo:socket:Socket-1_0-221710K010472B:state" }
sitemap main label="The Sanctum Sanctorum"
{
Frame label="Office" {
Switch item=Wemo_Enail_Switch icon="poweroutlet_us"
Text item=Enail_Timer_Remaining visibility=[Enail_Timer<60] icon="clock"
Switch item=Hue_Office_Light label="Toggle" icon="light"
Slider item=Hue_Office_Light label="Brightness"
Colorpicker item=Hue_Office_Light label="Color" icon="colorpicker"
}
}
import org.openhab.core.library.types.*
import org.openhab.model.script.actions.*
import org.joda.time.*
var Timer timerEnail = null
rule "Initialize Enail switch on startup"
when
System started
then
sendCommand(Wemo_Enail_Switch, OFF)
postUpdate(Enail_Timer,60)
end
rule "Turn Enail Off after 1hr"
when
Item Wemo_Enail_Switch changed to ON
then
if(Hue_Office_Light.state == OFF) {
postUpdate(Enail_Timer,60)
val org.eclipse.xtext.xbase.lib.Functions$Function2 makeTimer = [
int myDelay,
org.eclipse.xtext.xbase.lib.Functions$Function2 makeTimer2 |
if(myDelay>0) {
postUpdate(Enail_Timer_Remaining, myDelay)
// to save processes I only update UI once a minute
timerEnail=createTimer(now.plusMinutes(1)) [|
postUpdate(Enail_Timer_Remaining, myDelay-1)
makeTimer2.apply(myDelay-1, makeTimer2)
]
}else {
// timer reach zero
postUpdate(Enail_Timer,60)
// what I want to happen when the timer stops
sendCommand(Wemo_Enail_Switch, OFF)
}
]
if(Hue_Office_Light.state == ON) {
timerEnail.cancel
timerEnail=null
postUpdate(Enail_Timer_Remaining,0)
postUpdate(Enail_Timer,60)
}
// extracts the value of the selection menu so you can set different times through the UI
var int int_Enail = (Enail_Timer.state as DecimalType).intValue
// updates the sitemap item straight away otherwise you have to wait 1 minute
postUpdate(Enail_Timer_Remaining, int_Enail)
// what I want to happen at the start of the timer
}
end
rule "Turn Enail Off 1hr after Light"
when
Item Hue_Office_Light changed to OFF
then
if(Wemo_Enail_Switch.state == ON) {
postUpdate(Enail_Timer,60)
val org.eclipse.xtext.xbase.lib.Functions$Function2 makeTimer = [
int myDelay,
org.eclipse.xtext.xbase.lib.Functions$Function2 makeTimer2 |
if(myDelay>0) {
postUpdate(Enail_Timer_Remaining, myDelay)
// to save processes I only update UI once a minute
timerEnail=createTimer(now.plusMinutes(1)) [|
postUpdate(Enail_Timer_Remaining, myDelay-1)
makeTimer2.apply(myDelay-1, makeTimer2)
]
}else {
// timer reach zero
postUpdate(Enail_Timer,60)
// what I want to happen when the timer stops
sendCommand(Wemo_Enail_Switch, OFF)
}
]
if(Hue_Office_Light.state == ON) {
timerEnail.cancel
timerEnail=null
postUpdate(Enail_Timer_Remaining,0)
postUpdate(Enail_Timer,60)
}
// extracts the value of the selection menu so you can set different times through the UI
var int int_Enail = (Enail_Timer.state as DecimalType).intValue
// updates the sitemap item straight away otherwise you have to wait 1 minute
postUpdate(Enail_Timer_Remaining, int_Enail)
// what I want to happen at the start of the timer
}
end