This is either a bug in my code or Hue in general (or something wrong with SmartThings).
Wrote the following app to select a hue randomly from a supplied list. Seems pretty simple, and values ARE updated in the sim. But as soon as I run it without the sim monitoring it, the lamps only come up in red (even if red is not in the random hues list), and subsequent ‘ons’ do nothing.
What have I missed?
definition(
name: "Random Color Switch",
namespace: "soletc.com",
author: "Scottin Pollock",
description: "When a switch is pressed, turns on Hues to Random color.",
category: "My Apps",
iconUrl: "http://solutionsetcetera.com/stuff/STIcons/HueLogo.png",
iconX2Url: "http://solutionsetcetera.com/stuff/STIcons/HueLogo@2x.png")
preferences {
section("When the following is turned on and off...") {
input name: "master", title: "Which Switch?", type: "capability.switch", required: true
}
section("Turn on or off all of these Hues") {
input "hues", "capability.colorControl", title: "Which Hue Bulbs?", required:false, multiple:true
input "saturation", "number", title: "Saturation (0-255)", required: false, multiple:false
input "lightLevel", "number", title: "Brightness (0-255)", required: false, multiple:false
input "RCVs", "text", title: "Random Hues (0-65535)", required: false, multiple:false
}
}
def installed() {
subscribeToEvents()
}
def updated() {
unsubscribe()
subscribeToEvents()
}
def subscribeToEvents() {
subscribe(master, "switch.on", onHandler, [filterEvents: false])
subscribe(master, "switch.off", offHandler, [filterEvents: false])
}
def onHandler(evt) {
doHUE()
}
def offHandler(evt) {
hues.off()
}
def doHUE() {
def saturationInt = saturation / 255 * 100 as Integer
def lightLevelInt = lightLevel / 255 * 100 as Integer
def theHUE = randomHUE() as Integer
log.debug theHUE
def colorInt = theHUE / 65535 * 100 as Integer
log.debug "${colorInt} ${saturationInt} ${lightLevelInt}"
def hueSETTINGS = [hue: colorInt, saturation: saturationInt, level: lightLevelInt]
log.debug hueSETTINGS
hues*.setColor(hueSETTINGS)
}
def randomHUE() {
def result = RCVs[new Random().nextInt(RCVs.size())]
}