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.
19 namespace: "smartthings",
20 author: "SmartThings",
21 description: "Turn on or off any number of switches when an existing switch is tapped twice in a row.",
22 category: "Convenience",
23 iconUrl: "https://s3.amazonaws.com/smartapp-icons/Meta/light_outlet.png",
24 iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Meta/light_outlet@2x.png"
28 section("When this switch is double-tapped...") {
29 input "master", "capability.switch", title: "Where?"
31 section("Turn on or off all of these switches as well") {
32 input "switches", "capability.switch", multiple: true, required: false
34 section("And turn off but not on all of these switches") {
35 input "offSwitches", "capability.switch", multiple: true, required: false
37 section("And turn on but not off all of these switches") {
38 input "onSwitches", "capability.switch", multiple: true, required: false
44 subscribe(master, "switch", switchHandler, [filterEvents: false])
50 subscribe(master, "switch", switchHandler, [filterEvents: false])
53 def switchHandler(evt) {
56 // use Event rather than DeviceState because we may be changing DeviceState to only store changed values
57 def recentStates = master.eventsSince(new Date(now() - 4000), [all:true, max: 10]).findAll{it.name == "switch"}
58 log.debug "${recentStates?.size()} STATES FOUND, LAST AT ${recentStates ? recentStates[0].dateCreated : ''}"
61 if (evt.value == "on" && lastTwoStatesWere("on", recentStates, evt)) {
62 log.debug "detected two taps, turn on other light(s)"
64 } else if (evt.value == "off" && lastTwoStatesWere("off", recentStates, evt)) {
65 log.debug "detected two taps, turn off other light(s)"
70 log.trace "Skipping digital on/off event"
74 private onSwitches1() {
75 def t1 = (switches).findAll{it}
76 def t2 = (onSwitches).findAll{it}
81 private offSwitches1() {
82 def t1 = (switches).findAll{it}
83 def t2 = (offSwitches).findAll{it}
88 private lastTwoStatesWere(value, states, evt) {
92 log.trace "unfiltered: [${states.collect{it.dateCreated + ':' + it.value}.join(', ')}]"
93 def onOff = states.findAll { it.physical || !it.type }
94 log.trace "filtered: [${onOff.collect{it.dateCreated + ':' + it.value}.join(', ')}]"
96 // This test was needed before the change to use Event rather than DeviceState. It should never pass now.
97 if (onOff[0].date.before(evt.date)) {
98 log.warn "Last state does not reflect current event, evt.date: ${evt.dateCreated}, state.date: ${onOff[0].dateCreated}"
99 result = evt.value == value && onOff[0].value == value
102 result = onOff.size() > 1 && onOff[0].value == value && onOff[1].value == value