Defining constants in rules

I am wondering how I can define constant in rules/lambdas, instead of having to redeclare them every time i change the lights?

var HSBType hsbValue
var HSBType hsbValueMasterBedRoom
var HSBType hsbValueBathRoom

val hsbOff = HSBType::fromRGB(0, 0, 0) 
val hsbOn = HSBType::fromRGB(255, 255, 255)
var boolean partyMode
	
var int redColor
var int greenColor
var int blueColor
var DecimalType dimmer_MasterBedRoom_Value

var int redValue
var int greenValue
var int blueValue
var String RGBvalues


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

 val Functions.Function3 rgbLights= [ GroupItem mainGroup,GroupItem subGroup, HSBType hsbValueIn |
    val hsbOff = HSBType::fromRGB(0, 0, 0)
    
	mainGroup.members.filter[l|l.state.toString != hsbOff.toString].forEach[l| l.sendCommand(hsbOff.toString()) ]
	subGroup.members.forEach[l2|l2.sendCommand(hsbValueIn)]	
]

however if I comment out val hsbOff = HSBType::fromRGB(0, 0, 0) I get this error:

2017-09-19 21:02:27.227 [ERROR] [.script.engine.ScriptExecutionThread] - Rule 'Activate Scene LivingRoom': cannot invoke method public java.lang.String org.eclipse.smarthome.core.library.types.HSBType.toString() on null

You have to pass everything that a lambda uses into that lambda.

If you need to use hsbOFF in your lambda, you need to declare and define it in your lambda or you need to pass it as an argument.

2 Likes

I guess its not to cpu intensive to redeclare it in the lambda function on every call. So I just keep doing that, its more clear than pass it into the lambda for the end user I think.

If the Rules Engine is smart about things, which it might be, it will only declare it once. I’ve seen some evidence that the Rules Engine does some caching and re-use of lambda objects like this so I wouldn’t be surprised.

But honestly, you should ALWAYS code for the person. Never worry about how it impacts the CPU or memory unless you KNOW it is a problem. Your time is far more valuable than the computer’s.

1 Like