Split ColorItem to compare with Number

Hi, i have a color item and need to compare its first number with a specific number.

I am able to split this number, but since i need to transform it to a String, i cannot compare it with a number.

Is there a way to do this correctly?

    if (colorItem.state.toString.split(",").get(0) < 276 && colorItem.state.toString.split(",").get(0) > 273)

colorItems value: “274,0,100”

As you see, i need to transform it with .toString into a String item. Then i can split it by “,” and get the first number, which varies between 274 and 275. So far it works good. But when i try to compare if the value is lower than 276 AND higher than 273, it gives me some errors.

I tried a lot with .intValue or “as DecimalType” but this is not working, since the .intValue and DecimalType are not part of String.

Wow, never had a fix this fast.

    if (colorItem.state.toString.split(",").get(0) < "276" && colorItem.state.toString.split(",").get(0) > "273")

did the trick. I just added quotation marks to the comparing numbers.

That’s not going to work in all cases. That’s doing a String comparison meaning it’s comparing in alphabetical order. Thus "5" > "1234" is true because “5” comes later in alphabetical order then “1” does.

But a ColorItem carries its state as an HSBType (see HSBType (openHAB Core 3.4.0-SNAPSHOT API)). HSBType had methods to get the each of the three values independently. In you case you want the hue. You’ll see that it returns a DecimalType so you can use it just like you’d use the state of a NumberItem in a rule by casting it to Number.

    val hue = (colorItem.state as HSBType).getHue() as Number
    if(hue < 276 && hue > 273)

I don’t know for sure if you need the cast to HSBType or not. I’ve included it to be thorough.

Thank you. This works perfect. I tried without HSBType and it throws some errors. Here is my final rule:

    if (((colorItem.state as HSBType).getHue() as Number) < 276 && ((colorItem.state as HSBType).getHue() as Number) > 273)

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