Rule error calculating with values

Hello,

I get the following error when a rule is executed:

Error during the execution of rule Washingmachine kWh gesamt: An error occured during the script execution: Could not invoke method: org.eclipse.xtext.xbase.lib.ObjectExtensions.operator_plus(java.lang.Object,java.lang.String) on instance: null

The rule:

rule "Washingmachine kWh gesamt"
when
    Item WallPlug1_SensorPower changed
   //Time cron "0 0/1 * 1/1 * ? *"
then 

val EnergyPlug = WallPlug1_ElectricMeterKWh.state
val EnergyAll = EnergyPlug + 324
val EnergyCostAll = EnergyAll * 0.24

	postUpdate(Washingmachine_Costall, EnergyCostAll)
	postUpdate(Washingmachine_kWhall, EnergyAll)
	
end

It seems to me, that there is an problem with the operator + and *. The strange thing is, that I have the same operators in the same rule file in other rules without any problems. And there I calculate also with values.

Has anybody a hint for me.

Thank you!

Seems like your variable EnergyPlug is NULL. That means, it does not have any value. Try to add

  logInfo( "Ruledebug", "Value of EnergyPlug: " +  EnergyPlug)

And look at the openhab.log to check if it really has a value.

Like this:

rule "Washingmachine kWh gesamt"
when
    Item WallPlug1_SensorPower changed
   //Time cron "0 0/1 * 1/1 * ? *"
then 

val EnergyPlug = WallPlug1_ElectricMeterKWh.state
logInfo( "Ruledebug", "Value of EnergyPlug: " +  EnergyPlug)
end

i’m not an expert in this, but first i wouldn’t use “val” if you want to change theese values, use “var” instead!

second i would try

var Number EnergyPlug = WallPlug1_ElectricMeterKWh.state as DecimalType

Regards

1 Like

What type does your item WallPlug1_ElectricMeterKWh have? I think it should be Number in order to calculate with the state.

That was the solution. Thank you for the quick support.

Have a nice weekend!