I would use the Expire binding and Associated Items (which you already are using).
- Still create your new Item with “Timer_” appended to the name. Make these Item Switches and bind these Items to the Expire binding.
- Add your light Swtiches and Dimmers to the same group, I’ll assume it is gLights below.
Group gLightTimers
Switch Timer_SwitchFrontFloods_Switch (gLightTimers) { expire="5m,command=OFF" }
Switch Timer_SwitchDownstairsHall_Dimmer (gLightTimers) { expire="10m,command=OFF" }
The rules become very simple:
rule "gLights received update"
when
Item SwitchFrontFloods_Switch received command ON or
Item SwitchDownstairsHall_Dimmer received command ON
then
Thread::sleep(100) // may be required for persistence to catch up
val light = gLights.members.filter[l | l.lastUpdate != null].sortBy[lastUpdate].last
val timer = gLightsTimers.members.filter[t | t.name == "Timer_"+light.name].head
timer.sendCommand(ON)
end
rule "Timer went OFF"
when
Item Timer_SwitchFrontFloods_Switch received command OFF or
Item Timer_SwitchDownstairsHall_Dimmer received command OFF
then
Thread::sleep(100) // may be required for persistence to catch up
val timer = gLightsTimers.members.filter[t | t.lastUpdate != null].sortBy[lastUpdate].last
val light = val light = gLights.members.filter[l | "Timer_"+l.name == timer.name].sortBy[lastUpdate].last
light.sendCommand(OFF)
end
If these lights will always be timer based (i.e. there is no way/desire to turn them on and have them remain ON) you don’t even need the rules. Just add the Expire binding to the Light Switch and Dimmer Items directly.
The only limitation is the timeout is hardcoded rather than based on an Item you can change on your sitemap.
Check and check. These rules do that too.
You can change the .rules file all you want with this configuration. But if you change the .items files your timers will be wiped out.
A better approach would be to use the Joda DateTimes anyway. Store now.plusMinutes(15)
as your timeoutTime then to check you see if(now.isAfter(timeoutTime))
or just pass `timeoutTime to the timer directly:
createTimer(now.plusMinutes(15), [| ...
This isn’t Java. Rules are a Domain Specific Language written on top of the Xtext language which runs inside of Java. You have access to some Java libraries but the syntax is decidely not Java.