2 * Copyright 2015 SmartThings
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 * in compliance with the License. You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
10 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
11 * for the specific language governing permissions and limitations under the License.
20 namespace: "smartthings",
21 author: "SmartThings",
22 description: "Flashes a set of lights in response to motion, an open/close event, or a switch.",
23 category: "Convenience",
24 iconUrl: "https://s3.amazonaws.com/smartapp-icons/Meta/light_motion-outlet-contact.png",
25 iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Meta/light_motion-outlet-contact@2x.png"
29 section("When any of the following devices trigger..."){
30 input "motion", "capability.motionSensor", title: "Motion Sensor?", required: false
31 input "contact", "capability.contactSensor", title: "Contact Sensor?", required: false
32 input "acceleration", "capability.accelerationSensor", title: "Acceleration Sensor?", required: false
33 input "mySwitch", "capability.switch", title: "Switch?", required: false
34 input "myPresence", "capability.presenceSensor", title: "Presence Sensor?", required: false
36 section("Then flash..."){
37 input "switches", "capability.switch", title: "These lights", multiple: true
38 input "numFlashes", "number", title: "This number of times (default 3)", required: false
40 section("Time settings in milliseconds (optional)..."){
41 input "onFor", "number", title: "On for (default 1000)", required: false
42 input "offFor", "number", title: "Off for (default 1000)", required: false
47 log.debug "Installed with settings: ${settings}"
53 log.debug "Updated with settings: ${settings}"
61 subscribe(contact, "contact.open", contactOpenHandler)
64 subscribe(acceleration, "acceleration.active", accelerationActiveHandler)
67 subscribe(motion, "motion.active", motionActiveHandler)
70 subscribe(mySwitch, "switch.on", switchOnHandler)
73 subscribe(myPresence, "presence", presenceHandler)
77 def motionActiveHandler(evt) {
78 log.debug "motion $evt.value"
82 def contactOpenHandler(evt) {
83 log.debug "contact $evt.value"
87 def accelerationActiveHandler(evt) {
88 log.debug "acceleration $evt.value"
92 def switchOnHandler(evt) {
93 log.debug "switch $evt.value"
97 def presenceHandler(evt) {
98 log.debug "presence $evt.value"
99 if (evt.value == "present") {
101 } else if (evt.value == "not present") {
106 private flashLights() {
108 def onFor = onFor ?: 1000
109 def offFor = offFor ?: 1000
110 def numFlashes = numFlashes ?: 3
112 log.debug "LAST ACTIVATED IS: ${state.lastActivated}"
113 if (state.lastActivated) {
114 def elapsed = now() - state.lastActivated
115 def sequenceTime = (numFlashes + 1) * (onFor + offFor)
116 doFlash = elapsed > sequenceTime
117 log.debug "DO FLASH: $doFlash, ELAPSED: $elapsed, LAST ACTIVATED: ${state.lastActivated}"
121 log.debug "FLASHING $numFlashes times"
122 state.lastActivated = now()
123 log.debug "LAST ACTIVATED SET TO: ${state.lastActivated}"
124 def initialActionOn = switches.collect{it.currentSwitch != "on"}
127 log.trace "Switch on after $delay msec"
128 switches.eachWithIndex {s, i ->
129 if (initialActionOn[i]) {
137 log.trace "Switch off after $delay msec"
138 switches.eachWithIndex {s, i ->
139 if (initialActionOn[i]) {