HSBType struggles

I’m struggling with my hue binding. The code below is just a test to cycle through the colors, updating every 2 seconds. It causes a Cannot cast org.openhab.core.library.types.HSBType to void Error in the log.

If I remove the as HSBType from currentState = hue_color_4.state as HSBType, then I get an error in the editor saying Type mismatch: cannot convert from State to HSBType and an error in the logs that says the name '.hue cannot be resolved to an item or type.

The code from the last bit of hue binding wiki page https://github.com/openhab/openhab/wiki/Hue-Binding doesn’t work either because it never fires due to this line: if (philipsColor1.state instanceof HSBType)

My non-functional code:

rule "hue test"
	when
		Time cron "0/2 * * * * ? *"
	then
		var HSBType currentState
		currentState = hue_color_4.state as HSBType
		var double new_H = currentState.hue.floatValue() + 6.0
		var double new_S = currentState.saturation.floatValue() + 5.0
		var double new_B = currentState.saturation.floatValue()

		if  (new_H > 360.0) new_H=0.0
		if  (new_S > 100.0)	new_S=100.0
		if	(new_B > 100.0) new_B=100.0 	
		
		val String color = String::format("%.3f",new_H)+","+String::format("%.3f",new_S)+ ","+String::format("%.3f",new_B) 
		hue_color_4.sendCommand(color)
       logInfo("File", "color test: "+ color)
	end

Any help appreciated

  1. What’s the item definition of hue_color_4?
  2. for setting a specific color, you don’t have to use a string but HSBType:
var HSBType currentState
currentState = hue_color_4.state as HSBType
var DecimalType new_H = currentState.hue + 6
var PercentType new_S = currentState.saturation + 5
var PercentType new_B = currentState.brightness
var HSBType newState = new HSBType(new_H,new_S,new_B)
hue_color_4.sendCommand(newState)
logInfo("File", "color test: hue = {} sat = {} bright = {}", new_H, new_S, new_B)

As I don’t use the color item other than UI, I’m not sure if this code does work as expected (I use long values to send via exec binding and therefore use other maths for the vars)

val HSBType color = new HSBType(new_H, new_S, new_B)
hue_color_4.sendCommand(color)
1 Like

Udo,
hue_color_4 is a color item bound to a hue light bulb.
Color hue_color_4 "Livingroom Hue 4" (Hue) {hue="4"}

The problem is that it is choking on this line:
currentState = hue_color_4.state as HSBType

Rich, I believe your code would work on my setup, but my problem is that I’m trying to get the current state, modify the values and then send it back.

I got it to work with the code below, but would love to know why I can’t make the line above work. I’ve had this experience with almost every example on the wiki. It doesn’t work out of the box. It makes me think I’ve got something fundamentally wrong with my installation.

rule "hue test"
	when
		Time cron "0/10 * * * * ? *"
	then
		var String stringState 
		stringState = hue_color_4.state.toString()
		var segments = stringState.split(',').iterator
		var double new_H = Double::parseDouble(segments.next)  + 1.0
		var double new_S = Double::parseDouble(segments.next) + 1.0
		var double new_B = Double::parseDouble(segments.next)

		if  (new_H > 360.0) new_H=0.0
		if  (new_S > 70.0)	new_S=70.0
		if	(new_B > 100.0) new_B=100.0 	
		
		val String color = String::format("%.3f",new_H)+","+String::format("%.3f",new_S)+ ","+String::format("%.3f",new_B) 
		hue_color_4.sendCommand(color)
	end

