Garage security/motion sensor if door closed


(ugly) #1

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"
}

}


(Michael Langkilde) #2

This can all be solved by using the ST multisensor and doing a notify me when there is movement. I put the sensor right near the top where the Garage Door Opener attaches to the garage door due to knowing other friends and people having someone break into their garage door by pushing in on the door and unlatching the lever so the garage door is free to open and released from the motor. I have a siren in the garage that will go on during certain modes and there is movement on the garage door. So far it has been very nice to have and preempts actually setting of the home security and hopefully to discourage and scare anyone that is trying to open my garage.

If someone has broken through a window it would be interesting to see how much movement is caused on the garage door to see if ST multisensor senses that movement.


(ugly) #3

Hi,

Thanks i would like to write the code. Any suggestions for that?

Thanks,
Chris