2 * Lights Off with No Motion and Presence
4 * Author: Bruce Adelsman
8 name: "Lights Off with No Motion and Presence",
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"
18 section("Light switches to turn off") {
19 input "switches", "capability.switch", title: "Choose light switches", multiple: true
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
25 section("Delay before turning off") {
26 input "delayMins", "number", title: "Minutes of inactivity?"
31 subscribe(motionSensor, "motion", motionHandler)
32 subscribe(presenceSensors, "presence", presenceHandler)
37 subscribe(motionSensor, "motion", motionHandler)
38 subscribe(presenceSensors, "presence", presenceHandler)
41 def motionHandler(evt) {
42 log.debug "handler $evt.name: $evt.value"
43 if (evt.value == "inactive") {
44 runIn(delayMins * 60, scheduleCheck, [overwrite: false])
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])
55 def isActivePresence() {
56 // check all the presence sensors, make sure none are present
57 def noPresence = presenceSensors.find{it.currentPresence == "present"} == null
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"
72 log.debug "Presence is active: do nothing"
75 log.debug "Motion has not stayed inactive long enough since last check ($elapsed ms): do nothing"
78 log.debug "Motion is active: do nothing"