Alert when cat stuck in closet


(Rob) #1

I am trying to get into coding my own Smart App, but family, job, and life in general have hampered my progress. My idea is simple enough: alert me when I accidentally lock my sneaky cats in the closet. I have a motion sensor in the closet, and I have a contact sensor on the door. I would think it would be relatively trivial to say “alert me when there’s motion in the closet, but only when the door is closed.” Does something like this already exist? Thanks in advance.


(Micheal ) #2

Have you confirmed the motion sensor is sensitive enough to register the cats?

And how would you want the alert (SMS, Push, etc)?


#3

If the sensor is indeed sensitive enough to see the cat then I’d recommend @JoeC’s rule builder.

If motion is detected and door is closed, notify “there’s a cat in the closet lol”


(Rob) #4

The sensors definitely register the cats. I experimented by allowing them into the closet and watching the motion detector fire when they more around. A push notification would be ideal.


(Rob) #5

I actually have this rule working in Simple Rule Builder, but I’m trying to learn what the code for this would look like, and would also prefer for it to run natively in SmartThings without having to rely on a 3rd party integration.


(Micheal ) #6

I can whip this out tomorrow. I can share the code so you can learn from it…how does that sound?


(Jody) #7

I got something I think will work for you. If you want to try it, it is available in the shared code repository. @d2htornado

   /**
 *  Copyright 2015 Jody Albritton
 *
 *  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 *  in compliance with the License. You may obtain a copy of the License at:
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
 *  on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
 *  for the specific language governing permissions and limitations under the License.
 *
 *  Motion When Closed
 *
 *  Author: Jody Albritton
 *  Date: 2015-07-30
 *  Version: 1.0
 * Change Log:
 */
definition(
    name: "Motion When Closed",
    namespace: "jodyalbritton",
    author: "jodyalbritton",
    description: "Get a push notification or text message when motion occurs in a closed off area.",
    category: "Convenience",
    iconUrl: "https://s3.amazonaws.com/smartapp-icons/Meta/window_contact.png",
    iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Meta/window_contact@2x.png"
)

preferences {
	section("When these doors are closed"){
		input "contacts", "capability.contactSensor", title: "Closed Doors", required: true, multiple: true
	}
    section("And this motion sensor is triggered"){
        input "motions", "capability.motionSensor", title: "Motion Here", required: true, multiple: true
    }
	section("Send this message (optional, sends standard status message if not specified)"){
		input "messageText", "text", title: "Message Text", required: false
	}
	section("Via a push notification and/or an SMS message"){
        input("recipients", "contact", title: "Send notifications to") {
            input "phone", "phone", title: "Phone Number (for SMS, optional)", required: false
            input "pushAndPhone", "enum", title: "Both Push and SMS?", required: false, options: ["Yes", "No"]
        }
	}
	section("Minimum time between messages (optional, defaults to every message)") {
		input "frequency", "decimal", title: "Minutes", required: false
	}
}

def installed() {
	log.debug "Installed with settings: ${settings}"
	subscribeToEvents()
}

def updated() {
	log.debug "Updated with settings: ${settings}"
	unsubscribe()
	subscribeToEvents()
}

def subscribeToEvents() {
	subscribe(motions, "motion.active", eventHandler)
}

def eventHandler(evt) {
	log.debug "Notify got evt ${evt}"
    def areClosed = doorsClosed()
    log.debug areClosed
    if (areClosed == true){
        if (frequency) {
            def lastTime = state[evt.deviceId]
            if (lastTime == null || now() - lastTime >= frequency * 60000) {
                sendMessage(evt)
            }
        }
        else {
            sendMessage(evt)
        }
    }
}

def listContacts() {
     contacts?.collect{[type: "contact", id: it.id, name: it.displayName, status: it.currentValue('contact')]}?.sort{it.name}
}

private sendMessage(evt) {
	def msg = messageText ?: defaultText(evt)
	log.debug "$evt.name:$evt.value, pushAndPhone:$pushAndPhone, '$msg'"

    if (location.contactBookEnabled) {
        sendNotificationToContacts(msg, recipients)
    }
    else {

        if (!phone || pushAndPhone != "No") {
            log.debug "sending push"
            sendPush(msg)
        }
        if (phone) {
            log.debug "sending SMS"
            sendSms(phone, msg)
        }
    }
	if (frequency) {
		state[evt.deviceId] = now()
	}
}

def doorsClosed(){
    def closed = false
		contacts.each { contact ->
        	def isClosed = contact.currentValue('contact')
        	if (isClosed == "closed" ) {
        		closed = true
             } else {
             	closed = false
             }
            }
    return closed
}

private defaultText(evt) {

		evt.descriptionText + "in a closed area"
}

(Rob) #8

@MichaelS, that’s very kind. Thank you so much.


(Rob) #9

Thanks, @jodyalbritton! I’ll definitely try this out.


(Rob) #10

Wow, my brain is exploding. It’s been a very long time since I’ve had to do any sort of programming, and I’ve only begun diving into the SmartThings documentation. In what I was originally trying to do, I subscribed to both motion and door events. It appears that this only subscribes to motion events. Is that correct?

Thanks again, this is very helpful.


(Jody) #11

All I subscribe to were the motion events. Then it loops through all of the doors you select and makes sure they are indeed closed. This would also work if you had an area with two doors that a pet could get trapped in. Or if you wanted to be alerted when someone went into a room that was closed. So, no you do not need the opening and closing of the doors for this work.


(Jody) #12

@d2htornado check out this live coding session that is going on right now.


(Micheal ) #13

Sounds like you are taken care of…this could would certainly work for you!


(Rob) #14

Definitely, thanks to you both for the help. Turns out I was going about it all wrong and am still learning.

BTW, @jody.albritton I sat in on part of the live coding event. Will have to watch the rest later, but that’s a really helpful resource. Very nice to watch and have someone explaining as it’s happening.