Cast a int variable to DecimalType

Hallo Community,

I have a problem, that hopefully can be be fixed by some of you really fast. In the following rule I try to convert the variable color inside the sendCommand line to an expected DecimalType, but

rule "changeColor"
	when
			Item LCN_M11_Relay_2 changed from OPEN to CLOSED
	then
			ColorLoop = true 
			var HSBType currentState = Hue3LampColor.state
			
			var int color		= currentState.hue.intValue
			var PercentType sat 	= currentState.getSaturation()	// new PercentType(100) // 0-100
			var PercentType bright	= currentState.getBrightness()	// new PercentType(100) // 0-100
			while (ColorLoop == true) {
				if (ColorDirection == false) {
					color = color + ColorInc 	// ColorInc
					if (color > 360)
						color = 0
				}
				else {
					color = color - ColorInc 	// ColorInc
					if (color < 0)
						color = 360
				}
				logInfo("changeColor", String::format ("color = %d", color))
				Hue3LampColor.sendCommand(new HSBType(new DecimalType(color), new sat, new bright))
				Thread::sleep(100)
			}
	end

I get the following error message:

09:21:10.168 [ERROR] [.script.engine.ScriptExecutionThread] - Rule ā€˜changeColor’: An error occurred during the script execution: null

Please let me know what I have to do to get this f**king code running.
Thanks and best regards
Steffan

Where is the definition of ColorInc, ColorDirection and ColorLoop?

Hello @Stefbert,

are you already using openHAB 2.2? Consider loading the rule in VSCode to get LSP support.

Regarding your issue: ā€œnullā€ in the error message could mean that one of those assignment lines returns a null value instead of a value matching the type. I’d suggest to add more log lines to find out which. That would be the first step to tackle this problem.
Besides that I’d also be interested in what @Udo_Hartmann already said. What is ColorDirection and are you sure it can be compared to false?

Thanks for answering,

the problem was inside the Command.

//WRONG:
Hue3LampColor.sendCommand(new HSBType(new DecimalType(color), new sat, new bright))

//CORRECT
Hue3LampColor.sendCommand(new HSBType(new DecimalType(color), sat, bright))

SO it was just the added new in front of sat and bright. Now everything works fine and I go on with my project.

Thanks and best regards
Steffan

1 Like