[SOLVED] HSBType fails in Script

What’s your definition of your item? The following should work:

In items:

Color    LT_LivingRM2   "Light Color"    <hue>    {hue="1"}

In rules:

rule "Test"
when
    Item TestSW received command ON
then
    val HSBType red     = new HSBType(new DecimalType(000),new PercentType(100),new PercentType(100))
    val HSBType green   = new HSBType(new DecimalType(120),new PercentType(100),new PercentType(100))
    val HSBType blue    = new HSBType(new DecimalType(240),new PercentType(100),new PercentType(100))
    LT_LivingRM2.sendCommand(red)
    Thread::sleep(2000)
    LT_LivingRM2.sendCommand(green)
    Thread::sleep(2000)
    LT_LivingRM2.sendCommand(blue)
    TestSW.sendCommand(OFF)
end

@danielwalters86 - yes, the script is what appears in the first post in this thread.

@Benjy - I currently have that in a rule and it works fine. I want to move the code to a script so that I can call it with one line from various rules and reuse the code.

The code above appears in 8 different rules, multiply that times the 15-20 lights in the house and there’s a lot of duplication in my rules that I could strip out and make more manageable.

Thanks

Fair enough, I can’t currently test this until tommorow but since scripts can’t have the import statement, you might need to fully qualify the variable type.

Replace HSBType with org.openhab.core.library.types.HSBType etc, if that doesn’t work try using “[Item].toString”

Interesting thought, didn’t think about trying it that way.

Ok, I’ve change the HSBType line to this:

var light = new org.openhab.core.library.types.HSBType(hue,sat,bright)

And now I get this error:

2016-02-23 15:49:25.725 [ERROR] [o.o.c.s.ScriptExecutionThread ] - Error during the execution of rule ‘Test’: An error occured during the script execution: Could not invoke constructor: org.openhab.core.library.types.HSBType.HSBType(org.openhab.core.library.types.DecimalType,org.openhab.core.library.types.PercentType,org.openhab.core.library.types.PercentType)

Thoughts?

Not sure what you mean here - “[Item].toString”

Thanks

.HSBType.HSBType shouldn’t happen, that’s definately not how you’ve written it?

HSBTypes after they’ve been declared can be converted to a string. A colour item can accept this string as a command. I.e. SendCommand(LT_LivingRM2,light.toString)

If I change the declaration to the following the constructor error goes away.

var light = new org.openhab.core.library.HSBType(hue,sat,bright)

And this error comes back.

2016-02-23 21:47:42.031 [ERROR] [o.o.c.s.ScriptExecutionThread ] - Error during the execution of rule ‘Test’: An error occured during the script execution: null

And changing to the following command I get the same null error as above.

sendCommand(LT_LivingRM2,light.toString)

Thanks for the help; I have a solution I can work with!

Didn’t understand that I could just send the command as a string, so no need to declare the variables and pack them in HSBType.

Rather than use the following to set an individual light:

var hue = new DecimalType(360)
var sat = new PercentType(100)
var bright = new PercentType(100)
var light = new HSBType(hue,sat,bright)
sendCommand(LT_LivingRM2,light)

I can simply do this:

sendCommand(LT_LivingRM2,“360,100,100”)

And that simple command will work from a rule or a script.

Thanks again!

Hi Scot,

Glad you have a working solution, but I was able to test the following script using HSBType with no problems, the light turned red instantly:

var hue = new org.openhab.core.library.types.DecimalType(360)
var sat = new org.openhab.core.library.types.PercentType(100)
var bri = new org.openhab.core.library.types.PercentType(100)

var light = new org.openhab.core.library.types.HSBType(hue,sat,bri)

sendCommand(LT_LivingRM2,light)

I didn’t fully qualify hue, sat and bright the way you did, only thought to do it with HSBType.

I’m sure this learning experience will come in handy later.

Thanks for the follow up Ben.

No problem @p912s , since you’re on the topic of getting a rule/script working for many lights, the item part of sendCommand([item],[command]) can also be a string. This means that you can construct an item name to use dynamically. e.g. if you have a group of switches which are designed to control different lights:

