Converting DSL Rule to JS (ECMAScript 2022+)

Hej there,

I am trying to convert a working DSL rule into JS (ECMAScript 2022+). The DSL rule works fine and look quite “elegant” meaning it uses built-in functions like “.getBrightness()”.

I managed to get the brightness value from my lamp, but it seems a little bit too complicated to me and I cannot find something like “.getBrighness()” for JS.

Assume the Color value in HSB coming from the item: 24.5,50.8,100. I need the “100”
Example DSL:

var lamp = WiZ_Lamp_Color
var lampColorState = lamp.state as HSBType
var brightness = lampColorState.getBrightness()

My version in JS:

var lamp = WiZ_Lamp_Color
var lampColorState = lamp.state
var brightness = lampColorState.toString().split(',')[2]

The conversion to string plus the splitting etc seems not quite elegant.
How could I use something like .getBrightness()? Is there something like this? Where can I find docs about those things?

So far I read the getting started but I am not quite secure in how to do a rule. Looking in the forum sometime does not help me here as DSL and JS look quite the same from my level of experience (so far)…

I don’t know HUE and how to use it, but the JS Scripting also has those built-in methods:

But another thing: Why do you want to move from DSL Rule to JS Scripting? DSL is not deprecated and probably never will. you can still run DSL in parallel to JS Scripting rules…?

The following should work:

var brightness = items.WiZ_Lamp_Color.rawState.getBrightness().floatValue();

Wrt to docs, this is not well documented, but in JS in general you can do anything with the Item state that you can to in DSL by accessing the raw Java state through the rawState property. Keep in mind that in that case you get Java types returned, these need to be handled and often are not compatibly with JS types without some conversions (note that I get the float value above from the brightness, which is a PercentType).

3 Likes