Random Lights

Here’s an app that will randomly turn lights on and off when the security system is armed. The lights will only turn on between sunset and sunrise, and the time will be random within a range you specify. Hopefully this makes thieves think twice before breaking in.

An added bonus is that if you have dogs, or other animals, and want an area to always have a light on for them, you can do that with this.

definition
(
    name: "RandomLights",
    namespace: "MySpace",
    author: "Me",
    description: "RandomLights",
    category: "Safety & Security",
    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("Lights") 
   {
        input "DoggieLights", "capability.switch", required: true, multiple: true, title:"Doggie Lights"
        input "RandomLights","capability.switch", required: false, multiple: true, title:"Random Lights"
        input "minTime", "number", title: "Min Light Time"
        input "maxTime", "number", title: "Max Light Time"
        input "fromTime", "time", title: "From", required: true
        input "toTime", "time", title:"To", required: true
    }
}

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

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

def initialize() 
{
    subscribe(location,"alarmSystemStatus", alarmStatusHandler)
}

def alarmStatusHandler(evt) 
{
    def status = location.currentState("alarmSystemStatus")?.value
    log.debug "alarm status: " + status
    def randomizer = new Random()
    //log.debug"alarm status changed to: ${evt.value}"
    handleLights()
}

def handleLights()
{
    def randomizer = new Random()
    int randomTime = (randomizer.nextInt() % (maxTime - minTime)) + minTime
    if (isSunDown() && !shouldLightsStayOff())
    {
        def frequency = RandomLights.count { it != null }
        log.debug "frequency: " + frequency
        def doggieFrequency = DoggieLights.count { it != null }
        def currentState = location.currentState("alarmSystemStatus")
        log.debug "current alarm state: $currentState.value"
        if (currentState.value =="away")
        {
             log.debug "Status is armed"
             int random = Math.abs(randomizer.nextInt() % frequency) + 1
             log.debug "Random number: $random,Random Time: $randomTime"
             for (int i = 0; i < frequency; i++)
             {
                 if (i != random)
                 {
                       RandomLights[i].off()
                 }
                 else
                 {
                      RandomLights[i].on()
                 }
             }
             random = Math.abs(randomizer.nextInt() % doggieFrequency)
             log.debug "Doggie Random:" + random
             for (int i = 0; i < doggieFrequency; i++)
             {
                  if (i != random)
                  {
                      DoggieLights[i].off()
                  }
                  else
                  {
                      DoggieLights[i].on()
                  }
              }
         }
         else
         {
              log.debug"Status is disarmed"
         }
     }
     runIn(randomTime*60, handleLights)
}

def isSunDown()
{
    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 shouldLightsStayOff()
{
    def between = timeOfDayIsBetween(fromTime, toTime, new Date(),location.timeZone)
    if (between) 
    {
        return false
    } 
    else
    {
        return true
    }
}
 

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 […]