Type cast item number state to a variable

Hi
I’m trying to put the state of an item of type number into a variable and then display it with logINFO. I keep getting error messages in the log file.
The code is

rule Circulation_Pump
when
Member of CirculationPump received update
then
{var PumpIndicator = 1.0
logInfo(“INFO”, “circulation”)
PumpIndicator = Kitchen_Thermostat_Fan_State.state
logInfo(“INFO”, PumpIndicator)}
end

the error message is

2019-06-05 11:24:01.037 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule ‘Circulation_Pump’: An error occurred during the script execution: Could not invoke method: org.eclipse.smarthome.model.script.actions.LogAction.logInfo(java.lang.String,java.lang.String,java.lang.Object[]) on instance: null

I’ve also tried with var PumpIndicator = 1

Kitchen_Thermostat_Fan_State is of type number with values from 0 to 3

Any idea where I’m going wrong
Thanks
Tim

At the end logInfo only accepty String. Try PumpIndicator.toString. And your curley brackts are not needed and you should add a logIfo stat shows the value of Kitchen_Thermostat_Fan_State

Thanks for the advice. I changed the code to

rule Circulation_Pump

when
Member of CirculationPump received update
then
var PumpIndicator = 1
logInfo(“INFO”, “circulation”)
PumpIndicator = Kitchen_Thermostat_Fan_State.state
logInfo(“INFO”, PumpIndicator.toString)
end

and now get error message

2019-06-05 14:33:20.651 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule ‘Circulation_Pump’: An error occurred during the script execution: Could not invoke method: java.lang.Integer.toString() on instance: 0

Let’s find out what your Item state is.

then
   logInfo("INFO", "item state " + Kitchen_Thermostat_Fan_State.state.toString)
   var PumpIndicator = Kitchen_Thermostat_Fan_State.state as Number
   logInfo("INFO", "variable " + PumpIndicator.toString)

Thanks. That seems to have fixed it. Playing around a bit if I declare

var PumpIndicator = 1

then I get the error. If I declare the variable as you have done then it works fine. I even tried declaring it as you did and then adding 1

var PumpIndicator = Kitchen_Thermostat_Fan_State.state as Number + 1

and that gives the correct result. It seems that java is not happy with the type “Number” used in OpenHab for some reason. I wonder what the underlying java type of Number actually is ?

For info : the reason I am doing this is to turn the circulation pump for my central heating on and off. I have several thermostats and if any of them is on then I need the circulation pump to be on. The group CirculationPump contains all the thermostats. So if one changes state I’m planning on adding up all the thermostat states and if the answer is >0 then make sure the pump is on. If it is 0 then turn the pump off.

You don’t need a Rule for the calculation.

Group:Number:Sum CirculationPump

With a definition like this, CirculationPump.state will be the number of it’s members that are ON.

Thanks Rich.
I’ll play around with this. I think I can see an easy way to do it like this with these thermostats

Tim

Java is quite happy with Number types.

var x = 1
does not I think create a Number but probably a primitive integer (no toString method, etc.

var Number x = 1
should do the trick.

MyItem.state gives a state object, which isn’t exactly a Number type either. It could be the state of a switch or dimmer Item, after all.

MyItem.state as Number
does what you expect.

Thanks. That makes sense. I didn’t think of explicitly declaring it as a number type :frowning: