4 * Source: https://github.com/tslagle13/SmartThings/blob/master/Director-Series-Apps/Lighting-Director/Lighting%20Director.groovy
6 * Current Version: 2.9.4
11 * Version - 1.30.1 Modification by Michael Struck - Fixed syntax of help text and titles of scenarios, along with a new icon
12 * Version - 1.40.0 Modification by Michael Struck - Code optimization and added door contact sensor capability
13 * Version - 1.41.0 Modification by Michael Struck - Code optimization and added time restrictions to each scenario
14 * Version - 2.0 Tim Slagle - Moved to only have 4 slots. Code was to heavy and needed to be trimmed.
15 * Version - 2.1 Tim Slagle - Moved time interval inputs inline with STs design.
16 * Version - 2.2 Michael Struck - Added the ability to activate switches via the status locks and fixed some syntax issues
17 * Version - 2.5 Michael Struck - Changed the way the app unschedules re-triggered events
18 * Version - 2.5.1 Tim Slagle - Fixed Time Logic
19 * Version - 2.6 Michael Struck - Added the additional restriction of running triggers once per day and misc cleanup of code
20 * Version - 2.7 Michael Struck - Added feature that turns off triggering if the physical switch is pressed.
21 * Version - 2.81 Michael Struck - Fixed an issue with dimmers not stopping light action
22 * Version - 2.9 Michael Struck - Fixed issue where button presses outside of the time restrictions prevent the triggers from firing and code optimization
23 * Version - 2.9.1 Tim Slagle - Further enhanced time interval logic.
24 * Version - 2.9.2 Brandon Gordon - Added support for acceleration sensors.
25 * Version - 2.9.3 Brandon Gordon - Added mode change subscriptions.
26 * Version - 2.9.4 Michael Struck - Code Optimization when triggers are tripped
28 * Source code can be found here: https://github.com/tslagle13/SmartThings/blob/master/smartapps/tslagle13/vacation-lighting-director.groovy
30 * Copyright 2015 Tim Slagle and Michael Struck
32 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
33 * in compliance with the License. You may obtain a copy of the License at:
35 * http://www.apache.org/licenses/LICENSE-2.0
37 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
38 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
39 * for the specific language governing permissions and limitations under the License.
44 name: "Lighting Director",
45 namespace: "tslagle13",
46 author: "Tim Slagle & Michael Struck",
47 description: "Control up to 4 sets (scenarios) of lights based on motion, door contacts and illuminance levels.",
48 category: "Convenience",
49 iconUrl: "https://raw.githubusercontent.com/MichaelStruck/SmartThings/master/Other-SmartApps/Lighting-Director/LightingDirector.png",
50 iconX2Url: "https://raw.githubusercontent.com/MichaelStruck/SmartThings/master/Other-SmartApps/Lighting-Director/LightingDirector@2x.png",
51 iconX3Url: "https://raw.githubusercontent.com/MichaelStruck/SmartThings/master/Other-SmartApps/Lighting-Director/LightingDirector@2x.png")
54 page(name: "timeIntervalInputA", title: "Only during a certain time", refreshAfterSelection:true) {
56 input "A_timeStart", "time", title: "Starting", required: false, refreshAfterSelection:true
57 input "A_timeEnd", "time", title: "Ending", required: false, refreshAfterSelection:true
61 page name:"pageSetupScenarioA"
67 def pageProperties = [
74 return dynamicPage(pageProperties) {
75 section("Setup Menu") {
76 href "pageSetupScenarioA", title: getTitle(settings.ScenarioNameA), description: getDesc(settings.ScenarioNameA), state: greyOut(settings.ScenarioNameA)
78 section([title:"Options", mobileOnly:true]) {
79 label title:"Assign a name", required:false
84 // Show "pageSetupScenarioA" page
85 def pageSetupScenarioA() {
89 type: "capability.switch",
90 title: "Control the following switches...",
96 type: "capability.switchLevel",
97 title: "Dim the following...",
104 type: "capability.motionSensor",
105 title: "Using these motion sensors...",
110 def inputAccelerationA = [
111 name: "A_acceleration",
112 type: "capability.accelerationSensor",
113 title: "Or using these acceleration sensors...",
117 def inputContactA = [
119 type: "capability.contactSensor",
120 title: "Or using these contact sensors...",
125 def inputTriggerOnceA = [
126 name: "A_triggerOnce",
128 title: "Trigger only once per day...",
132 def inputSwitchDisableA = [
133 name: "A_switchDisable",
135 title: "Stop triggering if physical switches/dimmers are turned off...",
141 type: "capability.lock",
142 title: "Or using these locks...",
150 title: "Only during the following modes...",
158 options: ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"],
159 title: "Only on certain days of the week...",
168 options: [[10:"10%"],[20:"20%"],[30:"30%"],[40:"40%"],[50:"50%"],[60:"60%"],[70:"70%"],[80:"80%"],[90:"90%"],[100:"100%"]],
169 title: "Set dimmers to this level",
174 def inputTurnOnLuxA = [
177 title: "Only run this scenario if lux is below...",
182 def inputLuxSensorsA = [
183 name: "A_luxSensors",
184 type: "capability.illuminanceMeasurement",
185 title: "On these lux sensors",
190 def inputTurnOffA = [
193 title: "Turn off this scenario after motion stops or doors close/lock (minutes)...",
198 def inputScenarioNameA = [
199 name: "ScenarioNameA",
201 title: "Scenario Name",
206 def pageProperties = [
207 name: "pageSetupScenarioA",
210 return dynamicPage(pageProperties) {
211 section("Name your scenario") {
212 input inputScenarioNameA
215 section("Devices included in the scenario") {
217 input inputAccelerationA
224 section("Scenario settings") {
226 input inputTurnOnLuxA
227 input inputLuxSensorsA
231 section("Scenario restrictions") {
232 input inputTriggerOnceA
233 input inputSwitchDisableA
234 href "timeIntervalInputA", title: "Only during a certain time...", description: getTimeLabel(A_timeStart, A_timeEnd), state: greyedOutTime(A_timeStart, A_timeEnd), refreshAfterSelection:true
263 subscribe(settings.A_motion, "motion", onEventA)
267 subscribe(settings.A_acceleration, "acceleration", onEventA)
271 subscribe(settings.A_contact, "contact", onEventA)
275 subscribe(settings.A_lock, "lock", onEventA)
278 if(A_switchDisable) {
279 subscribe(A_switches, "switch.off", onPressA)
280 subscribe(A_dimmers, "switch.off", onPressA)
287 if ((!A_triggerOnce || (A_triggerOnce && !state.A_triggered)) && (!A_switchDisable || (A_switchDisable && !state.A_triggered))) { //Checks to make sure this scenario should be triggered more then once in a day
288 if ((!A_mode || A_mode.contains(location.mode)) && getTimeOk (A_timeStart, A_timeEnd) && getDayOk(A_day)) { //checks to make sure we are not opperating outside of set restrictions.
289 if ((!A_luxSensors) || (A_luxSensors.latestValue("illuminance") <= A_turnOnLux)){ //checks to make sure illimunance is either not cared about or if the value is within the restrictions
290 def A_levelOn = A_level as Integer
292 //Check states of each device to see if they are to be ignored or if they meet the requirments of the app to produce an action.
293 if (getInputOk(A_motion, A_contact, A_lock, A_acceleration)) {
294 log.debug("Motion, Door Open or Unlock Detected Running '${ScenarioNameA}'")
295 settings.A_dimmers?.setLevel(A_levelOn)
296 settings.A_switches?.on()
298 state.A_triggered = true
300 runOnce (getMidnight(), midNightReset)
303 if (state.A_timerStart){
304 unschedule(delayTurnOffA)
305 state.A_timerStart = false
309 //if none of the above paramenters meet the expectation of the app then turn off
312 if (settings.A_turnOff) {
313 runIn(A_turnOff * 60, "delayTurnOffA")
314 state.A_timerStart = true
317 settings.A_switches?.off()
318 settings.A_dimmers?.setLevel(0)
319 if (state.A_triggered) {
320 runOnce (getMidnight(), midNightReset)
327 log.debug("Motion, Contact or Unlock detected outside of mode or time/day restriction. Not running scenario.")
333 settings.A_switches?.off()
334 settings.A_dimmers?.setLevel(0)
335 state.A_timerStart = false
336 if (state.A_triggered) {
337 runOnce (getMidnight(), midNightReset)
342 //when physical switch is actuated disable the scenario
344 if ((!A_mode || A_mode.contains(location.mode)) && getTimeOk (A_timeStart, A_timeEnd) && getDayOk(A_day)) { //checks to make sure we are not opperating outside of set restrictions.
345 if ((!A_luxSensors) || (A_luxSensors.latestValue("illuminance") <= A_turnOnLux)){
346 if ((!A_triggerOnce || (A_triggerOnce && !state.A_triggered)) && (!A_switchDisable || (A_switchDisable && !state.A_triggered))) {
348 state.A_triggered = true
349 unschedule(delayTurnOffA)
350 runOnce (getMidnight(), midNightReset)
351 log.debug "Physical switch in '${ScenarioNameA}' pressed. Triggers for this scenario disabled."
358 //resets once a day trigger at midnight so trigger can be ran again the next day.
359 def midNightReset() {
360 state.A_triggered = false
361 state.B_triggered = false
362 state.C_triggered = false
363 state.D_triggered = false
366 private def helpText() {
368 "Select motion sensors, acceleration sensors, contact sensors or locks to control a set of lights. " +
369 "Each scenario can control dimmers and switches but can also be " +
370 "restricted to modes or between certain times and turned off after " +
371 "motion stops, doors close or lock. Scenarios can also be limited to " +
372 "running once or to stop running if the physical switches are turned off."
376 //should scenario be marked complete or not
377 def greyOut(scenario){
385 //should i mark the time restriction green or grey
386 def greyedOutTime(start, end){
395 def getTitle(scenario) {
403 //recursively applies label to each scenario depending on if the scenario has deatils inside it or not
404 def getDesc(scenario) {
405 def desc = "Tap to create a scenario"
407 desc = "Tap to edit scenario"
414 def midnightToday = timeToday("2000-01-01T23:59:59.999-0000", location.timeZone)
418 //used to recursively check device states when methods are triggered
419 private getInputOk(motion, contact, lock, acceleration) {
421 def motionDetected = false
422 def accelerationDetected = false
423 def contactDetected = false
424 def unlockDetected = false
428 if (motion.latestValue("motion").contains("active")) {
429 motionDetected = true
434 if (acceleration.latestValue("acceleration").contains("active")) {
435 accelerationDetected = true
440 if (contact.latestValue("contact").contains("open")) {
441 contactDetected = true
446 if (lock.latestValue("lock").contains("unlocked")) {
447 unlockDetected = true
451 result = motionDetected || contactDetected || unlockDetected || accelerationDetected
456 private getTimeOk(starting, ending) {
458 if (starting && ending) {
460 def start = timeToday(starting).time
461 def stop = timeToday(ending).time
462 result = start < stop ? currTime >= start && currTime <= stop : currTime <= stop || currTime >= start
466 result = currTime >= start
469 result = currTime <= stop
472 log.trace "timeOk = $result"
476 def getTimeLabel(start, end){
477 def timeLabel = "Tap to set"
480 timeLabel = "Between" + " " + hhmm(start) + " " + "and" + " " + hhmm(end)
483 timeLabel = "Start at" + " " + hhmm(start)
486 timeLabel = "End at" + hhmm(end)
491 private hhmm(time, fmt = "h:mm a")
493 def t = timeToday(time, location.timeZone)
494 def f = new java.text.SimpleDateFormat(fmt)
495 f.setTimeZone(location.timeZone ?: timeZone(time))
499 private getDayOk(dayList) {
502 def df = new java.text.SimpleDateFormat("EEEE")
503 if (location.timeZone) {
504 df.setTimeZone(location.timeZone)
507 df.setTimeZone(TimeZone.getTimeZone("America/New_York"))
509 def day = df.format(new Date())
510 result = dayList.contains(day)