I am trying to write what i thought was a simple smartapp. When a motion sensor is tripped and there is only 1 person home (mobile presence sensors) turn off selected lights.
When testing it in the simulator with simulated devices, the motionSensor event doesn’t trigger every time. Actually it seems to only trigger once or twice. Is this the simulator or is something wrong with my code?
see code below:
preferences {
section("When only 1 of these people are home") {
input "people", "capability.presenceSensor", title: "People", multiple: true
}
section("And Motion is detected from") {
input "motionSensor", "capability.motionSensor", title: "Choose sensor(s)", multiple: true
}
section("Turn off These Lights") {
input "switches", "capability.switch", title: "Choose lights", multiple: true
}
}
def installed() {
subscribeToEvents()
}
def updated() {
unsubscribe()
subscribeToEvents()
}
def subscribeToEvents() {
subscribe(motionSensor, "motion", motionHandler)
}
def motionHandler(evt) {
log.debug "motionHandler: This event name is ${evt.name}, the value is ${evt.value}"
if("active" == evt.value) {
def countPeople = people.findAll { person ->
def presenceState = person.currentState("presence")
}
if (countPeople.size() == 1) {
switches.off()
}
log.debug "Motion Detected! Turning off lights becasue ${countPeople.size()} out of ${people.size()} person(s) are home"
} else if("inactive" == evt.value) {
log.debug "Motion Stopped. ${countPeople.size()} out of ${people.size()} person(s) are home"
}
}