Items:

Group:Switch:OR(ON, OFF)     EventSwitch
Switch                       BR             "Bedroom Event Switch"     (EventSwitch)
[...]
Switch                       KR             "Kitchen Event Switch"     (EventSwitch)

Color                        BR_Colour      "Bedroom Light Colour"     <hue> 
[...]
Color                        KR_Colour      "Kitchen Light Colour"     <hue>

In a rule:

rule "TestRule"
when
    Item EventSwitch changed
then
    val light = new HSBType(new DecimalType(000),new PercentType(100),new PercentType(100))
    EventSwitch.members.filter(s | s.state==ON).forEach(d | sendCommand(d.name + "_Colour",light.toString))
end

I use the technique above to control lights in different rooms with the relevant switches without ever having to change the room. If I were to get lights in the bathroom for example, I’d just have to define the items, and the rule automatically applies.

@Benjy I have a similiar problem, I have defined all my lights (RGB lights, hence would like to use RGB values instead of hue,bright,sat) as items where they belong to several groups :
Color LB6 "LB6"(Group_DMX,Group_DMX_LivingRoom,Group_DMX_Bar) {dmx="CHANNEL[82,83,84:1000]"}

I then have in my sitemap a color wheel for each room,
Color lampColorLivingRoom "The Lamp Color"

I then have a scene mapping :
Switch item=Scene_LivingRoom1 label="Group 1" mappings=[1="TV", 2="DINNER", 3="BAR",4="OFF"]

But now I would like get the scene when i press BAR button to loop through all my lights in DMX_LivingRoom check if they are off(“0,0,0”) if not set them to off if they do not belong to DMX_Bar, if they do so set them to the color in lampColorLivingRoom

Here are my rules that does not quite work…

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////// Switch the different areas where LED-Lights should be switched on/off ////////////////7////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////


rule "changeLightGroup"
when
	Item Scene_LivingRoom2 received command
then
	//logInfo("Debug", "hsbValue equals change group " + hsbValue.toString)
	
	//Group_Light.members.forEach [s | logInfo("Debug", s.name + " equals " + s.state.toString)]
	//Group_Light.members.forEach[s | s.sendCommand(OFF)]
	/*
	if (receivedCommand==1) {
			Group_Tv.members.forEach [ item | 
				sendCommand(item,hsbValue)
			]
	}

	if (receivedCommand==2) {
		Group_Dinner.members.forEach [ item | 
			item.sendCommand(hsbValue)
		]
	}
*/
	//Switch on just bar
	if (receivedCommand==3) {
		Group_DMX_LivingRoom.members.filter( s | s.state != "0,0,0").forEach [ item | 
			item.sendCommand(HSBType::fromRGB(0, 0, 0))
		]
		Group_DMX_Bar.members.forEach [ item | 
			item.sendCommand(hsbValue)
		]	
	}

	//Switch OFF
	if (receivedCommand==4) {
		Group_DMX_LivingRoom.members.filter( s | s.state != "0,0,0").forEach [ item | 
			item.sendCommand(HSBType::fromRGB(0, 0, 0))
		]
	}
	
	
end

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////// Change the moodings of light, e.g. warm white, white, lounge and night ////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
rule "changeLightMood"
when
	Item MoodSelector_LivingRoom received command
then
	
	//warm white
	if (receivedCommand==1) {
		hsbValue   = HSBType::fromRGB(255, 0, 0)
		lampColorLivingRoom.sendCommand(hsbValue)		
	}
    //white
	if (receivedCommand==2) {
		hsbValue   = HSBType::fromRGB(0, 255, 0)
		lampColorLivingRoom.sendCommand(hsbValue)
	}
	//lounge
	if (receivedCommand==3) {
		hsbValue   = HSBType::fromRGB(0, 0, 255)
		lampColorLivingRoom.sendCommand(hsbValue)	
	}
	//night
	if (receivedCommand==4) {
		hsbValue   = HSBType::fromRGB(255, 255, 255)
		lampColorLivingRoom.sendCommand(hsbValue)
	}
	
end

Can you be sure that they are precisely “0,0,0” when off? Many light states will reduce it’s brightness to zero but keep the hue and saturation the same.

What does the line

Group_Light.members.forEach [s | logInfo("Debug", s.name + " equals " + s.state.toString)]

output?

17:42:17.949 [INFO ] [eclipse.smarthome.model.script.Debug] - LL1 equals 240.0,100.0,100.0
17:42:18.046 [INFO ] [eclipse.smarthome.model.script.Debug] - LL2 equals 240.0,100.0,100.0
17:42:18.198 [INFO ] [eclipse.smarthome.model.script.Debug] - LL3 equals 240.0,100.0,100.0
17:42:18.329 [INFO ] [eclipse.smarthome.model.script.Debug] - LL4 equals 240.0,100.0,100.0
17:42:18.469 [INFO ] [eclipse.smarthome.model.script.Debug] - LL5 equals 240.0,100.0,100.0
17:42:18.615 [INFO ] [eclipse.smarthome.model.script.Debug] - LL6 equals 240.0,100.0,100.0
17:42:18.760 [INFO ] [eclipse.smarthome.model.script.Debug] - LL7 equals 240.0,100.0,100.0
17:42:18.964 [INFO ] [eclipse.smarthome.model.script.Debug] - LL8 equals 240.0,100.0,100.0
17:42:19.177 [INFO ] [eclipse.smarthome.model.script.Debug] - LL9 equals 240.0,100.0,100.0
17:42:19.349 [INFO ] [eclipse.smarthome.model.script.Debug] - LL10 equals 240.0,100.0,100.0
17:42:19.573 [INFO ] [eclipse.smarthome.model.script.Debug] - LL11 equals 240.0,100.0,100.0
17:42:19.775 [INFO ] [eclipse.smarthome.model.script.Debug] - LL12 equals 240.0,100.0,100.0
17:42:19.914 [INFO ] [eclipse.smarthome.model.script.Debug] - LL13 equals 240,100,100
17:42:20.017 [INFO ] [eclipse.smarthome.model.script.Debug] - LL14 equals 240.0,100.0,100.0
17:42:20.124 [INFO ] [eclipse.smarthome.model.script.Debug] - LL15 equals 240.0,100.0,100.0
17:42:20.315 [INFO ] [eclipse.smarthome.model.script.Debug] - LL16 equals 240,100,100
17:42:20.411 [INFO ] [eclipse.smarthome.model.script.Debug] - LL17 equals 240.0,100.0,100.0
17:42:20.470 [INFO ] [eclipse.smarthome.model.script.Debug] - LL18 equals 240.0,100.0,100.0
17:42:20.571 [INFO ] [eclipse.smarthome.model.script.Debug] - LL19 equals 240.0,100.0,100.0
17:42:20.658 [INFO ] [eclipse.smarthome.model.script.Debug] - LL20 equals 240.0,100.0,100.0
17:42:20.750 [INFO ] [eclipse.smarthome.model.script.Debug] - LL21 equals 240,100,100
17:42:20.917 [INFO ] [eclipse.smarthome.model.script.Debug] - LL22 equals 240,100,100
17:42:21.014 [INFO ] [eclipse.smarthome.model.script.Debug] - LL23 equals 240.0,100.0,100.0
17:42:21.133 [INFO ] [eclipse.smarthome.model.script.Debug] - LL24 equals 240,100,100
17:42:21.272 [INFO ] [eclipse.smarthome.model.script.Debug] - LL25 equals 240.0,100.0,100.0
17:42:21.412 [INFO ] [eclipse.smarthome.model.script.Debug] - LL26 equals 240,100,100
17:42:21.566 [INFO ] [eclipse.smarthome.model.script.Debug] - LL27 equals 240,100,100
17:42:21.676 [INFO ] [eclipse.smarthome.model.script.Debug] - LL28 equals 240,100,100
17:42:21.774 [INFO ] [eclipse.smarthome.model.script.Debug] - LL29 equals 240.0,100.0,100.0
17:42:21.864 [INFO ] [eclipse.smarthome.model.script.Debug] - LL30 equals 240,100,100
17:42:21.967 [INFO ] [eclipse.smarthome.model.script.Debug] - LB1 equals 240,100,100
17:42:22.085 [INFO ] [eclipse.smarthome.model.script.Debug] - LB2 equals 240.0,100.0,100.0
17:42:22.168 [INFO ] [eclipse.smarthome.model.script.Debug] - LB3 equals 240,100,100
17:42:22.261 [INFO ] [eclipse.smarthome.model.script.Debug] - LB4 equals 240,100,100
17:42:22.373 [INFO ] [eclipse.smarthome.model.script.Debug] - LB5 equals 240,100,100
17:42:22.515 [INFO ] [eclipse.smarthome.model.script.Debug] - LB6 equals 240.0,100.0,100.0
17:42:22.907 [INFO ] [eclipse.smarthome.model.script.Debug] - LMB1 equals 0.0,0.0,0.0

