Garage door notifier at night (WTIH CODE)

garage

(Russ Pearlman) #1

Hey all, I had a relative of mine ask for an app to notify him if the garage door is left open after sunset and before sunrise. He did the research but couldn’t find anything, so I went ahead and wrote him a SmartApp. I’ve decided to share it with everyone in case they find it useful.

Enjoy!
Russ

/**
 *  Garage Notifier
 *
 *  Copyright 2016 Russ Pearlman
 *
 *  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.
 *
 */
definition(
name: "Garage Door Notifier v5",
namespace: "russbp",
author: "Russ Pearlman",
description: "Notify on garage door being left open at night",
category: "My Apps",
iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png",
iconX3Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png")

preferences
{
section("Which garage door?") 
{
    input "door1", "capability.garageDoorControl"
}

section("Notify after how many minutes?") 
{
    input "minutesLater", "number", title: "Delay?"
}

section("Notifications") 
{
	    input "sendPushMessage", "enum", title: "Send a push notification?", metadata:[values:["Yes", "No"]], required: false
	    input "phoneNumber", "phone", title: "Enter phone number to send text notification.", required: false
}
}

def installed()
{
log.debug "Garage Notifier installed. (URL: http://www.github.com/smartthings-users/smartapp.auto-lock-door)"
initialize()
}

def updated()
{
unsubscribe()
unschedule()
log.debug "Garage Notifier updated."
initialize()
}

def initialize()
{
log.debug "Settings: ${settings}"
subscribe(door1, "door", doorHandler)
subscribe(location, "sunset", sunsetHandler)
}

private isItNight()
{
	def sunRise = getSunriseAndSunset().sunrise.time + 24*60*60*1000
def sunSet = getSunriseAndSunset().sunset.time
def nowTime = now()
	log.debug "Sunrise: ${sunRise}."
log.debug "Sunset: ${sunSet}."
log.debug "Now: ${nowTime}."
log.debug "The time zone for this location is: ${location.timeZone}"
log.debug "The zip code for this location: ${location.zipCode}"

	if(nowTime > sunSet && nowTime < sunRise)
{
        	return true
        }
        else {
        	return false
        }
  }
  
def doorHandler(evt)
{
	//OPEN
if(evt.value == "open")
{
    log.debug "${door1} open."
    
    //If at night, schedule notification to run after configured minutes delay  
    if(isItNight())
    {
    	log.debug "At night!"
        def delay = minutesLater * 60
        log.debug "Setting notification in ${minutesLater} minutes (${delay} seconds)."
        runIn (delay, garageLeftOpen)
    }
    else
    	log.debug "Not at night!"
}

//CLOSED
if(evt.value == "closed")
{
    log.debug "${door1} closed."
    log.debug "Cancelling previous notify task (if any)..."

    //Unschedule any notifications since it closed
    unschedule( garageLeftOpen )   
}
}


def sunsetHandler(evt) 
{
log.debug "Sun has set!"

//At sunset, schedule notification if door is open
if(door1.opened == true)
{
    log.debug "It's sunset and the door is open!"
    def delay = minutesLater * 60
    log.debug "Setting notification in ${minuteslater} minutes (${delay} seconds)."
    runIn (delay, garageLeftOpen)
}
}

def garageLeftOpen()
{
log.debug "Notify ${door1} left open!"
if(sendPushMessage != "No")
{
    log.debug("Sending push notification...")
    sendPush("${door1} has been left open at night for ${minutesLater} minutes.")
}
if(phoneNumber != null)
{
    log.debug("Sending text notification...")
    sendSms(phoneNumber, "${door1} has been left open at night for ${minutesLater} minutes.")
}
}

(Jason "The Enabler" as deemed so by @Smart) #2

Hey nice job. Can it notify at any time? Or only at night? I haven’t installed it yet to check it out… Sorry, driving.


(Travis) #3

Great job… you can do this with CoRe and a sensor. Send Push or SMS message. I use CoRe to automatically close my garage door after a certain time if its left open and also send me a notification that it was auto-closed. Works flawlessly.


(Russ Pearlman) #4

The code is simply to do notifications from sunset to sunrise. It also does not automatically close the garage door.

If you would like for me to extend it to provide more customization, that would be no problem. Just let me know what you want.

Russ


(Jason "The Enabler" as deemed so by @Smart) #5

I think you have a great base program going here and I’d like to use it.

Mostly I’m looking for reliable notifications that the door had been left open.

I use CoRE for this now and I plan to use this as a secondary level of security. I’m ask about redundancy, retired Navy, lol… It’s why our plans all have two engines… Redundancy!

I’m not to concerned about automatically closing the door, I prefer to do that via the ST app when i forget the door… Cause if I forgot that I’ve usually forgotten everything else as well.

But, that would be a very good addition to the app that I’m sure would be used.


(Russ Pearlman) #6

I can work on it later. So, just to be clear here are the requirements:

  1. Either select sunrise/sunset

OR

  1. Specify your own “time of day”

OR

  1. Notify regardless of time of day - just after the specified amount of time

In any of the cases, it will merely send a push or SMS notification based on how you configure it. Correct?


(Andrea Bianco) #7

Thank you - this is great. I also use “IS my home secure” from @tslagle13 - as found on Github.
I like this alot too! Great job.


(DJAC) #8

Hey there I’m trying to do the same thing but with my hue lights
So when the garage is open… Two hue lights go on.

That’s the easy part. But when the garage door closes the lights must restore their original state…

Has anyone found out how to do this is notify with hue just gives you a timer but does not stay on confirming indeed the door is still open