Hi, had a though about a recipe that uses a hue light. My son is always asking that the light have different colors which means either: (i) writing the app so I can set it up to use a different color and then change it each time myself; (ii) go to the hue app to randomly change colors during the day so when the recipe is triggered things have changed or (iii) figure out a way to instruct the smart app to randomly select a hue/saturation for the light when turned on.
Does anyone know how to accomplish the last thing because that would be pretty cool.
While you can certainly generate a random number with groovy, I don’t think that is what you want. Remember there are over 65000 different hues available. As luck would usually have it, you may see a number of shades from random number generation that appear identical.
I think you would be better served to design a reasonable number of preset values, and either cycle thru those, or choose one of them randomly.
In any event, calculating a number between 1 and 20 with groovy looks like:
Ok. I made some changes to my overall app which is simple. I will test in real life, but in the simulation it appears not to change color. My concern is that this is device related NOT my program.
I have the OSRAM 73661 Smart Connected Lighting LED Flexible Strip. The device is installed properly and I can change colors directly by using the device within ST.
Here is my code:
/**
* 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.
*
* Turn It On When It Opens
*
* Author: SmartThings
*/
definition(
name: "Matthew's Under Bed Light",
namespace: "JDogg",
author: "JDogg",
description: "Turn something on when an open/close sensor opens.",
category: "Convenience",
iconUrl: "https://s3.amazonaws.com/smartapp-icons/Meta/light_contact-outlet.png",
iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Meta/light_contact-outlet@2x.png"
)
preferences {
section("When the door opens..."){
input "contact1", "capability.contactSensor", title: "Where?"
}
section("Turn on a light..."){
input "switches", "capability.colorControl", multiple: false
}
}
def init()
{
subscribe(contact1, "contact.open", contactOpenHandler)
subscribe(contact1, "contact.closed", contactClosedHandler)
}
def updated()
{
unsubscribe()
subscribe(contact1, "contact.open", contactOpenHandler)
subscribe(contact1, "contact.closed", contactClosedHandler)
}
def contactOpenHandler(evt) {
log.debug "$evt.value: $evt, $settings"
log.trace "Turning on switches: $switches"
//Get Random Number
def ranNum = new Random().nextInt(65000) + 1
def ranSat = new Random().nextInt(99) + 1
log.debug "Color is $ranNum and Saturation is $ranSat"
switches.setLevel(99)
switches.setSaturation(ranSat)
switches.setHue(ranNum)
switches.on()
}
def contactClosedHandler(evt) {
log.debug "$evt.value: $evt, $settings"
log.trace "Turning off switches: $switches"
switches.off([delay: 300 * 1000])
}
I see how to apparently use setHue, setColor etc… but they don’t seem to work.
No matter what levels I choose when the app functions (ie. the light goes on), the color, hue and brightness of the light is the last color set by the device and not a random color/hue determined by the app.
Without the entire code I am not sure what could be the problem. What I didn’t show in my code is where I got the variables “hueLevel”, “saturationLevel” etc. Are you using hue as a property or a variable (i.e. def newValue = [hue: hue, …?
I do know that the variables have to be integers, if that helps at all. Please share the code around it and we can figure out the issue…this does work for me.
/**
* 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.
*
* Turn It On When It Opens
*
* Author: SmartThings
*/
definition(
name: "Matthew's Under Bed Light",
namespace: "JDogg",
author: "JDogg",
description: "Turn something on when an open/close sensor opens.",
category: "Convenience",
iconUrl: "https://s3.amazonaws.com/smartapp-icons/Meta/light_contact-outlet.png",
iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Meta/light_contact-outlet@2x.png"
)
preferences {
section("When the door opens..."){
input "contact1", "capability.contactSensor", title: "Where?"
}
section("Turn on a light..."){
input "switches", "capability.colorControl", multiple: false
}
}
def init()
{
subscribe(contact1, "contact.open", contactOpenHandler)
subscribe(contact1, "contact.closed", contactClosedHandler)
subscribe(switches, "setColor",colorHandler)
// subscribe(switches, "switch", onOffHandler)
//subscribe(switches, "level", colorHandler)
//subscribe(switches, "hue", colorHandler)
//subscribe(switches, "saturation", colorHandler)
//subscribe(switches, "colorTemperature", tempHandler)
}
def updated()
{
unsubscribe()
subscribe(contact1, "contact.open", contactOpenHandler)
subscribe(contact1, "contact.closed", contactClosedHandler)
}
def contactOpenHandler(evt) {
log.debug "$evt.value: $evt, $settings"
log.trace "Turning on switches: $switches"
switches.on()
def colorMap = [hue:stateMap.hue.toInteger(new Random().nextInt(65000) + 1),saturation:stateMap.saturation.toInteger(new Random().nextInt(99) + 1),level:stateMap.level]
switches.setColor(colorMap)
//Get Random Number
//def ranNum = new Random().nextInt(65000) + 1
//def ranSat = new Random().nextInt(99) + 1
//switches.setLevel(99)
//switches.setSaturation(ranSat)
//switches.setHue(ranNum)
log.debug "Light is on and $colorMap"
}
def contactClosedHandler(evt) {
log.debug "$evt.value: $evt, $settings"
log.trace "Turning off switches: $switches"
switches.off([delay: 300 * 1000])
}
That worked well (I had to do the same with the saturation. I simply set the dimmer level to a certain number). In addition I eliminated the switches.on() command. Since these are technically dimmers, you can simply set the level via setColor to have it come on, and then use a setLevel to 0 to turn it off…
Does that help you at all? I didn’t write any new code outside of that, and it seemed to work for me. Let me know how you do with this. I recommend you look at the Hue Mood Lighting app in the sample code section as that has some good examples of colored lights.