[SOLVED] HSBType fails in Script

Hello all,

I’ve been trying to move common commands for my hue lights to scripts to simply rules and be able to reuse the code. But I can’t get the HSBType to work in a script.

Anybody have any ideas on how to make this work?

// following lines ok - script continues
var DecimalType hue = 360
var PercentType sat = 100
var PercentType bright = 100

// if the following line isn’t commented out - the script stops here
// no errors - but the light changes below don’t get executed
//var HSBType light = (hue,sat,bright)

// this line commented out because the light line above doesn’t work
//sendCommand(LT_LivingRM2,light)

// the following work if HSBType above is commented out
sendCommand(LT_LivingRM2,10)
Thread::sleep(2000)
sendCommand(LT_LivingRM2,100)
Thread::sleep(2000)
sendCommand(LT_LivingRM2,10)

I had started a previous question on this before narrowing the problem to HSBType here.

Thanks!

Could you try changing your HSBType line back to:

var HSBType light = new HSBType(hue,sat,bright)

If that doesn’t work could you post the error log as well please.

Thanks for the reply.

Changed the line back as requested and here are the results:

2016-02-23 09:33:54.901 [INFO ] [c.internal.ModelRepositoryImpl] - Refreshing model 'test.script’
2016-02-23 09:34:09.888 [INFO ] [runtime.busevents ] - TestSW received command ON
2016-02-23 09:34:09.907 [ERROR] [o.o.c.s.ScriptExecutionThread ] - Error during the execution of rule ‘Test’: An error occured during the script execution: null

Could you also paste your entire ‘Test’ rule please? I can’t tell what would cause that from the above.

Here’s the rule for the switch:

rule "Test"
when
Item TestSW received update
then
if (TestSW.state == ON) {
callScript(“test”)
//turn switch off
sendCommand(TestSW, OFF)
}
end

Just to confirm, you have a file called test.script in ${openhab.home}/configurations/scripts?

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.