Controlling Color items

Hi, I am trying to set up an MQTT controlled LED strip analogue to https://github.com/openhab/openhab/wiki/Samples-Rules#how-to-use-colorpicker-widget-with-knxdali-rgb-led-stripe

I don’t think I changed anything but the variable names in my code:

import org.openhab.core.library.types.*

    var HSBType BH_Livingroom_hsbValue
    var String  BH_Livingroom_redValue
    var String  BH_Livingroom_greenValue
    var String  BH_Livingroom_blueValue

    rule "Set BH_Livingroom_RGB value"
    when
            Item BH_Livingroom_RGB changed
    then
            hsbValue = BH_Livingroom_RGB.state as HSBType

            BH_Livingroom_redValue   = BH_Livingroom_hsbValue.red.intValue.toString
            BH_Livingroom_greenValue = BH_Livingroom_hsbValue.green.intValue.toString
            BH_Livingroom_blueValue  = BH_Livingroom_hsbValue.blue.intValue.toString

            sendCommand( BH_Livingroom_LedR, BH_Livingroom_redValue )
            sendCommand( BH_Livingroom_LedG, BH_Livingroom_greenValue )
            sendCommand( BH_Livingroom_LedB, BH_Livingroom_blueValue )
    end

However, I get

2016-09-07 21:09:27.490 [ERROR] [o.o.c.s.ScriptExecutionThread ] - Error during the execution of rule 'Set BH_Livingroom_RGB value': org.eclipse.xtext.util.PolymorphicDispatcher$NoSuchMethodException: Couldn't find method ''_assignValue'' for objects [JvmVoid:  (eProxyURI: bh_livingroom_rgb.rules#xtextLink_::0.2.0.2.0.0::0::/1), <null> hsbValue <XCastedExpressionImpl>, 4.096385542168675,97.6470588235294,100, org.eclipse.xtext.xbase.interpreter.impl.DefaultEvaluationContext@59cf8bff, org.eclipse.xtext.util.CancelIndicator$1@26a7d4e0]

I have no idea what is wrong here.

Also, I need the reverse channel, since the device can be controlled from other sides. For this purpose, it reports it’s state to an MQTT topic. I set autoupdate to false for the items and configured the back channel. How do I set up the rule, though? It is not quite clear to me how to work with the HSBType variable (and why the values are cast to String in the forward rule).

These are the items:

Dimmer BH_Livingroom_LedR     "LED Red"               <dimmer>        (All) { mqtt=">[home-le:home-le/bh/livingroom/light/indirect/r/cmd:command:*:default],<[home-le:home-le/bh/livingroom/light/indirect/r/state:state:default]", autoupdate="false", sendFrequency="200" }
Dimmer BH_Livingroom_LedG     "LED Green"             <dimmer>        (All) { mqtt=">[home-le:home-le/bh/livingroom/light/indirect/g/cmd:command:*:default],<[home-le:home-le/bh/livingroom/light/indirect/g/state:state:default]", autoupdate="false", sendFrequency="200" }
Dimmer BH_Livingroom_LedB     "LED Blue"              <dimmer>        (All) { mqtt=">[home-le:home-le/bh/livingroom/light/indirect/b/cmd:command:*:default],<[home-le:home-le/bh/livingroom/light/indirect/b/state:state:default]", autoupdate="false", sendFrequency="200" }
Color  BH_Livingroom_RGB      "RGB Light"             <slider>

Come on, people, this is an official example that doesn’t work for some reason (yeah, or I screwed up somewhere :)). Can somebody have look or give me debug advise?

Pretty please?

Hi Kvolt

Remember that this is an open source project, and the wiki is open to anyone to contribute to.

I can see in your rule that you have named a variable incorrectly, you declare the variable:

    var HSBType BH_Livingroom_hsbValue

and you try to use it:

    hsbValue = BH_Livingroom_RGB.state as HSBType

Can you see where the mistake is?

Yes, of course. Damit. So I did screw up :slight_smile: Thanks for noticing and sorry for wasting your time.

I am also struggling with the reverse rule. I tried this, but neither does it work, nor do I get any errors:

rule "Set BH_Livingroom_RGB value reverse"
when
        BH_Livingroom_LedR changed or
        BH_Livingroom_LedG changed or
        BH_Livingroom_LedB changed
then
        BH_Livingroom_hsbValue = BH_Livingroom_RGB.state as HSBType

        BH_Livingroom_hsbValue.red.intValue = BH_Livingroom_LedR.state.intValue
        BH_Livingroom_hsbValue.green.intValue = BH_Livingroom_LedG.state.intValue
        BH_Livingroom_hsbValue.blue.intValue = BH_Livingroom_LedB.state.intValue

        sendUpdate( BH_Livingroom_RGB, BH_Livingroom_hsbValue )
end

I guess I am overwriting all of BH_Livingroom_hsbValue, but it shouldn’t be a problem in general to initialize like that, should it?

Color items are particularly tricky.

Though your syntax is still broken.

rule "Set BH_Livingroom_RGB value reverse"
when
        BH_Livingroom_LedR changed or
        BH_Livingroom_LedG changed or
        BH_Livingroom_LedB changed

Should be

rule "Set BH_Livingroom_RGB value reverse"
when
       Item BH_Livingroom_LedR changed or
       Item BH_Livingroom_LedG changed or
       Item BH_Livingroom_LedB changed

You might do better with “updated”. When you are messing with colors you are sending out hundreds of values. I am not sure how your reverse thing works but pictures a color wheel and the RGB values, then start sliding round the wheels and those numbers will be going crazy. You are asking your rule to do something when any of them change, and as they are going to be changing at the same time the amount of potential triggers is huge :slight_smile:

Is all your MQTT working as as far as you know? Can you send and receive commands as you are expecting to?

Have you tried Node Red? http://flows.nodered.org/ You might have more luck cobbling together something outside of openHAB and use openHAB as the main hub.

Or seeing if there is a Python script you can play with and trigger with MQTT or a executable item thingy, that’s what I do for some stuff. Life’s too short :slight_smile:

Thanks for your help. Yes, I noted my broken syntax. also, there was no “sendUpdate()”. Unfortunately, none of that is reported in the logs and the designer, which I started to have a look what is going on there, only reported errors after a first change in the editor there.

This is now working pretty well:

import org.openhab.core.library.types.*

    var HSBType BH_Livingroom_hsbValue
    var String  BH_Livingroom_redValue
    var String  BH_Livingroom_greenValue
    var String  BH_Livingroom_blueValue

    rule "Set BH_Livingroom_RGB value"
    when
            Item BH_Livingroom_RGB received command
    then
            BH_Livingroom_hsbValue = BH_Livingroom_RGB.state as HSBType

            BH_Livingroom_redValue   = BH_Livingroom_hsbValue.red.intValue.toString
            BH_Livingroom_greenValue = BH_Livingroom_hsbValue.green.intValue.toString
            BH_Livingroom_blueValue  = BH_Livingroom_hsbValue.blue.intValue.toString

            sendCommand( BH_Livingroom_LedR, BH_Livingroom_redValue )
            sendCommand( BH_Livingroom_LedG, BH_Livingroom_greenValue )
            sendCommand( BH_Livingroom_LedB, BH_Livingroom_blueValue )
    end

    rule "Set BH_Livingroom_RGB value reverse"
    when
            Item BH_Livingroom_LedR changed or
            Item BH_Livingroom_LedG changed or
            Item BH_Livingroom_LedB changed
    then
            var red = (BH_Livingroom_LedR.state as DecimalType).intValue
            var green = (BH_Livingroom_LedG.state as DecimalType).intValue
            var blue = (BH_Livingroom_LedB.state as DecimalType).intValue
            var color = new java.awt.Color( red, green, blue )
            var hsb = new HSBType(color) as HSBType

            postUpdate( BH_Livingroom_RGB, hsb )
    end

I might add a very short lock for the reverse rule when the forward rule is triggered, but it does still work. Note that I changed the forward rule to “received command”