Adding temperatures in rule script gives unexpected results

  • Platform information:
    • Hardware: Raspberry Pi 3
    • OS: OpenHABian
    • openHAB version: 3.0.2

I am attempting to create some rules for Nest thermostats to change their setpoints (target temperature) on certain conditions. I can create a basic rule to set the setpoint to a specific input value. However, what I actually need is to change the setpoint by an offset (in this case 5 or 10 degrees F). I couldn’t figure out a way to do that using the Rules GUI, so I’m trying to write is as a script in the rule’s actions. The script is simple: get the current thermostat setpoint, add the offset, and reset the setpoint to the new temperature.

Here’s my current test code:

logInfo("TEST Reset Upstairs - Start", "Attempting to reset Upstairs_Setpoint. Current setpoint state = " + Upstairs_Setpoint.state)
  
var setpoint = Upstairs_Setpoint.state as Number
var offset = Cooling_Offset_degF.state as Number
logInfo("TEST Reset Upstairs - Working", "old setpoint = " + setpoint + ", offset = " + offset)

setpoint = setpoint + offset
logInfo("TEST Reset Upstairs - Working", "new setpoint = " + setpoint)
sendCommand(Upstairs_Setpoint, setpoint)

logInfo("TEST Reset Upstairs - End", "Finished resetting Upstairs_Setpoint. New setpoint state = " + Upstairs_Setpoint.state)

And my log file (from /var/log/openhab/openhab.log) gives:

2021-07-07 22:59:31.321 [INFO ] [odel.script.TEST Reset Upstairs - Start] - Attempting to reset Upstairs_Setpoint. Current setpoint state = 89.999996 °F
2021-07-07 22:59:31.331 [INFO ] [el.script.TEST Reset Upstairs - Working] - old setpoint = 89.999996 °F, offset = 5.0 °F
**2021-07-07 22:59:31.341 [INFO ] [el.script.TEST Reset Upstairs - Working] - new setpoint = 563.522220**
2021-07-07 22:59:31.350 [INFO ] [.model.script.TEST Reset Upstairs - End] - Finished resetting Upstairs_Setpoint. New setpoint state = 89.999996 °F
2021-07-07 22:59:31.849 [WARN ] [binding.nest.internal.sdm.api.SDMAPI] - SDM API error: Temperature setpoint is out of range [10.000000, 32.222301] in FAHRENHEIT.

The script seems to execute without error, but it evaluates the temperature addition incorrectly. It evaluates 89.999996 + 5.0 = 563.522220.

After digging around the forums and documentation, I’m guessing this is some kind of unit conversion issue, even though both setpoint and offset are already in degrees F. However, I didn’t find any resolutions from my forum searches. I tried some of the temperature item state conversion methods shown in the Rules totorial at OpenHAB | Rules #Working with item state conversions as such:

setpoint = setpoint|"°F" + offset|"°F"

But that didn’t work either. How would I revise this script to correctly perform the addition?
Below is the complete rule code:

triggers: []
conditions: []
actions:
  - inputs: {}
    id: "1"
    configuration:
      type: application/vnd.openhab.dsl.rule
      script: >-
        logInfo("TEST Reset Upstairs - Start", "Attempting to reset Upstairs_Setpoint.
        Current setpoint state = " + Upstairs_Setpoint.state)
          
        var setpoint = Upstairs_Setpoint.state as Number
        var offset = Cooling_Offset_degF.state as Number
        logInfo("TEST Reset Upstairs - Working", "old setpoint = " + setpoint + ", offset = " + offset)
        setpoint = setpoint + offset
        logInfo("TEST Reset Upstairs - Working", "new setpoint = " + setpoint)
        sendCommand(Upstairs_Setpoint, setpoint)

        logInfo("TEST Reset Upstairs - End", "Finished resetting Upstairs_Setpoint. New setpoint state = " + Upstairs_Setpoint.state)
    type: script.ScriptAction

Work with Quantity types.

The cause of your problem -

yields a number with units, but this does not behave like a true quantity type in computations.

Thanks for the link. I figured it was an issue with units, but I wasn’t finding the method for properly converting them. This was very helpful.
I revised my script as such:

var setpoint = (Upstairs_Setpoint.state as QuantityType<Number>).toBigDecimal
var offset = (Cooling_Offset_degF.state as QuantityType<Number>).toBigDecimal

and it is now working!