2 * Vacation Lighting Director
4 * Version 2.5 - Moved scheduling over to Cron and added time as a trigger.
5 * Cleaned up formatting and some typos.
7 * Made people option optional
8 * Added sttement to unschedule on mode change if people option is not selected
10 * Version 2.4 - Added information paragraphs
12 * Source code can be found here: https://github.com/tslagle13/SmartThings/blob/master/smartapps/tslagle13/vacation-lighting-director.groovy
14 * Copyright 2016 Tim Slagle
16 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
17 * in compliance with the License. You may obtain a copy of the License at:
19 * http://www.apache.org/licenses/LICENSE-2.0
21 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
22 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
23 * for the specific language governing permissions and limitations under the License.
28 // Automatically generated. Make future change here.
30 name: "Vacation Lighting Director",
31 namespace: "tslagle13",
33 category: "Safety & Security",
34 description: "Randomly turn on/off lights to simulate the appearance of a occupied home while you are away.",
35 iconUrl: "http://icons.iconarchive.com/icons/custom-icon-design/mono-general-2/512/settings-icon.png",
36 iconX2Url: "http://icons.iconarchive.com/icons/custom-icon-design/mono-general-2/512/settings-icon.png"
43 page name: "timeIntervalInput"
50 def pageProperties = [
58 return dynamicPage(pageProperties) {
60 paragraph "This app can be used to make your home seem occupied anytime you are away from your home. " +
61 "Please use each of the the sections below to setup the different preferences to your liking. "
63 section("Setup Menu") {
64 href "Setup", title: "Setup", description: "", state:greyedOut()
65 href "Settings", title: "Settings", description: "", state: greyedOutSettings()
67 section([title:"Options", mobileOnly:true]) {
68 label title:"Assign a name", required:false
85 type: "capability.switch",
91 def frequency_minutes = [
92 name: "frequency_minutes",
98 def number_of_active_lights = [
99 name: "number_of_active_lights",
101 title: "Number of active lights",
105 def pageName = "Setup"
107 def pageProperties = [
110 nextPage: "pageSetup"
113 return dynamicPage(pageProperties) {
116 paragraph "In this section you need to setup the deatils of how you want your lighting to be affected while " +
117 "you are away. All of these settings are required in order for the simulator to run correctly."
119 section("Simulator Triggers") {
121 href "timeIntervalInput", title: "Times", description: timeIntervalLabel(), refreshAfterSelection:true
123 section("Light switches to turn on/off") {
126 section("How often to cycle the lights") {
127 input frequency_minutes
129 section("Number of active lights at any given time") {
130 input number_of_active_lights
139 def falseAlarmThreshold = [
140 name: "falseAlarmThreshold",
142 title: "Default is 2 minutes",
148 title: "Only on certain days of the week",
151 options: ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
154 def pageName = "Settings"
156 def pageProperties = [
159 nextPage: "pageSetup"
164 type: "capability.presenceSensor",
165 title: "If these people are home do not change light status",
170 return dynamicPage(pageProperties) {
173 paragraph "In this section you can restrict how your simulator runs. For instance you can restrict on which days it will run " +
174 "as well as a delay for the simulator to start after it is in the correct mode. Delaying the simulator helps with false starts based on a incorrect mode change."
176 section("Delay to start simulator") {
177 input falseAlarmThreshold
180 paragraph "Not using this setting may cause some lights to remain on when you arrive home"
183 section("More options") {
189 def timeIntervalInput() {
190 dynamicPage(name: "timeIntervalInput") {
192 input "startTimeType", "enum", title: "Starting at", options: [["time": "A specific time"], ["sunrise": "Sunrise"], ["sunset": "Sunset"]], defaultValue: "time", submitOnChange: true
193 if (startTimeType in ["sunrise","sunset"]) {
194 input "startTimeOffset", "number", title: "Offset in minutes (+/-)", range: "*..*", required: false
197 input "starting", "time", title: "Start time", required: false
201 input "endTimeType", "enum", title: "Ending at", options: [["time": "A specific time"], ["sunrise": "Sunrise"], ["sunset": "Sunset"]], defaultValue: "time", submitOnChange: true
202 if (endTimeType in ["sunrise","sunset"]) {
203 input "endTimeOffset", "number", title: "Offset in minutes (+/-)", range: "*..*", required: false
206 input "ending", "time", title: "End time", required: false
225 if (newMode != null) {
226 subscribe(location, modeChangeHandler)
228 if (starting != null) {
229 schedule(starting, modeChangeHandler)
231 log.debug "Installed with settings: ${settings}"
234 def modeChangeHandler(evt) {
235 def delay = (falseAlarmThreshold != null && falseAlarmThreshold != "") ? falseAlarmThreshold * 60 : 2 * 60
236 runIn(delay, scheduleCheck)
240 //Main logic to pick a random set of lights from the large set of lights to turn on and then turn the rest off
241 def scheduleCheck(evt) {
244 // turn off all the switches
247 // grab a random switch
248 def random = new Random()
249 def inactive_switches = switches
250 for (int i = 0 ; i < number_of_active_lights ; i++) {
251 // if there are no inactive switches to turn on then let's break
252 if (inactive_switches.size() == 0){
256 // grab a random switch and turn it on
257 def random_int = random.nextInt(inactive_switches.size())
258 inactive_switches[random_int].on()
260 // then remove that switch from the pool off switches that can be turned on
261 inactive_switches.remove(random_int)
264 // re-run again when the frequency demands it
265 schedule("0 0/${frequency_minutes} * 1/1 * ? *", scheduleCheck)
267 //Check to see if mode is ok but not time/day. If mode is still ok, check again after frequency period.
269 log.debug("mode OK. Running again")
272 //if none is ok turn off frequency check and turn off lights.
275 //don't turn off lights if anyone is home
277 log.debug("Stopping Check for Light")
281 log.debug("Stopping Check for Light and turning off all lights")
293 //below is used to check restrictions
295 modeOk && daysOk && timeOk && homeIsEmpty
299 private getModeOk() {
300 def result = !newMode || newMode.contains(location.mode)
301 log.trace "modeOk = $result"
305 private getDaysOk() {
308 def df = new java.text.SimpleDateFormat("EEEE")
309 if (location.timeZone) {
310 df.setTimeZone(location.timeZone)
313 df.setTimeZone(TimeZone.getTimeZone("America/New_York"))
315 def day = df.format(new Date())
316 result = days.contains(day)
318 log.trace "daysOk = $result"
322 private getHomeIsEmpty() {
325 if(people?.findAll { it?.currentPresence == "present" }) {
329 log.debug("homeIsEmpty: ${result}")
334 private getSomeoneIsHome() {
337 if(people?.findAll { it?.currentPresence == "present" }) {
341 log.debug("anyoneIsHome: ${result}")
346 private getTimeOk() {
348 def start = timeWindowStart()
349 def stop = timeWindowStop()
350 if (start && stop && location.timeZone) {
351 result = timeOfDayIsBetween(start, stop, new Date(), location.timeZone)
353 log.trace "timeOk = $result"
357 private timeWindowStart() {
359 if (startTimeType == "sunrise") {
360 result = location.currentState("sunriseTime")?.dateValue
361 if (result && startTimeOffset) {
362 result = new Date(result.time + Math.round(startTimeOffset * 60000))
365 else if (startTimeType == "sunset") {
366 result = location.currentState("sunsetTime")?.dateValue
367 if (result && startTimeOffset) {
368 result = new Date(result.time + Math.round(startTimeOffset * 60000))
371 else if (starting && location.timeZone) {
372 result = timeToday(starting, location.timeZone)
374 log.trace "timeWindowStart = ${result}"
378 private timeWindowStop() {
380 if (endTimeType == "sunrise") {
381 result = location.currentState("sunriseTime")?.dateValue
382 if (result && endTimeOffset) {
383 result = new Date(result.time + Math.round(endTimeOffset * 60000))
386 else if (endTimeType == "sunset") {
387 result = location.currentState("sunsetTime")?.dateValue
388 if (result && endTimeOffset) {
389 result = new Date(result.time + Math.round(endTimeOffset * 60000))
392 else if (ending && location.timeZone) {
393 result = timeToday(ending, location.timeZone)
395 log.trace "timeWindowStop = ${result}"
399 private hhmm(time, fmt = "h:mm a")
401 def t = timeToday(time, location.timeZone)
402 def f = new java.text.SimpleDateFormat(fmt)
403 f.setTimeZone(location.timeZone ?: timeZone(time))
407 private timeIntervalLabel() {
409 switch (startTimeType) {
412 start += hhmm(starting)
417 start += startTimeType[0].toUpperCase() + startTimeType[1..-1]
418 if (startTimeOffset) {
419 start += startTimeOffset > 0 ? "+${startTimeOffset} min" : "${startTimeOffset} min"
425 switch (endTimeType) {
428 finish += hhmm(ending)
433 finish += endTimeType[0].toUpperCase() + endTimeType[1..-1]
435 finish += endTimeOffset > 0 ? "+${endTimeOffset} min" : "${endTimeOffset} min"
439 start && finish ? "${start} to ${finish}" : ""
442 //sets complete/not complete for the setup section on the main dynamic page
451 //sets complete/not complete for the settings section on the main dynamic page
452 def greyedOutSettings(){
454 if (people || days || falseAlarmThreshold ) {