How to handle a HUE Color light within JS

Hello community,

I tried to enhance my rule, which handels hue white light quite perfect, to color lights. It’s seems not be as easy as expected, because I cant access the brightness directly because the color lights use the HSBType.

With the following code I get the current parameters of the bulb.

var Hue, Brightness, Saturation ;
var things = Java.type('org.openhab.core.model.script.actions.Things');
var currentState = Java.type('org.openhab.core.library.types.HSBType');


currentState = itemRegistry.getItem('Huecolorlamp2TestLeuchte_Farbe').getState();
Hue = currentState.getHue();
Brightness = currentState.getBrightness();
Saturation = currentState.getSaturation();

So far so good. Then I modify one parameter, e.g. the brightness, to send the new parameter set back to the lamp …

  events.sendCommand('Huecolorlamp2TestLeuchte_Farbe', Hue.toString + "," + Saturation.toString + "," + Brightness.toString);

But this won’t work. I tried to find some simple example … but I didn’t found one. It seams that the toString methode is not called correct, but … I have to less JavaScript experiences. Do I have to include a further library … don’t know

Can someone give me the right hint to solve this? Just a piece of code that addresses the same problem.

Thanks and stay healthy

In JS when you call a function you must call it as a function. There is no toString, it’s toString().

But if you just want to change the brightness, you can sendCommand(newBrightness) to a Color Item and it will only adjust the brightness. To turn it on or off you can sendCommand(“ON”).

Okay that was as easy as expected … but … if I want to change the color, what have I to then? Is there an other simple command to change the color or is it then necessary to use the HSBType?

For the record only, I changed the toString as I understood this to

  events.sendCommand('Huecolorlamp2TestLeuchte_Farbe', toString(Hue) + "," + toString(Saturation) + "," + toString(Brightness));

But this results in errors …

==> openhab.log <==
2022-01-07 16:46:55.339 [WARN ] [internal.defaultscope.ScriptBusEvent] - Command '[object Undefined],[object Undefined],[object Undefined]' cannot be parsed for item 'Huecolorlamp2TestLeuchte_Farbe (Type=ColorItem, State=40,55,8, Label=AZ_Licht, Category=ColorLight, Tags=[Control, Light], Groups=[Huecolorlamp2TestLeuchte])'.

You need all three parts of the HSB to define a color. So yes, to change the color you need to use an HSB type.

Not toString(Hue), Hue.toString()

Okay … I would think that I have tested this befor, but … well … now it works correctly.

  events.sendCommand('Huecolorlamp2TestLeuchte_Farbe', Hue.toString() + "," + Saturation.toString() + "," + Brightness.toString());

Thank you

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.