2 * Double Tap Mode Switch
4 * Copyright 2014 George Sudarkoff
6 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
7 * in compliance with the License. You may obtain a copy of the License at:
9 a http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
12 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
13 * for the specific language governing permissions and limitations under the License.
18 name: "Double Tap Timed Light Switch",
19 namespace: "com.sudarkoff",
20 author: "George Sudarkoff",
21 description: "Turn a light for a period of time when an existing switch is tapped OFF twice in a row.",
22 category: "Convenience",
23 iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
24 iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png"
28 page (name: "configPage", install: true, uninstall: true) {
29 section ("When this switch is double-tapped OFF...") {
30 input "master", "capability.switch", required: true
33 section ("Turn it on for this many minutes...") {
34 input "duration", "number", required: true
37 section ("Notification method") {
38 input "sendPushMessage", "bool", title: "Send a push notification?"
41 section (title: "More Options", hidden: hideOptionsSection(), hideable: true) {
42 input "phone", "phone", title: "Additionally, also send a text message to:", required: false
44 input "customName", "text", title: "Assign a name", required: false
46 def timeLabel = timeIntervalLabel()
47 href "timeIntervalInput", title: "Only during a certain time", description: timeLabel ?: "Tap to set", state: timeLabel ? "complete" : null
49 input "days", "enum", title: "Only on certain days of the week", multiple: true, required: false,
50 options: ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
51 input "modes", "mode", title: "Only when mode is", multiple: true, required: false
55 page (name: "timeIntervalInput", title: "Only during a certain time") {
57 input "starting", "time", title: "Starting", required: false
58 input "ending", "time", title: "Ending", required: false
78 state.currentAppLabel = customName
80 subscribe(master, "switch", switchHandler, [filterEvents: false])
83 def switchHandler(evt) {
87 // use Event rather than DeviceState because we may be changing DeviceState to only store changed values
88 def recentStates = master.eventsSince(new Date(now() - 4000), [all:true, max: 10]).findAll{it.name == "switch"}
89 log.debug "${recentStates?.size()} STATES FOUND, LAST AT ${recentStates ? recentStates[0].dateCreated : ''}"
91 if (evt.isPhysical()) {
92 if (evt.value == "on") {
95 else if (evt.value == "off" && lastTwoStatesWere("off", recentStates, evt)) {
96 log.debug "detected two OFF taps, turning the light ON for ${duration} minutes"
98 runIn(duration * 60, switchOff)
99 def message = "${master.label} turned on for ${duration} minutes"
104 log.trace "Skipping digital on/off event"
113 private lastTwoStatesWere(value, states, evt) {
116 log.trace "unfiltered: [${states.collect{it.dateCreated + ':' + it.value}.join(', ')}]"
117 def onOff = states.findAll { it.isPhysical() || !it.type }
118 log.trace "filtered: [${onOff.collect{it.dateCreated + ':' + it.value}.join(', ')}]"
120 // This test was needed before the change to use Event rather than DeviceState. It should never pass now.
121 if (onOff[0].date.before(evt.date)) {
122 log.warn "Last state does not reflect current event, evt.date: ${evt.dateCreated}, state.date: ${onOff[0].dateCreated}"
123 result = evt.value == value && onOff[0].value == value
126 result = onOff.size() > 1 && onOff[0].value == value && onOff[1].value == value
133 if (sendPushMessage != "No") {
144 // execution filter methods
146 modeOk && daysOk && timeOk
149 private getModeOk() {
150 def result = !modes || modes.contains(location.mode)
154 private getDaysOk() {
157 def df = new java.text.SimpleDateFormat("EEEE")
158 if (location.timeZone) {
159 df.setTimeZone(location.timeZone)
162 df.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"))
164 def day = df.format(new Date())
165 result = days.contains(day)
170 private getTimeOk() {
172 if (starting && ending) {
174 def start = timeToday(starting).time
175 def stop = timeToday(ending).time
176 result = start < stop ? currTime >= start && currTime <= stop : currTime <= stop || currTime >= start
178 log.trace "timeOk = $result"
182 private hideOptionsSection() {
183 (phone || starting || ending || customName || days || modes) ? false : true
186 private hhmm(time, fmt = "h:mm a")
188 def t = timeToday(time, location.timeZone)
189 def f = new java.text.SimpleDateFormat(fmt)
190 f.setTimeZone(location.timeZone ?: timeZone(time))
194 private timeIntervalLabel() {
195 (starting && ending) ? hhmm(starting) + "-" + hhmm(ending, "h:mm a z") : ""