It also comes up with a warning in the designer for the sendcommand(item,hsbvalue)

Ok, there is a huge bug in OH2 that if you remove group from items, it does not refresh, and you need to restart OH, huge thanks to @rlkoshak for pointing this out. Should we add this to the documentation @Kai, @ThomDietrich ?

Also I have problem finding out how to use hsbvalue so that item.sendcommand( HSBType::fromRGB(125, 125, 125)) so that it select the correct sendcommand and that the designer does not display it as an error… It is somehow not described well in the documentation, only slightly touched in the migration tutorial.

For the ambiguous function call, explicetly cast it to one or the other:

item.sendCommand(HSBType::fromRGB(125, 125, 125) as Command)

Similar problems occur when using DecimalType with sendCommand and postUpdate.

1 Like

Sorry to inform that this does not work for HSBVAlue:
Group_DMX_LivingRoom.members.filter[l|l.state.toString != hsbOff.toString].forEach[l| sendCommand(l,hsbOff as Command) ]

or
Group_DMX_LivingRoom.members.filter[l|l.state.toString != hsbOff.toString].forEach[l| l.sendCommand(hsbOff as Command) ]

seems like hsbvalue can not be casted to command:(

Command should be State

http://www.eclipse.org/smarthome/documentation/javadoc/org/eclipse/smarthome/core/library/items/ColorItem.html

Can still not figure out how to get rid off the ambigouse error:

Group_DMX_LivingRoom.members.filter[l|l.state.toString != hsbOff.toString].forEach[l| l.sendCommand(hsbOff as State) ]

or

Group_DMX_LivingRoom.members.filter[l|l.state.toString != hsbOff.toString].forEach[l| l.send(hsbOff as Command) ]

or
Group_DMX_LivingRoom.members.filter[l|l.state.toString != hsbOff.toString].forEach[l| l.postUpdate(hsbOff as HSBType) ]

does fix the problem

When all else fails, use .toString and move on to more interesting problems.

I would have expected pretty much all of those to work. I’m not sure what is going on but when you use toString it will force the code down a different path that should eliminate the error.

rule “SD_Rotgbfb”
when

Item SD_Red_Schalter received command
then

var DecimalType hue = new DecimalType(240) // 0-360; 0=red, 120=green, 240=blue, 360=red(again)
var PercentType sat = new PercentType(100) // 0-100
var PercentType bright = new PercentType(100) // 0-100
var light = new HSBType(hue,sat,bright)

SD_Red.sendCommand(light)

end

Ich habe das Problem, sendCommand ist Unterstrichen und der Compiler verweigert den Befehl

Item:
Color SD_Red “Rot” { channel=“hue:0210:1:bulb2:color” }
Switch SD_Red_Schalter
SSitemap: Switch item=SD_Red_Schalter mappings=[ON=“Click”]

Ich benutze Visual Studio Code mit Addon openhabian

ich bitte um Hilfe

danke