Random Hue Color within a smart app


(Justin) #1

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.


Setting hue colors with hex
(Convinced ST will never be unbroken…) #2

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:

new Random().nextInt(20) + 1


(Justin) #3

Where do I paste the code? After the command to turn on?


(Justin) #4

I figured step 1 to randomize the light is to actually be able to control the light so this is the portion of the code I tried to use:

}

def showerClosed(evt){
def vsOn = switch3.currentValue(“switch”)
def bathroomState = switch2.currentValue(“switch”)
def showerState = switch1.currentValue(“switch”)

def motionState = motion1.currentState("motion")
if (evt.value == "closed" && bathroomState == "on"  && showerState == "off"  && vsOn == "on"){
switch1.setLevel(99)
switch1.setSaturation(1)
switch1.setHue(57)
switch1.on([delay: 2 * 1000])
switch2.off([delay: 3 * 1000])
switch3.off([delay: 3 * 1000])  
 log.debug "switch 3 is ${vsOn}"
log.debug "Door is Closed because you ARE taking a shower"
disable()

I figured that I would create my switch and then set the hue and saturation, but unfortunately the color did not change.

Am I missing something with this step?


(Justin) #5

Just curious if anyone had any other insight into how to do this?


(Justin) #6

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])
    }

How To: Set Custom Color and Brightness of Hue Bulbs
(Justin) #7

Anyone got any thoughts on this?

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.


(Micheal ) #8

I had a similar issue with the individual controls, and had to use the set color command:

def newValue = [hue: hueLevel, saturation: saturationLevel, level: dimLevel as Integer]
coloredLight?.setColor(newValue)


(Justin) #9

Tried that and I get

 error java.lang.NullPointerException: Cannot get property 'hue' on null object @ line 60

(Micheal ) #10

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.


(Justin) #11

Here ya go.

/**
 *  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])
    }

(Micheal ) #12

Your issue seemed to be the equation, not the command itself. When I changed:

stateMap.hue.toInteger(new Random().nextInt(65000) + 1)

to

new Random().nextInt(65000) + 1

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.


(Justin) #13

Got it! Awesome!

Thank You so much!


(Micheal ) #14

No problem…interesting concept for an app! Good work!