[SOLVED] Help with Hue color rule

After having Hue lights for years now I figured I should finally automate some of it…like setting colors of christmas time.

It seems to me the best channel to trigger things off of is the color channel. That one seems to trigger the most reliably whether light is manipulated through the hue app, google, or OH itself.

So, I’d like the rule to trigger like

Item dw1Color changed from ??,??,0

I haven’t had any lucky finding how to trigger it like that.

I saw a suggestion from rich to just do it off of changes

Item dw1Color changed which would work if maybe I could figure out how to check the previous state

I did find some information in a topic from @rlkoshak but I don’t know well enough how to put it together.

say something like:

if ( (dw1Color.state as HSBType).brightness.previousState().state == 0 )

but that doesn’t actually work.

Ideally i’d like to work as the triggering item but i’ll take checking the previous state if that isn’t possible.

Little help please?

Here a few pointers that may help:

You probably figured out that this does not exist, for a full list of triggers see the docs: https://www.openhab.org/docs/configuration/rules-dsl.html#rule-triggers

But as you already saw, you can work of changes, this post here hopefully helps (note: instead of brightness you need to use getBrightness):

rule "Hue changed"
    HueLight changed
    logInfo("PREVIOUS STATE", previousState.toString)
    logInfo(NEW STATE", HueLight.state.toString)

See: https://www.openhab.org/docs/configuration/rules-dsl.html#implicit-variables-inside-the-execution-block

Thanks guys.

I’m not so much after the brightness changes as I am in when brightness changes from 0 (zero) to some other value. I only want to manipulate the color one time when the light turns on so that I’m not undoing any other changes to the light if someone (me, wife, etc) uses the hue app or whatever.

I am not quite sure how to read this. I am setting my HSB values only with the app (and hardly ever by OH2). My hue lights all remember their HSB state when switched on. I simply switch them on or off (rather than using HSB values) with OH2, this way they come on with the same HSB value when they were switched off. (and big bonus, if you update your lights, they will even remember their last status when the power goes out thanks to a new hue app feature).
So if all you want is to preserve the last color and brightness settings, just use the on/off switch function.

I didn’t see this spelled out exactly why this like didn’t work.

Only an Item has a previousState method. You are trying to call previousState on a party of a State. You need

if((dw1Color.previousState().state as HSBType). brightness == 0)
1 Like

In my opinion its quite a bit too complicated to have hue scenes in OH. My workaround is the all4hue app for programming scenes and colors. Through nodered and the hue plugin I can see and control all scenes from my hue bridge. So the scene is just an item in OH.
My switches and motion sensors are although programmed with rules with the all4hue app. But with the new hue binding, its just awesome to control things with the hue switches in parallel with OH and the hue internal rules.

I have no personal experience with trying to do this, but in other posts (I have linked one above), I have seen this written as:

if((dw1Color.previousState().state as HSBType).getBrightness().intValue() == 0)

Maybe it works this way

You shouldn’t need the intValue though the Rules DSL can be finiky sometimes. It would be better to use as Number instead though, especially on RPi. For some reason it had a really hard time parsing rules that use primitives.

1 Like

I’ll play with it in a few hours, thanks for the suggestion. The point of all this is there’s only 1 sure fire way to see the state of the hue color bulbs turning on from an off state. It’s only at that point that I want to set a color and only once. Reason being is the light gets adjusted after it’s on its likely someone is doing it on purpose so I don’t want the rule firing and setting the light back.

Light turns on, openhab sets a color, end of anything I intend to do with the light via openhab other than turning it back off.

As some of you with hue bulbs know sometimes they randomly decide to go back to White instead of the last color they were. This rule would be good for handling that if you’re doing little seasonal scenes.

Hope that all makes sense. I’ll try Rich’s suggestion tonight. The things I tried generally errored complaining about something being null but the state is persisted and I verified it with rest and a loginfo. Something about the way I constructed the check wasn’t right

Rich, as always, thank you so much!

some day i’m going to owe you a beer or something :beers:

This is exactly what I’m after (and it worked).

1 Like

Here’s the simple idea behind all this. Say from black friday until the 5th every year I want the lights to go green-red-green if they come on via the hue app somehow.
Of course actions that I initiate via OH will just set the lights the way I want. This rule is like a safety net:
I know there’s some calendar way to do this, I’ve seen examples thrown around but I haven’t looked into it just yet.

rule "Set Driveway Lights Color"
	Item dw1Color changed
	if( (dw1Color.previousState().state as HSBType).brightness == 0 ) {
		if ( now.getDayOfYear > 332 || now.getDayOfYear < 5 ) {
rule "Set Driveway Lights Color"
    Item dw1Color changed
    if ((previousState as HSBType).brightness == 0 && (now.getDayOfYear > 332 || now.getDayOfYear < 5)) {

I’m planning for more than “scene” :stuck_out_tongue:

not sure what changed - quite a lot i’m sure since 2.4 but now I have to use this line instead of Rich’s to get this working.

In case anyone wanders across this thread…

A quick and dirty hack for a couple of scenes triggered from a fibaro dimmer 2, might be useful to you:

rule "Kitchen Party Mode ON" when Item PartyMode changed from OFF to ON then
PartyTimer = createTimer(now, [ |
if(PartyMode.state == ON){
	 sendCommand(KitchenSpots1, (((Math::random * 255).intValue)) + "," + 100 + "," + 70)

rule "Kitchen Party Mode OFF" when Item  PartyMode changed from ON to OFF then
PartyTimer = null

rule "Kitchen Scene" when Item KitchenLightScene received update 14 then
if(KitchenLightDimmer.state == OFF).   {KitchenLightDimmer.sendCommand(ON)}
if(KitchenScene.state == 1 || KitchenScene.state == NULL)

else if(KitchenScene.state == 2)

else if(KitchenScene.state == 3)

else if(KitchenScene.state == 4)

else if(KitchenScene.state == 5)

else if(KitchenScene.state == 6)

else if(KitchenScene.state == 7)

else if(KitchenScene.state == 8)

else if(KitchenScene.state == 9)

else if(KitchenScene.state == 10)

else if(KitchenScene.state == 11)

else if(KitchenScene.state == 12)

else if(KitchenScene.state == 13)

else if(KitchenScene.state == 14)


rule "Kitchen Bottom" when Item     KitchenLightScene3 received update 26 then
if(PlynthLight.state == OFF || PlynthLightColour.state == NULL)    {PlynthLight.postUpdate(ON)PlynthLightColour.sendCommand("39,1,100")}
else     {PlynthLight.postUpdate(OFF)PlynthLightColour.sendCommand("39,1,0")}

rule "Kitchen Party Mode" when Item     KitchenLightScene received update 24 then
if(KitchenLightDimmer.state == OFF).   {KitchenLightDimmer.sendCommand(ON)}
if(PartyMode.state == OFF || PartyMode.state == NULL){PartyMode.sendCommand(ON)}
if(PartyMode.state == ON).   {PartyMode.sendCommand(OFF)}

@oeo Will give you the host hopefully for scenes and random lights for a disco