Garage and Nighttime Outdoor Lights

Here’s a Smart App that will turn on the garage lights any time a door is opened or motion is detected. With this in place, you’ll forget you even have a light switch.

It’ll also turn on your outdoor lights at sunset and turn them back off at sunrise to help give you a little more security

definition(

name: “Garage Lights”,
namespace: “mySpace”,
author: “Me”,
description: “Manage garage lights with garage door and motion sensors.”,
category: “Convenience”,
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(“Motion Sensors”) {

input “MotionSensors”, “capability.motionSensor”, required: true, multiple: true, title:”Motion Sensors”

}
section(“Garage Doors”) {

input “ExteriorDoors”, “capability.contactSensor”, required: true, multiple: true, title:”Exterior Doors”
input “InteriorDoors”, “capability.contactSensor”, required: false, multiple: true, title:”Interior Doors”

}
section(“Lights”) {

input “LightsInterior”, “capability.switch”, required: true, multiple: true, title:”Interior Lights”
input “LightsExterior”, “capability.switch”, required: false, multiple: true, title:”Exterior Lights”

}

def installed() {

log.debug “Installed with settings: ${settings}”

initialize()

}

def updated() {

log.debug “Updated with settings: ${settings}”

unsubscribe()
initialize()

}

def initialize() {

subscribe(MotionSensors, “motion.active”, motionDetectedHandler)
subscribe(MotionSensors, “motion.inactive”, doorOpenedHandler)
subscribe(ExteriorDoors, “contact.open”, doorOpenedHandler);
subscribe(ExteriorDoors, “contact.closed”, doorOpenedHandler);
subscribe(InteriorDoors, “contact.open”, doorOpenedHandler);
subscribe(InteriorDoors, “contact.closed”, doorOpenedHandler);
subscribe(location, “sunsetTime”, sunsetTimeHandler);
subscribe(location, “sunriseTime”, sunriseTimeHandler);

scheduleTurnOn(location.currentValue(“sunsetTime”))
scheduleTurnOff(location.currentValue(“sunriseTime”));

}

def sunriseTimeHandler(evt){

scheduleTurnOff(evt.value)

}

def sunsetTimeHandler(evt){

//when I find out the sunset time, schedule the lights to turn on with an offset
scheduleTurnOn(evt.value)

}

def scheduleTurnOff(sunriseString){

//get the Date value for the string
def sunriseTime = Date.parse(“yyyy-MM-dd’T’HH:mm:ss.SSS’Z'”, sunriseString)

//calculate the offset
def timeBeforeSunrise = new Date(sunriseTime.time)

log.debug “Scheduling for: $timeBeforeSunrise (sunrise is $sunriseTime)”

//schedule this to run one time
runOnce(timeBeforeSunrise, turnOff)

}

def scheduleTurnOn(sunsetString){

//get the Date value for the string
def sunsetTime = Date.parse(“yyyy-MM-dd’T’HH:mm:ss.SSS’Z'”, sunsetString)

//calculate the offset
def timeBeforeSunset = new Date(sunsetTime.time)

log.debug “Scheduling for: $timeBeforeSunset (sunset is $sunsetTime)”

//schedule this to run one time
runOnce(timeBeforeSunset, turnOn)

}

def turnOff(){

LightsExterior.off()

}

def turnOn(){

LightsExterior.on()

}

def motionDetectedHandler(evt){

log.debug “Motion Detected $evt”
LightsInterior.on()

}

def shouldLightsBeOn(){

log.debug “ShouldLightsBeOn”;
def noMotion = noMotionDetected();
log.debug “No Motion Sensors: $noMotion”

def allDoorsClosed = areDoorsClosed();
log.debug “No Doors Closed: $allDoorsClosed”

if(noMotion && allDoorsClosed)
{

log.debug(“Turning off Lights”)
LightsInterior.off();

if (!shouldExteriorLightsBeOn())
{

LightsExterior.off();

}
else
{

LightsExterior.on();

}

}
else
{

LightsInterior.on();
if (shouldExteriorLightsBeOn())
{

LightsExterior.on();

}
else
{

LightsExterior.off();

}

}

}

def motionUndetectedHandler(evt)
{

log.debug “Motion Undetected $evt”

shouldLightsBeOn();

}

def noMotionDetected()
{

def noMotion = true;
MotionSensors.each { noMotion = noMotion && (it.latestValue(“motion”) == “inactive” || it.latestValue(“motion”) == null) };
return noMotion;

}

def areDoorsClosed()
{

log.debug “Checking if doors are closed.”;
def noOpenDoors = true;
ExteriorDoors.each {noOpenDoors = noOpenDoors && (it.latestValue(“contact”) == “closed” || it.latestValue(“contact”) == null)}
InteriorDoors.each {noOpenDoors = noOpenDoors && it.latestValue(“contact”) == “closed”}

log.debug “No Open Doors: $noOpenDoors”
return noOpenDoors

}

def shouldExteriorLightsBeOn()
{

def sunTimes = getSunriseAndSunset(zipCode: “68128”)
log.debug(“Sunset: ” + sunTimes.sunset + “, Sunrise: ” + sunTimes.sunrise);
def between = timeOfDayIsBetween(sunTimes.sunset, sunTimes.sunrise, new Date(), location.timeZone)
log.debug “Between: $between”
return between;

}

def doorOpenedHandler(evt)
{

log.debug “Exterior Door Opened”;
shouldLightsBeOn();
log.debug “Done Exterior Door Opened”;

}

def interiorDoorOpenedHandler(evt)
{

LightsInterior.on()

}

def interiorDoorClosedHandler(evt)
{

shouldLightsBeOn();

}

Recent Content

link to Baby Dimmer Control

Baby Dimmer Control

Manage your Baby’s dimmer   This little app will help control the dimmer switch in your baby’s nursery, or any other dimmer switch. Perhaps late at night you don’t want the light at 100% brightness. This app will take care of that by allowing you to specify a time and what level to set the […]
link to Garage Door Monitor

Garage Door Monitor

Here’s a little app which will monitor your garage doors and alert you if they remain open for longer than you would like. It’s always a terrible feeling when you realize that you left the garage door open all evening, or throughout the heat of the day. You can also specify some motion sensors which […]