Advertisement
__Dave__

OH - Motionssensor with Timer

Jan 5th, 2019
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.58 KB | None | 0 0
  1. /*
  2. Naming convention:
  3. - Motiondetector-items: <ItemToSwitch>Motion
  4. - Timer-items: <ItemToSwitch>Timer
  5.  
  6. Requirements:
  7. - Timer-items use expire-binding for timeout with "command=OFF"
  8. - Astro-Binding used for "Sunset_Time" and "Sunrise_Time"
  9. - Group "gMotionDetectors" contains every motion-detector item
  10. - Group "gMotionTimers" contains every timer item
  11. - Group "gMotionTriggers" contains every item to switch
  12.  
  13. For triggering multiple items work with group for <ItemToSwitch>.
  14. Same goes for multiple triggering motion detectors.
  15. */
  16.  
  17. import java.util.concurrent.locks.ReentrantLock
  18.  
  19. var file = "motion.rules"
  20. var ReentrantLock lock = new ReentrantLock()
  21.  
  22. rule "Motion detected"
  23. when
  24.     Member of gMotionDetectors received update
  25. then
  26.     logInfo(file, "Motion detector " + triggeringItem.name + " changed to " + triggeringItem.state)
  27.     var sunset = new DateTime(Sunset_Time.state.toString)
  28.     var sunrise = new DateTime(Sunrise_Time.state.toString)
  29.     if (triggeringItem.state.toString() == "ON" && (sunrise.isAfterNow() || sunset.isBeforeNow())) {
  30.         var gotLock = new Boolean(lock.tryLock())
  31.         try {
  32.             if(gotLock){
  33.                 var ItemName = triggeringItem.name.replace("Motion", "")
  34.                 var ItemTimer = gMotionTimers.members.findFirst[name.equals(ItemName + "Timer")]
  35.                 var ItemToSwitch = gMotionTriggers.members.findFirst[name.equals(ItemName)]
  36.                 logInfo(file, "Switching item: " + ItemToSwitch.name + " / State: " + ItemToSwitch.state)
  37.                 logInfo(file, "Timer item: " + ItemTimer.name)
  38.                 if(ItemToSwitch.state == OFF) {
  39.                     ItemTimer.postUpdate(OFF)
  40.                     ItemTimer.sendCommand(ON)
  41.                     ItemToSwitch.sendCommand(ON)
  42.                 } else if(ItemTimer.state == ON) {
  43.                     logInfo(file, "Item already on -> restarting timer")
  44.                     ItemTimer.postUpdate(OFF)
  45.                     ItemTimer.sendCommand(ON)
  46.                 } else {
  47.                     logInfo(file, "Item switched on manually -> not setting timer")
  48.                 }
  49.             }
  50.         }
  51.         finally{
  52.             if(gotLock){
  53.                 lock.unlock()
  54.             }
  55.         }
  56.     }
  57. end
  58.  
  59. rule "Motion timers expired"
  60. when
  61.     Member of gMotionTimers received command OFF
  62. then
  63.     logInfo(file, "Timer expired: " + triggeringItem.name)
  64.     var ItemToSwitch = gMotionTriggers.members.findFirst[name.equals(triggeringItem.name.replace("Timer",""))]
  65.     logInfo(file, "Switching item: " + ItemToSwitch.name)
  66.     ItemToSwitch.sendCommand(OFF)
  67. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement