[SOLVED] Hue come on different colours

Hi everyone

I’m hoping for some help on this one prob easy for most of you

I have a hue compatible led strip on my stairs triggered by hue motion sensor it’s set too come on as a bright blue in the day and a nice red at night time and turns off after motion ends

I am getting bored of the blue and red color now and don’t feel like I’m making good use of what a colored strip does so,

What I’m after is random colors when the motion sensor is triggered come on a random colour but I have not got a clue how too write code for that brightness is not a concern at the moment only a nice range of random colors

Any help appertiated

The HSB in HSBType stands for “Hue Saturation Brightness”. So all you need to modify is the Hue value. This value is an integer between 0 and 359 (degrees in a circle). So all you need to do is randomly generate a number between 0 and 359 to get a random color, though saturation also has an impact on the color of the light so you might want to generate a random saturation as well, which is a number between 0 and 100 (i.e. PercentType).

You can generate a random using Math::random which returns a random number between 0.0 and 1.0. Then you can convert that to an integer between 0 and 359 using

val randHue = (Math::random * 360).intValue
val randSat = (Math::random * 100).intValue

Then send the command:

MyLight.sendCommand(randHue+","+randSat+","+MyLight.getStateAs(PercentType))

The getStateAs returns the brightness value).

1 Like

Did write a few scenes for myself - maybe you can use a bit of that:

Items:

Group GScenes
    "Alle Szenen"
    <mediacontrol>
    (Functions)

Number Scene_Hue_Color_All
    "Farb-Szenen"
    <colorlight>
    (GScenes)

Number Scene_Hue_Color_Bedroom
    "Farb-Szenen Schlafzimmer"
    <colorlight>
    (GScenes)

Number Scene_Hue_Color_Corridor
    "Farb-Szenen Flur"
    <colorlight>
    (GScenes)

Number Scene_Hue_Color_Left_Cabinet
    "Farb-Szenen linke Vitrine"
    <colorlight>
    (GScenes)

Number Scene_Hue_Color_Right_Cabinet
    "Farb-Szenen rechte Vitrine"
    <colorlight>
    (GScenes)


// Example, doesn't make sense to post all lights
Group Hue_LR_Showcase_Left
    "Hue Vitrine links"
    <light>
    (Livingroom, Items)
Switch Hue_LR_Showcase_Left_Switch
    "Hue Vitrine links Schalter"
    <light>
    (Hue_LR_Showcase_Left, GPower, GColor_Lights)
    { channel="hue:0210:ecb5fa004e07:stripe2:color" }
Dimmer Hue_LR_Showcase_Left_Brightness
    "Hue Vitrine links Helligkeit [%d %%]"
    <light>
    (Hue_LR_Showcase_Left)
    { channel="hue:0210:ecb5fa004e07:stripe2:color" }
Color Hue_LR_Showcase_Left_Color
    "Hue Vitrine links Farbe"
    <colorwheel>
    (Hue_LR_Showcase_Left, GColors)
    [ "Lighting" ]
    { channel="hue:0210:ecb5fa004e07:stripe2:color" }
Dimmer Hue_LR_Showcase_Left_Temperature
    "Hue Vitrine links Farbtemperatur [%d %%]"
    <rgb>
    (Hue_LR_Showcase_Left)
   	{ channel="hue:0210:ecb5fa004e07:stripe2:color_temperature" }
String Hue_LR_Showcase_Left_Alert
    "Hue Vitrine links Alarm"
    <alarm>
    (Hue_LR_Showcase_Left)
   	{ channel="hue:0210:ecb5fa004e07:stripe2:alert" }
Switch Hue_LR_Showcase_Left_Effect
    "Hue Vitrine links Effekt"
    <party>
    (Hue_LR_Showcase_Left)
   	{ channel="hue:0210:ecb5fa004e07:stripe2:effect" }

Rules:

val String rulesDomaine = "colorscenes.rules"

/*
HSB string values consist of three comma-separated values for 
- hue (0-360°),
- saturation (0-100%),
- and brightness (0-100%)
respectively, e.g. 
* 0   for red
* 120 for green
* 200 for turquoise
* 240 for blue
* 270 for violett
*/

var Timer rotateTimer = null
val Number timeInBetweenRotations = 10 // in minutes

/*
Set colors for all color lights
370 = warm light
400 = rotate (sync)
410 = rotate (async)
420 = color gradient
999 = scenes off
*/
rule "Set Colors"
when
    Item Scene_Hue_Color_All received command
then
    // 1. Check to see if the Rule has to run at all, if not exit.
    if( receivedCommand > 370) return;

    // Only log if rule applies
	logInfo(rulesDomaine, "Rule \"Set Colors\" triggered")

    // 2. Calculate what needs to be done.
    // if timer != null -> cancel
    rotateTimer?.cancel

    // 3. Do it. Only do actions in like sendCommand in one place at the end of the Rule.

    // Only if all color lights are off, switch them all on
    if (GColor_Lights.state == OFF) {
        GColor_Lights.sendCommand(ON)
    }

    Scene_Hue_Color_Bedroom.sendCommand(receivedCommand)
    Scene_Hue_Color_Corridor.sendCommand(receivedCommand)
    Scene_Hue_Color_Left_Cabinet.sendCommand(receivedCommand)
    Scene_Hue_Color_Right_Cabinet.sendCommand(receivedCommand)
end


rule "Rotate Colors - Syncron"
when
    Item Scene_Hue_Color_All received command 400
then
    // 1. Check to see if the Rule has to run at all, if not exit.
    if( receivedCommand != 400) return;

    // Only log if rule applies
	logInfo(rulesDomaine, "Rule \"Rotate Colors - Syncron\" triggered")

    // 2. Calculate what needs to be done.
    // if timer != null -> cancel
    rotateTimer?.cancel

    // 3. Do it. Only do actions in like sendCommand in one place at the end of the Rule.
    
    // Only if all color lights are off, switch them all on
    if (GColor_Lights.state == OFF) {
        GColor_Lights.sendCommand(ON)
    }

    rotateTimer = createTimer(now, [ |

        var Number color = (Math::random * 360.0).intValue + 1
		
		Scene_Hue_Color_Bedroom.sendCommand(color)
        Scene_Hue_Color_Corridor.sendCommand(color)
        Scene_Hue_Color_Left_Cabinet.sendCommand(color)
        Scene_Hue_Color_Right_Cabinet.sendCommand(color)

        rotateTimer.reschedule(now.plusMinutes(timeInBetweenRotations))
	])
end


rule "Rotate Colors - Asyncron"
when
    Item Scene_Hue_Color_All received command 410
then
    // 1. Check to see if the Rule has to run at all, if not exit.
    if( receivedCommand != 410) return;

    // Only log if rule applies
	logInfo(rulesDomaine, "Rule \"Rotate Colors - Asyncron\" triggered")

    // 2. Calculate what needs to be done.
    // if timer != null -> cancel
    rotateTimer?.cancel

    // 3. Do it. Only do actions in like sendCommand in one place at the end of the Rule.
    
    // Only if all color lights are off, switch them all on
    if (GColor_Lights.state == OFF) {
        GColor_Lights.sendCommand(ON)
    }

    rotateTimer = createTimer(now, [ |
		
		Scene_Hue_Color_Bedroom.sendCommand((Math::random * 360.0).intValue + 1)
        Scene_Hue_Color_Corridor.sendCommand((Math::random * 360.0).intValue + 1)
        Scene_Hue_Color_Left_Cabinet.sendCommand((Math::random * 360.0).intValue + 1)
        Scene_Hue_Color_Right_Cabinet.sendCommand((Math::random * 360.0).intValue + 1)

        rotateTimer.reschedule(now.plusMinutes(timeInBetweenRotations))
	])
end


rule "Color Gradient"
when
    Item Scene_Hue_Color_All received command 420
then
    // 1. Check to see if the Rule has to run at all, if not exit.
    if( receivedCommand != 420) return;

    // Only log if rule applies
	logInfo(rulesDomaine, "Rule \"Color Gradient\" triggered")

    // 2. Calculate what needs to be done.
    // if timer != null -> cancel
    rotateTimer?.cancel

    // 3. Do it. Only do actions in like sendCommand in one place at the end of the Rule.
    
    // Only if all color lights are off, switch them all on
    if (GColor_Lights.state == OFF) {
        GColor_Lights.sendCommand(ON)
    }

    rotateTimer = createTimer(now, [ |

        var Number color = Integer::parseInt(Hue_LR_Showcase_Left_Color.state.toString.split(",").get(0))
        if (color >= 359) {
            color = 0
        } else {
            color = color + 1
        }        
		
		Scene_Hue_Color_Bedroom.sendCommand(color)
        Scene_Hue_Color_Corridor.sendCommand(color)
        Scene_Hue_Color_Left_Cabinet.sendCommand(color)
        Scene_Hue_Color_Right_Cabinet.sendCommand(color)

        rotateTimer.reschedule(now.plusSeconds(60))
	])
end


rule "End Timer"
when
    Item GColor_Lights changed from ON to OFF or
    Item Scene_Hue_Color_All changed to 999
then
	logInfo(rulesDomaine, "Rule \"End Timer\" triggered")
    rotateTimer?.cancel
    //Scene_Hue_Color_All.sendCommand(999)
end


rule "Resume Timer"
when
    Item GColor_Lights changed from OFF to ON
then
    if (Scene_Sunrise.state == ON) return
	logInfo(rulesDomaine, "Rule \"Resume Timer\" triggered with state: " +
            Scene_Hue_Color_All.state)
    Scene_Hue_Color_All.sendCommand(Scene_Hue_Color_All.state as Number)
end


rule "Set Color Bedroom"
when
    Item Scene_Hue_Color_Bedroom received command
then
    // 1. Check to see if the Rule has to run at all, if not exit.
    if( receivedCommand > 360) return;

    // Only log if rule applies
	logInfo(rulesDomaine, "Rule \"Set Color Bedroom\" triggered")

    // 2. Calculate what needs to be done.
    var String color = receivedCommand.toString
    var String brightness = Hue_BR_Wardrobe_Color.state.toString.split(",").get(2)
    var String hsb_triple = color + ",100," + brightness

    // 3. Do it. Only do actions in like sendCommand in one place at the end of the Rule.
    Hue_BR_Wardrobe_Color.sendCommand(hsb_triple)
end


rule "Set Color Corridor"
when
    Item Scene_Hue_Color_Corridor received command
then
    // 1. Check to see if the Rule has to run at all, if not exit.
    if( receivedCommand > 360) return;

    // Only log if rule applies
	logInfo(rulesDomaine, "Rule \"Set Color Corridor\" triggered")

    // 2. Calculate what needs to be done.
    var String color = receivedCommand.toString
    var String brightness = Hue_C_Lamp_Color.state.toString.split(",").get(2)
    var String hsb_triple = color + ",100," + brightness

    // 3. Do it. Only do actions in like sendCommand in one place at the end of the Rule.
    Hue_C_Lamp_Color.sendCommand(hsb_triple)
end


rule "Set Color Left Cabinet"
when
    Item Scene_Hue_Color_Left_Cabinet received command
then
    // 1. Check to see if the Rule has to run at all, if not exit.
    if( receivedCommand > 360) return;

    // Only log if rule applies
	logInfo(rulesDomaine, "Rule \"Set Color Left Cabinet\" triggered")

    // 2. Calculate what needs to be done.
    var String color = receivedCommand.toString
    var String brightness = Hue_LR_Showcase_Left_Color.state.toString.split(",").get(2)
    var String hsb_triple = color + ",100," + brightness

    // 3. Do it. Only do actions in like sendCommand in one place at the end of the Rule.
    Hue_LR_Showcase_Left_Color.sendCommand(hsb_triple)
end


rule "Set Color Right Cabinet"
when
    Item Scene_Hue_Color_Right_Cabinet received command
then
    // 1. Check to see if the Rule has to run at all, if not exit.
    if( receivedCommand > 360) return;

    // Only log if rule applies
	logInfo(rulesDomaine, "Rule \"Set Color Right Cabinet\" triggered")

    // 2. Calculate what needs to be done.
    var String color = receivedCommand.toString
    var String brightness = Hue_LR_Showcase_Right_Color.state.toString.split(",").get(2)
    var String hsb_triple = color + ",100," + brightness

    // 3. Do it. Only do actions in like sendCommand in one place at the end of the Rule.
    Hue_LR_Showcase_Right_Color.sendCommand(hsb_triple)
end


rule "Set Color Bedroom - Warm Light"
when
    Item Scene_Hue_Color_Bedroom received command 370
then
    // 1. Check to see if the Rule has to run at all, if not exit.
    if( receivedCommand != 370) return;

    // Only log if rule applies
	logInfo(rulesDomaine, "Rule \"Set Color Bedroom - Warm Light\" triggered")

    // 2. Calculate what needs to be done.

    // 3. Do it. Only do actions in like sendCommand in one place at the end of the Rule.
    Hue_BR_Wardrobe_Temperature.sendCommand(100)
end

rule "Set Color Corridor - Warm Light"
when
    Item Scene_Hue_Color_Corridor received command 370
then
    // 1. Check to see if the Rule has to run at all, if not exit.
    if( receivedCommand != 370) return;

    // Only log if rule applies
	logInfo(rulesDomaine, "Rule \"Set Color Corridor - Warm Light\" triggered")

    // 2. Calculate what needs to be done.

    // 3. Do it. Only do actions in like sendCommand in one place at the end of the Rule.
    Hue_C_Lamp_Temperature.sendCommand(100)
end

rule "Set Color Left Cabinet - Warm Light"
when
    Item Scene_Hue_Color_Left_Cabinet received command 370
then
    // 1. Check to see if the Rule has to run at all, if not exit.
    if( receivedCommand != 370) return;

    // Only log if rule applies
	logInfo(rulesDomaine, "Rule \"Set Color Left Cabinet - Warm Light\" triggered")

    // 2. Calculate what needs to be done.

    // 3. Do it. Only do actions in like sendCommand in one place at the end of the Rule.
    Hue_LR_Showcase_Left_Temperature.sendCommand(100)
end

rule "Set Color Right Cabinet - Warm Light"
when
    Item Scene_Hue_Color_Right_Cabinet received command 370
then
    // 1. Check to see if the Rule has to run at all, if not exit.
    if( receivedCommand != 370) return;

    // Only log if rule applies
	logInfo(rulesDomaine, "Rule \"Set Color Right Cabinet - Warm Light\" triggered")

    // 2. Calculate what needs to be done.

    // 3. Do it. Only do actions in like sendCommand in one place at the end of the Rule.
    Hue_LR_Showcase_Right_Temperature.sendCommand(100)
end

Sitemap:

		Selection item=Scene_Hue_Color_All
			label="Farbszenen"
			mappings=[
				999="Aus",
				0="Rot",
				120="Grün",
				200="Türkis",
				240="Blau",
				270="Violett",
				370="Warmes Licht",
				400="Wechselnde Farben (synchronisiert)",
				410="Wechselnde Farben (asynchron)",
				420="Farbverlauf"
			]
		Switch item=Hue_LR_Showcase_Right_Switch
			label="Vitrine rechts"
			mappings=[ON="Ein"]
			visibility=[Hue_LR_Showcase_Right_Switch != ON]
		Text item=Hue_LR_Showcase_Right_Switch
			label="Vitrine rechts [MAP(lights.map):%s]"
			valuecolor=[ON="green", OFF="maroon"]
			visibility=[Hue_LR_Showcase_Right_Switch == ON] {
			Default item=Hue_LR_Showcase_Right_Brightness
				label="Helligkeit"
			Default item=Hue_LR_Showcase_Right_Temperature
				label="Farbtemperatur"
			Default item=Hue_LR_Showcase_Right_Color
				label="Farbton"
			Switch item=Hue_LR_Showcase_Right_Switch
				label="Lichtband"
				mappings=[OFF="Aus"]
			Switch item=Hue_LR_Showcase_Right_Alert
				label="Alarm [MAP(lights.map):%s]"
				mappings=[NONE="Aus", SELECT="Alarm", LSELECT="Langer Alarm"]
			Selection item=Scene_Hue_Color_Right_Cabinet
				label="Farbszenen"
				mappings=[
					0="Rot",
					120="Grün",
					200="Türkis",
					240="Blau",
					270="Violett",
					370="Warmes Licht"
				]
1 Like

Hi @rlkoshak & @fex thanks for the reply’s

This is the first time i have tried this and still haven’t got a clue how too use what you sent so my rule is not working and i’m not sure how too lay it out do i need to create any extra items or is it just the way i have layed out my rule?

rule "testcolormath"
when
	Item TestSwitch1 changed to ON 
then
	val randHue = (Math::random * 360).intValue
	val randSat = (Math::random * 100).intValue
	BULB6LRMAIN1_Color.sendCommand(randHue+","+randSat+","+BULB6LRMAIN1_Color.getStateAs(PercentType))
end

There is an infinity of ways a Rule can be “not working.” Not working how?

Add logging to verify whether or not the Rule is running.
Add logging to see what randHue and randSat are being set to
Add logging to see what BULB6LRMAIN1_Color.getStateAs(PercentType) returns

1 Like

from what I can tell the rule runs fine and does receive random hue & saturation values thanks for that the problem seems to be that the bulb doesn’t light as it receives a 0 brightness how would i send a non random brightness value along with the random results?

how do you add logging for these i’m only familiar with simple log info but looking at the log i can see it reciving random values :wink:

rule "testcolormath"
when
	Item TestSwitch1 changed to ON 
then
	logInfo("log", "started")
	val randHue = (Math::random * 360).intValue
	val randSat = (Math::random * 100).intValue
	BULB6LRMAIN1_Color.sendCommand(randHue+","+randSat+","+BULB6LRMAIN1_Color.getStateAs(PercentType))


2019-11-13 21:06:26.238 [INFO ] [g.eclipse.smarthome.model.script.log] - started
2019-11-13 21:06:26.289 [ome.event.ItemCommandEvent] - Item 'BULB6LRMAIN1_Color' received command 15,54,0
2019-11-13 21:06:26.295 [INFO ] [g.eclipse.smarthome.model.script.log] - ended

Replace BULB6LRMAIN1_Color.getStateAs(PercentType) with what ever brightness you want. That part sends the current brightness to the light. If the current brightness is 0, it sends 0.

1 Like

Thanks for that Rich got it working one last thing how do i stop the light from changing when already on?

rule "Random color Led strip staris"
when
	Item Frontdoor_Motion changed to ON or
	Item Landing_Motion changed to ON
then
if( STRIP1STSTRIP_Color.state == OFF ) {
	val randHue = (Math::random * 360).intValue
	val randSat = (Math::random * 100).intValue
	STRIP1STSTRIP_Color.sendCommand(randHue+","+randSat+","+ "100")
}
end

You can STRIP1STSTRIP_Color.getStateAs(OnOffType) == ON to tell if the light is on. Or you can use (STRIP1STSTRIP_Color.getStateAs(PercentType) as Number) > 0.

1 Like

Thanks as always appreciate the help works great

The rule is just alot slower too run now I know nothing can be done about that its due too latency between the communications between hue motion sensor, the bridge, OH and the Strip works perfectly now :slightly_smiling_face:

Thanks again

Thanks @fex your post also helped me troubleshoot what was wrong with my rule :slight_smile:

1 Like

Alterntively you could simply switch on the effect-channel:

Not exactly random but looping through all the colours.

1 Like

Hi @job thanks for the reply I know about the color loop channel its not quite what I was after im saving that for other things :wink: