/** * Copyright 2015 SmartThings * * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except * in compliance with the License. You may obtain a copy of the License at: * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License * for the specific language governing permissions and limitations under the License. * * Power Allowance * * Author: itsamti */ definition( name: "Automate Lights", namespace: "itsamti", author: "itsamti", description: "Turn On/Off Light based on event of other light.", category: "Green Living", iconUrl: "https://s3.amazonaws.com/smartapp-icons/Meta/light_outlet.png", iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Meta/light_outlet@2x.png" ) preferences { section("Which Driver Switch On/Off...") { input "theSwitch", "capability.switch" , multiple: true } section("Which Switch On/Off based on above event...") { input "theSwitchEvent", "capability.switch" , multiple: true } section("Turn it off how many seconds later?") { input "secondsLater", "number", title: "When?" } section("Turn it off if Driver Switch On and Vice-Versa?") { input "InvertVal", "boolean", title: "Invert?" } } def installed() { log.debug "Installed with settings: ${settings}" subscribe(theSwitch, "switch.on", switchOnHandler, [filterEvents: false]) subscribe(theSwitch, "switch.off", switchOffHandler, [filterEvents: false]) } def updated() { log.debug "Updated with settings: ${settings}" unsubscribe() subscribe(theSwitch, "switch.on", switchOnHandler, [filterEvents: false]) subscribe(theSwitch, "switch.off", switchOffHandler, [filterEvents: false]) } def switchOnHandler(evt) { log.debug "Switch ${theSwitchEvent} turned: ${evt.value}" def delay = secondsLater if (InvertVal != "true") { log.debug "Turning On in ${secondsLater} seconds (${delay}seconds)" runIn(delay, turnOnSwitch) } else { log.debug "Turning Off in ${secondsLater} seconds (${delay}seconds)" runIn(delay, turnOffSwitch) } } def switchOffHandler(evt) { log.debug "Switch ${theSwitchEvent} turned: ${evt.value}" def delay = secondsLater if (InvertVal != "false") { log.debug "Turning On in ${secondsLater} seconds (${delay}seconds)" runIn(delay, turnOnSwitch) } else { log.debug "Turning Off in ${secondsLater} seconds (${delay}seconds)" runIn(delay, turnOffSwitch) } } def turnOnSwitch() { theSwitchEvent.on() } def turnOffSwitch() { theSwitchEvent.off() }