You take the current values and change the values as you currently are. And replace the val String color = String::format... line with my line val HSBType color = new HSBType(....

The code is equivalent only it uses the HSBType to send the new value instead of the String which is what I figured was the source of the problem.

I [quote=“shogan50, post:1, topic:12143”]
The code from the last bit of hue binding wiki page Hue Binding · openhab/openhab1-addons Wiki · GitHub doesn’t work either because it never fires due to this line: if (philipsColor1.state instanceof HSBType)
[/quote]

This is a major clue actually. If the rule never fires because of that if statement it means that the state of the Item isn’t an HSBType which means you can’t cast it to an HSBType (i.e. hue_color_4.state as HSBType would throw an error).

I thought a Color Item stored its state as an HSBType so this is very odd behavior indeed. Furthermore the toString seems to be printing it out as an HSBType.

What are the import statements at the top of your rule file? Do they include

import org.openhab.core.library.types.HSBType

(only needed if running on openHAB 1.x.)

Copying that precisely results in this error.

org.openhab.core.library.types.HSBType cannot be resolved to
a type.

I’m not sure how to check what version I am running. I downloaded from here http://www.openhab.org/getting-started/downloads.html a few weeks ago and believe it to be 1.8. I’m running on a Pi 3.

On a Raspberry Pi running Raspian, you should install openHAB using the apt-get method described here:

Installing this way tends to eliminate a set of possible oddities. The only reason I suggest it is because the error you receive doesn’t make any sense to me!

I’m also really struggling with this!

I have a Color Item and as I understand this item is of HSBType.
Now I want to convert the HSB to RGB values in the range 0-255 and send that as an hex-string to my LED-strip.

But I fail on first base with this error message in the log:
2017-10-24 15:48:53.213 [INFO ] [penhab.model.script.WIFI_RGB_1] - WIFI_RGB_1 (Type=ColorItem, State=228.676056,83.757550,100.000000)
2017-10-24 15:48:53.342 [ERROR] [o.o.c.s.ScriptExecutionThread ] - Error during the execution of rule ‘Extract RGBW values from HSL setting’: Cannot cast org.openhab.core.library.types.HSBType to void

Here is the Item definition:

Color	WIFI_RGB_1			"LED-strip vard."	<slider>	(gVardag)

and here is the rule: I have not completed the rule yet because it fails immediately after the logInfo statement

rule "Extract RGBW values from HSB setting"
	when
	Item WIFI_RGB_1 received command
		then
		logInfo( "WIFI_RGB_1", WIFI_RGB_1.toString)
		var redValue = (WIFI_RGB_1.state as HSBType).red
		var greenValue = (WIFI_RGB_1.state as HSBType).green
		var blueValue = (WIFI_RGB_1.state as HSBType).blue
		
		logInfo( "redValue" , redValue.toString)
		logInfo( "greenValue" , greenValue.toString)
		logInfo( "blueValue" , blueValue.toString)
end

So according to everything I have read above, I should be able to cast a Color item to HSBType but that does not work. I have tried several other ways described elsewhere in the community but without success. Now I am at the ends of my wits.

Just to make it clear, you cannot cast a Color Item to an HSBType, and in fact, you are not doing that. You can cast the State of a Color Item to an HSBType and that is what you are doing with (WIFI_RGB_1.state as HSBType).

Add some log statements between your lines that assign your red, green, and blue values. There is more than 100 msec between your log statement and the error which seems like a really long time. I can’t tell if the line generating the error is redValue or later on.

Try using .getRed et all instead of just red. It shouldn’t make a difference but something is going on. The code as written looks correct to me.

I made the following changes to the code:

rule "Extract RGBW values from HSB setting"
	when
	Item WIFI_RGB_1 received command
		then
		logInfo( "WIFI_RGB_1", WIFI_RGB_1.toString)
		var redValue = (WIFI_RGB_1.state as HSBType).getRed
		logInfo( "redValue" , redValue.toString)
		var greenValue = (WIFI_RGB_1.state as HSBType).getGreen
		logInfo( "greenValue" , greenValue.toString)
		var blueValue = (WIFI_RGB_1.state as HSBType).getBlue
		logInfo( "blueValue" , blueValue.toString)
end

putting a log statement between each assignment and using .getRed instead of .red
Made no difference to the log output:
2017-10-24 19:49:09.490 [INFO ] [penhab.model.script.WIFI_RGB_1] - WIFI_RGB_1 (Type=ColorItem, State=131.323944,86.022433,100.000000)
2017-10-24 19:49:09.596 [ERROR] [o.o.c.s.ScriptExecutionThread ] - Error during the execution of rule ‘Extract RGBW values from HSL setting’: Cannot cast org.openhab.core.library.types.HSBType to void
BTW I’m running OH 1.8.0 on an older RPi. Can’t tell the model but it is a few years old.

No difference but we now know for sure that the var redValue line is throwing the error.

What version of OH are you running? I’m surprised to see org.openhab.core.library.types. In OH 2 all of those classes moved to org.eclipse.smarthome.core.library.types. Are you using a 1.x version binding for these Color Items?

I’m running OH 1.8.0.
Since the LED-strip will be controlled over MQTT I have not installed any specific binding to handle color items. I will parse the RGB values to an hex string and send that over MQTT. Maybe this is the cause!

Should I install e.g. the Philips Hue binding even though I will not use any device like that?

OK, that is important to know. If there is a bug here then I’m afraid the only solution will be to upgrade to 2.x. You could try at least upgrading to 1.8.3 but if you need to upgrade you may as well go all the way.

You don’t need any extra bindings installed. The vast majority of users are on OH 2 where these classes have moved packages. I was just trying to figure out why the error was referencing the old package for HSBType. If you are on 1.8.0 that explains it.

No, HSBType is a core class. It is always there regardless of your installed bindings.

At this point, I don’t have a lot left to offer. Your code looks right. The error is not terribly helpful. And version 1.8.0 is three versions behind the latest stable release so I can’t really even offer any information as to whether there was an identified bug on this that has since been fixed.

My last recommendations is to explicitly force the types of the values to ints

var int redValue =

If that doesn’t work look to upgrade and see if that fixes it.

Thanks for your comments and for the rapid response!
The problem was driving me crazy but now there is a plausible explanation.

Since I am on the path of upgrading to OH2 anyway I will spend my energy on that, rather than break my head over 1.8.X
Again, thanks!

Did you define

import org.openhab.core.library.types.*

at the very beginning of the rules file?

For OH1 you need this import (or at least import org.openhab.core.library.types.HSBType)
For OH2 this import is obsolete.

My code is almost the same on OH1.8.0:

import org.openhab.core.library.types.*
import org.openhab.model.script.actions.*
import org.openhab.core.types.Command

var HSBType hsbValue

rule "Color Light"
when
        Item ColorLight changed
then
        hsbValue = ColorLight.state as HSBType
        var int red = hsbValue.red.intValue
        var int green = hsbValue.green.intValue
        var int blue = hsbValue.blue.intValue
        logInfo("colorlight","red = {}, green = {}, blue = {}", red, green, blue)
        executeCommandLine("/usr/bin/sendrgb " + red + " " + green + " " + blue)
end
1 Like

Udo, you are a star!

Once I checked my imports and did some other syntax changes, adapting to Xbase formats then the whole thing works! I should never have klicked on the Quick fix Organize imports. The code is below, adapted to an RGBW LED-strip. The W component is calculated as the least common denominator of RGB.

import org.openhab.core.library.types.*
import org.openhab.model.script.actions.*
import org.openhab.core.types.*
import org.openhab.core.library.items.*
import org.eclipse.xtext.xbase.lib.*
import java.util.Map
import java.util.Set

var String redString
var String greenString
var String blueString
var String whiteString
var String RGBvalues
var HSBType hsbValue

rule "Extract RGBW values from HSB setting"
	when
	Item WIFI_RGB_1 received command
		then
		logInfo( "WIFI_RGB_1", WIFI_RGB_1.toString)
		hsbValue = WIFI_RGB_1.state as HSBType
		var int redValue = hsbValue.red.intValue * 255 / 100
		var int greenValue = hsbValue.green.intValue * 255 / 100
		var int blueValue = hsbValue.blue.intValue * 255 / 100
		
		redString = Integer::toHexString(redValue)   //Convert byte to hex string
		if (redValue < 16) redString = "0" + redString //Left pad red string with 0 if single character only
		greenString = Integer::toHexString(greenValue)   //Convert byte to hex string
		if (greenValue < 16) greenString = "0" + greenString //Left pad green string with 0 if single character only
		blueString = Integer::toHexString(blueValue)   //Convert byte to hex string
		if (blueValue < 16) blueString = "0" + blueString //Left pad blue string with 0 if single character only
		var int whiteValue = Math::min(redValue, (Math::min(greenValue, blueValue)))  //White setting is set to least common denominator of colors
		whiteString = Integer::toHexString(whiteValue) //Convert byte to hex string
		if (whiteValue < 16) whiteString = "0" + whiteString  //Left pad white string with 0 if single character only
		
		RGBvalues = redString + greenString + blueString + whiteString //Combine into one message
		logInfo( "WIFI_RGB_1_RGB", RGBvalues.toString)
		sendCommand( WIFI_RGB_1_RGB, RGBvalues)
end