App idea and Random Feature


(Foneguy2) #1

I am wanting an app that can turn selected lights on for a random amount of time and then turn them off.
The idea being, if in Away mode (and after a certain time of day or sunset) then you can choose certain lights to turn on and off making the house look lived in.
I guess you might need other values too - i.e. how often during the night should the lights turn on etc…

Is there a random function that is available and is something like this app possible?

thanks


(Todd Wackford) #2

There used to be. I can’t find it now. It was under security. It was pretty cool. Instead of random on/off and duration, it used actual activity from a random day in the past. I’ll see if I can find the app name and code.

Twack


(Chrisf) #3

Did you ever find anything regarding this? I would be interested as well.


(Brock) #4
/**
 *  Smarter Home Light Simulator
 *
 *  Author: Austin Fonacier
 *  Twitter: @austinrfnd
 *  Github: http://github.com/austinrfnd
 *
 */

preferences {
  section("Light switches to turn on/off"){
    input "switches", "capability.switch", title: "Switches", multiple: true, required: true
  }
  section("How often to cycle the lights"){
    input "frequency_minutes", "number", title: "Minutes?"
  }
  section("Number of actives lights at any given time"){
    input "number_of_active_lights", "number", title: "Number of active lights"
  }
}

def installed() {
  scheduleCheck()
}

def updated() {
  unsubscribe()
  unschedule()
  scheduleCheck()
}

// We want to turn off all the lights
// Then we want to take a random set of lights and turn those on
// Then run it again when the frequency demands it
def scheduleCheck() {
  // turn off all the switches
  switches.off()
  
  // grab a random switch
  def random = new Random()
  def inactive_switches = switches
  for (int i = 0 ; i < number_of_active_lights ; i++) {
    // if there are no inactive switches to turn on then let's break
    if (inactive_switches.size() == 0){
      break
    }

    // grab a random switch and turn it on
    def random_int = random.nextInt(inactive_switches.size())
    inactive_switches[random_int].on()

    // then remove that switch from the pool off switches that can be turned on
    inactive_switches.remove(random_int)
  }

  // re-run again when the frequency demands it
  runIn(frequency_minutes * 60, scheduleCheck)
}

There you go.


(Foneguy2) #5

that’s really close - thanks.

I think the addition of turning the lights on for a random time and only when in certain modes would perfect this.
I may even be able to add those features :slight_smile:

Thanks again.