Oh4 - HSBType is deprecated warning

to transform color values to rgb I used the following rule for years without problems:

rule “clockColor”
when
Item clockColor changed
then
clockColorSet.sendCommand(((clockColor.state as HSBType).red.intValue * (clockColor.state as HSBType).brightness.intValue / 100).toString + “,” + ((clockColor.state as HSBType).green.intValue * (clockColor.state as HSBType).brightness.intValue / 100).toString + “,” + ((clockColor.state as HSBType).blue.intValue * (clockColor.state as HSBType).brightness.intValue / 100).toString)
end

After upgrading to oh4 logging claims HSBType to be deprecated:

The method getRed() from the type HSBType is deprecated

Following the official docs (Rules | openHAB) that still seems to be the recommended way.
In another sort of documentation (Deprecated List (openHAB Core 4.1.0-SNAPSHOT API)), that I unfortunately do not understand :-), it is advised to use ColorUtil.hsbToRgb(HSBType) instead.
Could anyone help me with the syntax of the new method or even bring the official docs up to date?

Just like you have it.

val rgb = ColorUtil.hsbToRgb(clockColor.state as HSBType) // returns an array of int values
val red = rgb.get(0)
val green = rgb.get(1)
val blue = rgb.get(2)

thanks for your quick reply. I think I missed something - even for the first line of code the logs stated:

The name ‘ColorUtil’ cannot be resolved to an item or type

adding

import org.openhab.core.util.ColorUtil

did not help

Logs? Error messages tend to tell you what’s wrong and how to fix it.

openhab.log:

2023-07-27 17:42:40.570 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID ‘clock’ failed: The name ‘ColorUtil’ cannot be resolved to an item or type; line 5, column 12, length 9 in clock

Please use code fences.

```
code goes here
```

This is the only rule in the file named clock.rules? The UID in the error doesn’t check out because if this is in a .rules file named “clock.rules” the UID should be clock_1 or clock-1 or something like that, meaning the first rule in the file. But if this is a UI rule, the entire syntax in the first post is wrong.

I only use text files for rules. But I checked it - in the administration gui it is called “clock-1” (containing the code from the text configuration).
I do believe it should be a problem with ColorUtil. Do I need to import something or is it part of the core?
just to be sure:

rule "clockColor"
when
	Item clockColor changed
then
	val rgb = ColorUtil.hsbToRgb(clockColor.state as HSBType)
	val red = rgb.get(0)
	logInfo("red", red.toString)
end

even if I delete the second and third row after “then”, the log shows the problem

The error is saying it’s not imported by default, which is a pain. So definitely try it with an import, though I think you said you already tried that. When you try that is the error the same?

yes - exactly the same.
ps: sorry - in the original log it actually said clock-1. my mistake during translating from german…
and - no other rule with a similar name

If it didn’t exist, there should be an error on the import I would think.

I don’t use Rules DSL and I don’t have any Color Items so I’m out of ideas.

thanks anyway
it still works the “old” way. I just wanted to be prepared for the future.
perhaps someone who knows a solution can update the docs…

I am running into the same issue. It doesn’t seem that the org.openhab.core.util namespace can be imported with the DSL rule engine. I have opened the below issue.

1 Like

Based on my testing, import errors don’t seem to generate any log event. I just tried to import some bogus package and the server logs didn’t show anything.

That’s probably worth filing an issue. I’m not sure that’s something that OH can control though as the import logic is implemented by an upstream library.

A fix has been added and as of snapshot openHAB 4.1.0 (Build #3615), method hsbToRgb is available in DSL rules. No need for any extra import.

rule "ColorItem Changes"
when
  Item ColorItem changed
then
  val rgb = hsbToRgb(ColorItem.state as HSBType)
  logInfo("ColorItem", "RGB: {},{},{}", rgb.get(0), rgb.get(1), rgb.get(2))
end
1 Like

I would like to create an HSBType from the three rgb-values.

at this time, i use the
HSBType.fromRGB method

what is the not deprecated version of this command in OpenHAB 4 ?