Garage door notifier at night (WTIH CODE)

Russ_Pearlman
Mon, 29 Aug 2016 17:01:10 +0000
SmartApps & Automations

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

8 posts - 5 participants