Update gidjit-hub.groovy
[smartapps.git] / official / lights-off-with-no-motion-and-presence.groovy
1 /**
2  *  Lights Off with No Motion and Presence
3  *
4  *  Author: Bruce Adelsman
5  */
6
7 definition(
8     name: "Lights Off with No Motion and Presence",
9     namespace: "naissan",
10     author: "Bruce Adelsman",
11     description: "Turn lights off when no motion and presence is detected for a set period of time.",
12     category: "Convenience",
13     iconUrl: "https://s3.amazonaws.com/smartapp-icons/Meta/light_presence-outlet.png",
14     iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Meta/light_presence-outlet@2x.png"
15 )
16
17 preferences {
18         section("Light switches to turn off") {
19                 input "switches", "capability.switch", title: "Choose light switches", multiple: true
20         }
21         section("Turn off when there is no motion and presence") {
22                 input "motionSensor", "capability.motionSensor", title: "Choose motion sensor"
23                 input "presenceSensors", "capability.presenceSensor", title: "Choose presence sensors", multiple: true
24         }
25         section("Delay before turning off") {                    
26                 input "delayMins", "number", title: "Minutes of inactivity?"
27         }
28 }
29
30 def installed() {
31         subscribe(motionSensor, "motion", motionHandler)
32     subscribe(presenceSensors, "presence", presenceHandler)
33 }
34
35 def updated() {
36         unsubscribe()
37         subscribe(motionSensor, "motion", motionHandler)
38     subscribe(presenceSensors, "presence", presenceHandler)
39 }
40
41 def motionHandler(evt) {
42         log.debug "handler $evt.name: $evt.value"
43         if (evt.value == "inactive") {
44                 runIn(delayMins * 60, scheduleCheck, [overwrite: false])
45         }
46 }
47
48 def presenceHandler(evt) {
49         log.debug "handler $evt.name: $evt.value"
50         if (evt.value == "not present") {
51                 runIn(delayMins * 60, scheduleCheck, [overwrite: false])
52         }
53 }
54
55 def isActivePresence() {
56         // check all the presence sensors, make sure none are present
57         def noPresence = presenceSensors.find{it.currentPresence == "present"} == null
58         !noPresence             
59 }
60
61 def scheduleCheck() {
62         log.debug "scheduled check"
63         def motionState = motionSensor.currentState("motion")
64     if (motionState.value == "inactive") {
65         def elapsed = now() - motionState.rawDateCreated.time
66         def threshold = 1000 * 60 * delayMins - 1000
67         if (elapsed >= threshold) {
68                 if (!isActivePresence()) {
69                 log.debug "Motion has stayed inactive since last check ($elapsed ms) and no presence:  turning lights off"
70                 switches.off()
71             } else {
72                 log.debug "Presence is active: do nothing"
73             }
74         } else {
75                 log.debug "Motion has not stayed inactive long enough since last check ($elapsed ms): do nothing"
76         }
77     } else {
78         log.debug "Motion is active: do nothing"
79     }
80 }