Skip to content

Instantly share code, notes, and snippets.

@KDTrey
Last active February 7, 2017 17:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KDTrey/a4e70810d6192df575e30e8df33a706f to your computer and use it in GitHub Desktop.
Save KDTrey/a4e70810d6192df575e30e8df33a706f to your computer and use it in GitHub Desktop.
/**
* Color Alternator
*
* Copyright 2016 Dominique Adkins
*
* 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.
*
*/
//This code is to be used in connection with SmartThings
definition(
name: "Color Alternator",
namespace: "KDTrey",
author: "Dominique Adkins",
description: "Switches the color of a bulb every X minutes",
category: "Convenience",
iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png",
iconX3Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png")
preferences {
section("Using this:") {
input "master", "capability.colorControl", multiple: false, required: true, title: "Master device"
}
section("Control these:") {
input "slaves", "capability.colorControl", multiple: true, required: true, title: "Slave devices"
}
}
def installed() {
log.debug "Installed with settings: ${settings}"
initialize()
}
def updated() {
log.debug "Updated with settings: ${settings}"
unsubscribe()
initialize()
}
def initialize() {
subscribe(master, "switch.on", onHandler)
subscribe(master, "switch.off", offHandler)
subscribe(master, "colorRed", setColorHandler1)
subscribe(master, "colorGreen", setColorHandler2)
}
def onHandler(evt) {
slaves.on()
runEvery5Minutes(setColorHandler1)
}
def offHandler(evt) {
unschedule()//cancels all schedule methods
slaves.off()
}
def setColorHandler1(evt){
//sets bulb to green
slaves.setHue(120)
slaves.setSaturation(100)
runIn(60, setColorHandler2)//changes to green in 1 minute
sendEvent(name: "color1", value: evt, isStateChange: true)
}
def setColorHandler2(evt){
//sets bulb to red
slaves.setHue(0)
slaves.setSaturation(100)
runIn(60, setColorHandler1)//changes to red in 1 minute
sendEvent(name: "color2", value: evt, isStateChange: true)
}
// TODO: implement event handlers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment