Issues creating HSBType from string

I am trying to create a HSBType variable in ECMAScript from a value I read from a lamp.
Easy go, I thought and started with importing the type:

var HSBType = Java.type('org.openhab.core.library.types.HSBType');

The documentation says that I can call the constructor as such:

HSBType(String value)
HSBType(DecimalType h, PercentType s, PercentType b)

Looks fitting to what my readings are…
items.Deckenlampe_Duschbad_Master_Farbe.state comes from a ColorLight control (Philips Hue Binding) and delivers a value looking as 37,58,0.

But no matter what I do, I cannot construct a HSBType object from this. I even tried manually in many different ways:

var HSB = HSBType(items.Deckenlampe_Duschbad_Master_Farbe.state);

var lampe = items.Deckenlampe_Duschbad_Master_Farbe.state;
var HSB = HSBType(lampe);

var HSB = HSBType(37,58,0);
var HSB = HSBType("37,58,0");
var HSB = HSBType(37.0,58.0,0.0);
var HSB = HSBType("37.0,58.0,0.0");

All of those lead to the same error:
TypeError: execute on org.openhab.core.library.types.HSBType failed due to: Message not supported.

Interestingly, test-wise calling the fromRGB constructor works!

var HSB = HSBType.fromRGB(1, 2, 3);

creates a HSBType object on which I can use the setter and getter functions as expected. But this is way useless as my intention actually is to create a HSBType object from HSB values… :crazy_face:
What am I doing wrong…?

Exactly this code or do you comment and only have one var HSB = line. If exactly this code, well the first one is wrong so you will always get this error.

For one, to create a new Object in Java (that’s what you are working with when you have Java.type) you need to use new.

The ones using numbers won’t work. 37 is a JavaScript Number, not a Java org.openhab.core.library.types.DecimalType. Similarly, 58 and 0 are JavaScript Numbers, not Java org.openhab.core.library.types.PercentType Objects.

"37.0,58.0,0.0" won’t work because PercentType doesn’t support decimal places. They must be an integer between 0 and 100.

If you add the new keyword the two valid ones are:

var HSBType = Java.type('org.openhab.core.library.types.HSBType');
var lampe = items.Deckenlampe_Duschbad_Master_Farbe.state; // String "37,58,0"
var HSB1 = new HSBType(lampe);
var HSB2 = new HSBType("37,58,0");

The third possible way would be:

var HSBType = Java.type('org.openhab.core.library.types.HSBType');
var DecimalType = Java.type('org.openhab.core.library.types.DecimalType');
var PercentType = Java.type('org.openhab.core.library.types.PercentType');
var HSB3 = new HSBType(new DecimalType(37), new PercentType(58), new PercentType(0));
1 Like

Well, these are the moments you feel you’ve been in the Python world for quite some time… :sweat_smile:

Many Thanks for the quick reply - which actually solved my issues!