Temperature operations in a DSL Rule

I have tried a number of things, but am stuck. Hoping the answer is easy. I have two zone heating and am trying raise the setpoint 1 degree F in the “main” zone if the “up” zone is running. The rule is;

// Triggers:
// - When UpstairsThermostat21_ThermostatOperatingState changed

// context: Optimize_furnace-1
var Number upstate = UpstairsThermostat21_ThermostatOperatingState.state
var Number <temperature> mainsetpointnow = MainThermostat14_SetpointHeating.state
var Number <temperature> mainsetpointnew = MainThermostat14_SetpointHeating.state
if( upstate == 1 ) {
  logWarn("events", "mainsetpointnow {}", mainsetpointnow)
  mainsetpointnew = mainsetpointnow += 1
  logWarn("events", "mainsetpointnew {}", mainsetpointnew)
//      MainThermostat14_SetpointHeating.postUpdate(mainsetpointnew)
    }
else{
  if( upstate == 0 ) {
  logWarn("events", "mainsetpointnow {}", mainsetpointnow)
  mainsetpointnew = mainsetpointnow -= 1
  logWarn("events", "mainsetpointnew {}", mainsetpointnew)
//      MainThermostat14_SetpointHeating.postUpdate(mainsetpointnew)    
    }  }

The problem is the operation to add one uses the mainsetpoint in degrees Kelvin. See log file output. (Note: I have suppressed the postUpdate, untill I can get this figured out.

2021-02-01 09:30:53.194 [WARN ] [org.openhab.core.model.script.events] - mainsetpointnow 66 °F
2021-02-01 09:30:53.203 [WARN ] [org.openhab.core.model.script.events] - mainsetpointnew 293.0388888888888888888888888888889
2021-02-01 09:32:05.657 [WARN ] [org.openhab.core.model.script.events] - mainsetpointnow 66 °F
2021-02-01 09:32:05.667 [WARN ] [org.openhab.core.model.script.events] - mainsetpointnew 291.0388888888888888888888888888889

Bob

DSL rules are quite loosely typed. You will not not get a Number here in your variable, you will get a state type object.

If your Item is say Number:Temperature type, the “default value” of it’s state is going to be something like 66 °F. That’s a Quantity - it’s not just a numeric, the units are part of the deal.

66 °F + 1
becomes a nonsense. Plus 1 what? °C, °F, K?
It guesses, and it has a bad guess.

66 °F + 1 °C
is in theory perfectly do-able, with the framework taking care of conversions.
However I think there is currently a bug here - the framework is muddling up which element is absolute temperature and which is the incremental temperature.

The safe way to work at the moment seems to be to get the Item state in known units, and use only those units in the rule.

Assuming you want to work in °F -

var mainsetpointnow = (MainThermostat14_SetpointHeating.state as QuantityType<Temperature>).toUnit("°F")
...
mainsetpointnew = 1 | °F + mainsetpointnow

Thanks. Your comments solved my issue. I have filled a log with all the options I have tried over two days and figured I was dealing with absolute zero, but did not know how to get out of the jam. I had tried the + 1|°F, but it was adding or subtracting 255.92 K. The key is the:

var mainsetpointnow = (MainThermostat14_SetpointHeating.state as QuantityType<Temperature>).toUnit("°F")

and I had not come across the posting you referenced after much searching, so was truly adrift until your response.

For the record DSL needs the variable before the addition & the var is also needed. The addition works with this;

var mainsetpointnew = mainsetpointnow + 1|°F

Thanks Bob

Also I see you are a frequent supporter on the forum and want to thank you for your continuing contributions to the Openhab community.