Problems in rules file Could not cast 10.0 to java.lang.Number

Hi all I have a couple of temperature itmes for my pool, one for current temperature and on for set temperature, in a rule to start circulationpump before heater is starting I will use a condition actual temperature is equal to or less than settemperature +2.0. However all my efforts ends up in
Could not cast 10.0 to java.lang.Number where 10.0 is the settemperature or a similar measseage about current temperature. I guess that it might be the UoM %.1f tha makes this difficult but I’m not sure. I hope someone could point me in the right direkction, I’v tried to use DecimalType and .floatValue or.doubleValue after the parenthesis.

Items

Number Pool_TEMP "Current Temperature   [%.1f °C]" (gPool_,gPool_Parse_json)
Number Pool_SET_TEMP "Target Temperature   [%.1f °C]" (gPool_,gPool_Parse_json)

Rule the part that being problematic

if((Pool_TEMP.state as Number) <= (Pool_SET_TEMP.state as Number +1) && Pool_cirkpump_shelly_output.state!=ON){
  Pool_cirkpump_shelly_output.sendCommand(ON)
}

/Marcus

Hi,

please adapt and give it a try:

Number:Temperature              Weather_CurrentHour_Temp            "current temperature [%.1f %unit%]"         <temperature>    (WeatherTodayCurrentHour)
if(Weather_CurrentHour_Temp.state < 7 | °C && Weather_Today_TempHigh.state < 10 | °C)

or e.g.:

    val oldval = WindowsensorVoltage.previousState().state as Number
    val newval = WindowsensorVoltage.state as Number

    if(oldval + 150 | mV < newval)
    { }

HI I made some adjustments and it seems to work until i perform the math operations. If I calculate val TemDiff as below it works, but i I would like to add lets say 2.0 to current temperature 31 it ends up as 312.15. In trhe log output one can see te current temperature printed to logg measagge and then add 2.0 and it ends up as 312.15

Is there any ideas for that?

val TempDiff = ((Pool_TEMP.state as Number) - (Pool_SET_TEMP.state as Number))

New items

Number:Temperature Pool_TEMP "Current Temperature   [%.1f %unit%]" (gPool_,gPool_Parse_json)
Number:Temperature Pool_SET_TEMP "Target Temperature   [%.1f %unit%]" (gPool_,gPool_Parse_json)

Rules

if(TempDiff < 5.0 && Pool_cirkpump_shelly_output.state!=ON){
  Pool_cirkpump_shelly_output.sendCommand(ON)
}

Log output

16:34:42.920 [INFO ] [.openhab.core.model.script.Teståäöåäö] - Temp: 37 °C Temp + 2.0: 312.15

Please post the actual error. Descriptions of the error are not good enough. It’s like taking your car to the mechanic and saying “it makes a noise” without even telling them from where the noise comes from.

That looks suspicious.What is the name of this .rules file?

Please show the full rule so we can see this log statement in context.

Hi,
as Rich mentioned, please post your full rule…

| °C

is important to use.

val currval = Pool_TEMP.state as Number
if(currval + 2 | °C < setval )

Hi

heres the full rule

  val TempDiff = ((Pool_TEMP.state as Number) - (Pool_SET_TEMP.state as Number))
  val test = ((Pool_TEMP.state as Number) +2.0) 
  val test2 = (Pool_TEMP.state as Number)
  logInfo("Teståäöåäö", "SetTemp - Verklig temperatur: " +TempDiff +" test: "+test)
  logInfo("Teståäöåäö", "Temp: " +test2 +" Temp + 2.0: "+test)
  if(Pool_mode.state =="Automated") {
  if(TempDiff < 5.0 && Pool_cirkpump_shelly_output.state!=ON){
  Pool_cirkpump_shelly_output.sendCommand(ON)
}
}

And log output ends up beeing 312.15 for calculated temperture. Maybe I need to implement the separator in the math so that the unit are excluded? The tempdiff math works and allways shows a valid difference so after a workaround my rule work but I would like to understand how to add or subtract from Number:Temperature item.

Hi

Thats ju my fake name of the logger during testing, rules filename are Pool.rules

Better use a meaningful logger name.
Please be aware that the logger provides a lot more functionality than two simple strings. The logger name can be used to activate/deactivate specific parts of the logging, i.e. if using karaf console:

log:set WARN org.openhab.core.model.script.Teståäöåäö

the info logging is switched off for this specific logger until you set the log level to INFO or DEBUG.

As you are using QuantityType Items Number:Temperature you either have to strip the unit or provide a matching QuantityType value when doing comparison.
The temperature is a bit tricky, as it’s calculated in Kelvin (afaik). So 37 °C - 32 °C is NOT 5 °C, but 310,15 K - 305,15 K = 5 K = -268,15 °C. :wink:
The easiest way to strip the unit is to convert the QuantityType to float:

val TempDiff = ((Pool_TEMP.state as Number).floatValue - (Pool_SET_TEMP.state as Number).floatValue
1 Like

Hi
This seems to work, a question to all of you that know more than me, could one use | °C in mathematic operations or ist that just for evaluating numbers out of a UoM number?

Yes, you must use the the unit in all match operations as well.

Nice appearence but a little harder to code and get around my head at least, now this beatifully community solved one more issue for me.

Thanks alot