Hi. I am trying to write and app to let me know if there’s motion in the garage IF the door is closed. This will basically tell me if some has broken in through the window. So far all I’m getting is an exception. I think it’s close. Can you please help? Thanks
/**
- Text Me When There’s Motion and door is closed
- Author: SmartThings
*/
definition(
name: “Text Me When There’s Motion and door is closed”,
namespace: “smartthings”,
author: “SmartThings”,
description: “Send a text message when there is motion while you are away.”,
category: “Convenience”,
iconUrl: “https://s3.amazonaws.com/smartapp-icons/Meta/intruder_motion-presence.png”,
iconX2Url: “https://s3.amazonaws.com/smartapp-icons/Meta/intruder_motion-presence@2x.png”
)
preferences {
section(“When there’s movement…”) {
input “motion1”, “capability.motionSensor”, title: “Where?”
}
section(“When the door is closed…”) {
input “contact1”, “capability.contactSensor”, title: “Where?”
}
section(“Text me at…”) {
input “phone1”, “phone”, title: “Phone number?”
}
}
def installed() {
subscribe(motion1, “motion.active”, motionActiveHandler)
subscribe(contact1, “contact.closed”, contactClosedHandler)
}
def updated() {
unsubscribe()
subscribe(motion1, “motion.active”, motionActiveHandler)
subscribe(contact1, “contact.closed”, contactClosedHandler)
}
def contactClosedHandler(evt) {
def latestThreeAxisState = multisensor.threeAxisState // e.g.: 0,0,-1000
if (latestThreeAxisState) {
def isClosed = Math.abs(latestThreeAxisState.xyzValue.z) > 250 // TODO: Test that this value works in most cases…
if (!isClosed) {
clearSmsHistory()
clearStatus()
}
}
}
def motionActiveHandler(evt) {
log.trace “$evt.value: $evt, $settings”
if (contact1.latestValue("closed") == "closed") {
// Don't send a continuous stream of text messages
def deltaSeconds = 10
def timeAgo = new Date(now() - (1000 * deltaSeconds))
def recentEvents = motion1.eventsSince(timeAgo)
log.debug "Found ${recentEvents?.size() ?: 0} events in the last $deltaSeconds seconds"
def alreadySentSms = recentEvents.count { it.value && it.value == "active" } > 1
if (alreadySentSms) {
log.debug "SMS already sent to $phone1 within the last $deltaSeconds seconds"
} else {
log.debug "$motion1 has moved while you were out, texting $phone1"
sendSms(phone1, "${motion1.label} ${motion1.name} moved while you were out")
}
} else {
log.debug "Motion detected, but sensor indicates door is open"
}
}