Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Naming convention:
- - Motiondetector-items: <ItemToSwitch>Motion
- - Timer-items: <ItemToSwitch>Timer
- Requirements:
- - Timer-items use expire-binding for timeout with "command=OFF"
- - Astro-Binding used for "Sunset_Time" and "Sunrise_Time"
- - Group "gMotionDetectors" contains every motion-detector item
- - Group "gMotionTimers" contains every timer item
- - Group "gMotionTriggers" contains every item to switch
- For triggering multiple items work with group for <ItemToSwitch>.
- Same goes for multiple triggering motion detectors.
- */
- import java.util.concurrent.locks.ReentrantLock
- var file = "motion.rules"
- var ReentrantLock lock = new ReentrantLock()
- rule "Motion detected"
- when
- Member of gMotionDetectors received update
- then
- logInfo(file, "Motion detector " + triggeringItem.name + " changed to " + triggeringItem.state)
- var sunset = new DateTime(Sunset_Time.state.toString)
- var sunrise = new DateTime(Sunrise_Time.state.toString)
- if (triggeringItem.state.toString() == "ON" && (sunrise.isAfterNow() || sunset.isBeforeNow())) {
- var gotLock = new Boolean(lock.tryLock())
- try {
- if(gotLock){
- var ItemName = triggeringItem.name.replace("Motion", "")
- var ItemTimer = gMotionTimers.members.findFirst[name.equals(ItemName + "Timer")]
- var ItemToSwitch = gMotionTriggers.members.findFirst[name.equals(ItemName)]
- logInfo(file, "Switching item: " + ItemToSwitch.name + " / State: " + ItemToSwitch.state)
- logInfo(file, "Timer item: " + ItemTimer.name)
- if(ItemToSwitch.state == OFF) {
- ItemTimer.postUpdate(OFF)
- ItemTimer.sendCommand(ON)
- ItemToSwitch.sendCommand(ON)
- } else if(ItemTimer.state == ON) {
- logInfo(file, "Item already on -> restarting timer")
- ItemTimer.postUpdate(OFF)
- ItemTimer.sendCommand(ON)
- } else {
- logInfo(file, "Item switched on manually -> not setting timer")
- }
- }
- }
- finally{
- if(gotLock){
- lock.unlock()
- }
- }
- }
- end
- rule "Motion timers expired"
- when
- Member of gMotionTimers received command OFF
- then
- logInfo(file, "Timer expired: " + triggeringItem.name)
- var ItemToSwitch = gMotionTriggers.members.findFirst[name.equals(triggeringItem.name.replace("Timer",""))]
- logInfo(file, "Switching item: " + ItemToSwitch.name)
- ItemToSwitch.sendCommand(OFF)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement