Unable to add values in rules [Solved]

I’ve got a couple of problems with manipluating values.

I’m taking an mqtt feed from emoncms, I want to calculate Usage from import and Solar, as convert the voltage by dividing by 10.
Items:

Number PowerUsage "Use now [%d W]" (gMqtt,gPower)
Number PowerImport "Import [%d W]" (gMqtt,gPower) {mqtt="<[mosquitto:emon/emontx1/power1:state:default]"}
Number PowerSolar "Solar [%d W]" (gMqtt,gPower) {mqtt="<[mosquitto:emon/emontx1/power2:state:default]"}
Number PowerMains "Mains [%d V]" (gMqtt,gPower) {mqtt="<[mosquitto:emon/emontx1/power4:state:JS(divide.js)]"}

Rules:

rule "Calculate Usage"
when
    Item PowerImport received update
then
    logInfo("calculateUsage", "Calculate Usage")
    if (PowerUsage.state == Uninitialized) {
    	postUpdate(PowerUsage, 0)
    }
    
	logInfo("calculateUsage", "Import: " + PowerImport.state)
	logInfo("calculateUsage", " Solar: " + PowerSolar.state)
	logInfo("calculateUsage", " Usage: " + PowerUsage.state)
	postUpdate(PowerUsage, PowerImport.state + PowerSolar.state)
	logInfo("calculateUsage", " Usage: " + PowerUsage.state)
end

divide.js

(function(i){ return i/100; })(input)

On updates I get the following errors:

2016-12-09 06:53:04.116 [INFO ] [.o.model.script.calculateUsage] - Calculate Usage
2016-12-09 06:53:04.135 [INFO ] [.o.model.script.calculateUsage] - Import: 687
2016-12-09 06:53:04.141 [INFO ] [.o.model.script.calculateUsage] -  Solar: 0
2016-12-09 06:53:04.147 [INFO ] [.o.model.script.calculateUsage] -  Usage: 0.0
2016-12-09 06:53:04.159 [ERROR] [o.o.c.s.ScriptExecutionThread ] - Error during the execution of rule 'Calculate Usage': Could not invoke method: org.eclipse.xtext.xbase.lib.ObjectExtensions.operator_plus(java.lang.Object,java.lang.String) on instance: null
2016-12-09 06:53:04.234 [WARN ] [o.u.i.items.ItemUIRegistryImpl] - Exception while formatting value '238.58' of item PowerMains with format '%d V': java.util.IllegalFormatConversionException: d != java.math.BigDecimal

Thanks
James

You have to cast the states of PowerImport and PowerSolar to DecimalType before you can add them.

PowerUsage.postUpdate((PowerImport.state as DecimalType) + (PowerSolar.state as DecimalType))

This might produce an ambiguous method call error, in which case add a toString.

PowerUsage.postUpdate(((PowerImport.state as DecimalType) + (PowerSolar.state as DecimalType)).toString)

NOTE: It is best to always use the method call on the Item rather than the sendCommand or postUpdate actions where ever possible.

Brilliant - Thank you!