How to use a variable item to set another item

In a rule I want to set the temperature of a radiator control with the help of a variable. The variable is a Number:Temperature item, which is defined like:

Number:Temperature wantedTemperature "Wanted Temperature" (EqpVar_Radiator) ["Point"]

I can set this variable in the UI , print it and compare it with the value of a bounded item (named
setPointTemperature to control the radiator) like in the following example action part of a rule:

val mailActions = getActions("mail", "mail:smtp:openhab")
if (setPointTemperature.state < wantedTemperature.state)
{
   mailActions.sendMail("mail@example.com",
                        "Temperature setting lower than wanted",
                        "setPointTemperature = " + setPointTemperature.state + "\n" +
                        "wantedTemperature = " +  wantedTemperature.state + "\n")
}

But what I can’t do is to set the bounded item to the value of the variable item. If I try this by putting the following line in the rule:

setPointTemperature.sendCommand(wantedTemperature.state)

I’ll get an error message like the following in the log file:

2021-01-23 13:01:41.013 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'radiator-control-1' failed: An error occurred during the script execution: Could not invoke method: org.openhab.core.model.script.actions.BusEvent.sendCommand(org.openhab.core.items.Item,java.lang.String) on instance: null in radiator-control

The following command without using a variable works as aspected:

setPointTemperature.sendCommand(20)

Based on the error message, I assume there is a casting problem here. I have already tried in vain to cast the variable with one of the following commands:

val settingValue = wantedTemperature.state as Number
val settingValue = wantedTemperature.state as DecimalType
val settingValue = wantedTemperature.state as String

Therefore my questions:

  • How can I use the variable item here?
  • Does the class Number have a property of type DecimalType or how else can I cast it to a plain number?

Thanks for your help.

What is the definition of setPointTemperature?


Does

val settingValue = wantedTemperature.state as Number
setPointTemperature.sendCommand(settingValue)

or

val Number settingValue = wantedTemperature.state as Number
setPointTemperature.sendCommand(settingValue)

work?

1 Like

The definition is:

Number:Temperature setPointTemperature "Temperature [%.1f °C]" <Heating> (Loc_BoilerRoom) ["Control", "Temperature"] { channel="zwave:device:edcd9b57:node13:thermostat_setpoint_heating" }

That works. My mistake was using settingValue.state instead of settingValue. This was wrong because settingValue has the type Number and not NumberItem and only NumberItem has the member state.

Thanks for you help.

var setpointTempBedroom = 21.0|“°C”

rule “Heater in Bedroom CO”

when Item BedroomWindowSensor_OpenStatus changed from CLOSED to OPEN

then {

            logInfo("Heater in Bedroom CO", "setpointTempBedroom=" + setpointTempBedroom)

            setpointTempBedroom = BedroomThermostat_SetpointTemperature

            logInfo("Heater in Bedroom CO", "setpointTempBedroom=" + setpointTempBedroom)    

            BedroomThermostat_SetpointTemperature.sendCommand(4.5|°C)   

}

end

rule “Heater in Bedroom OC”

when Item BedroomWindowSensor_OpenStatus changed from OPEN to CLOSED

then

    {       

            logInfo("Heater in Bedroom OC", "setpointTempBedroom=" + setpointTempBedroom) 

            BedroomThermostat_SetpointTemperature.sendCommand(setpointTempBedroom)

            logInfo("Heater in Bedroom OC", "setpointTempBedroom=" + setpointTempBedroom)

    }                       

end

Please tell me correct second rule

BedroomThermostat_SetpointTemperature is Number:Temperature

Hi Viktor,

from my experience I think that you should use the following in your first rule:

setpointTempBedroom = BedroomThermostat_SetpointTemperature.state as Number

instead of:

setpointTempBedroom = BedroomThermostat_SetpointTemperature

With that the second rule should be ok.