Unable to do simple math in my rule

Hello everyone,

I’m trying to set the color for a colorpicker using a value received from MQTT.

The value I received is from a parsed xml output:

String WLED_PGarageColorRed (WLED_Settings) { channel="mqtt:topic:5601fda0:WLED_PGarage_API"[profile="transform:XPATH", function="/vs/cl[1]/text()"]}

Receiving the data work perfectly.

Now, I want to get this value and update my colorpicker. Thus, I created this rule:

rule "WLED MQTT Red Received"
when
	Item WLED_PGarageColorRed changed
then
	var newColor = "false"
	var HSBType hsbValue = WLED_PGarageColor.state as HSBType
	var int redValue = (Math::round(hsbValue.red.intValue * 2.55)).intValue
	var int greenValue = (Math::round(hsbValue.green.intValue * 2.55)).intValue
	var int blueValue = (Math::round(hsbValue.blue.intValue * 2.55)).intValue
	
	if (WLED_PGarageColorRed.state != redValue.toString){
		var double redReceived = WLED_PGarageColorRed.state
		var redReceivedInt = (redReceived / 2.55)
		newColor = "true"
		logInfo("WLED","Red2: " + redReceived)
		
	}
	
	if (WLED_PGarageColorGreen.state != greenValue.toString){
		newColor = "true"
	}
	
	if (WLED_PGarageColorBlue.state != blueValue.toString){
		newColor = "true"
	}
	
	val rgbCol = "" + redValue + "" + greenValue + "" + blueValue
	
	if (newColor == "true" )  
	{
	logInfo("WLED", "Color changed from API")
		//WLED_PGarageColor.postUpdate(rgbCol)
	}
end

Now, when I run the job to update the value, I get the error
An error occurred during the script execution: Could not invoke method: org.eclipse.xtext.xbase.lib.DoubleExtensions.operator_divide(double,double) on instance: null

But if I log the value of redReceived , there is a value so why it’s saying there’s an null?

Thank you

edit
For more info, I just tried doing
var int redReceived = (Integer::parseInt(WLED_PGarageColorRed.state as Double))
This output me
Rule 'WLED MQTT Red Received': Could not cast 76 to java.lang.Double; line 51, column 44, length 36

I tried as Decimal, didn’t solved. I tried just parsing, says cannot convert null …
Rule 'WLED MQTT Red Received': An error occurred during the script execution: Could not invoke method: java.lang.Integer.parseInt(java.lang.String) on instance: null

This doesn’t answer your specific question, but I have also integrated WLED via MQTT:

Colorpicker works without rules, though uses a couple of JS transformations!

I cannot use the colorpicker as his. The color picker send RGB data while WLED MQTT use #HEX value. Thus, a way to simplify this is to use API call instead. This then send me an xml as an answer that I need to parse, and then I need to put this data into the colorpicker. And that’s what I’m trying to do.

Not sure I understand - did you read the link?

I use JS transformations to convert between RGB and HEX, so the colorpicker works.

yeah, I read that. I was more trying to understand what error I have right now, why I cannot simply cast a string to int/double or anything, why it says it’s null when it’s clearly not…

The wled binding is now merged and built into latest openhab, much easier to use.

Yeah I tried it and it’s really a mess, and for me unreliable. I send command with it like on/off and it’s not always toggling. Using my own mqtt bindind and api call solved my problems. Just too bad simple string to int doesn’t work and I don’t get why I’m getting the error message I posted.

If you are interested in helping the situation you could make a more detailed post of what did not work with the binding and how long ago you last tried. I would suspect you have a wifi connection issue if it was unreliable and that would be why you get better results with mqtt which is designed for unstable connections. To diagnose enter this URL using the ip of wled and look for the RSSI signal strength.

http://192.168.1.xx/json

How about logging out the string that you started with?

I did. Just before it throws the error, did a loginfo and it output a number, either as a number or string. Then casting it to anything doesn’t work.