Issue with Quantity of temperature since upgrade to OH4.1.1 from 4.0.4

The following code worked fine in OH4.0.4 but doesn’t in 4.1.0:

deltaTempGradient = Quantity('186' + 'K');'deltaTempGradient: ' + String(deltaTempGradient)));
deltaTimeGradient = Quantity('80' + 'min');'deltaTimeGradient: ' + String(deltaTimeGradient)));
heatingGradient = (Quantity(deltaTempGradient).divide(Quantity(deltaTimeGradient)));'heatingGradient: ' + String(heatingGradient)));
actualSetpoint = items.getItem('Remote_tutesOpenHAB_Gateway_Item_PLC_11_TSoll_Hauptschmelze').state;'Setpoint: ' + String(actualSetpoint)));
actualTemperature = items.getItem('remote_PLC_11_Item_PLC_11_TIst_Hauptschmelze').state;'actualTemperature: ' + String(actualTemperature)));
actualDeltaTemp = (Quantity(actualSetpoint).subtract(Quantity(actualTemperature)));'actualDeltaTemperature: ' + String(actualDeltaTemp)));
timeNeededToHeat = (Quantity(actualDeltaTemp).divide(Quantity(heatingGradient)));

but now it behaves a bit strange, but explainable:

to explain what happens:
heating gradient gets calculated correctly 186 K / 80 min = 2.325 K/min
the substraction 215°C - 215°C = 0°C is somehow right, but it’s not… The result of °C - °C is always K[elvin], even we use it in language different, the difference between 18°C and 19°C is exactly 1 Kelvin.
And in the followup, OH4.1.1 quantity interpretes 0°C correctly as 273.15K and does the operation:
0°C [273.15K] / 2.325 K/min = 117.483870967… min

I don’t know how it did it before, but it worked correctly resulting in 0 min or the intended mins if there is a delta.
I ASSUME it was calculating correctly [°C] - [°C] = [K] or it did a incorrect conversion of 0°C to 0K.

So please correct this or give me a hint, where in github the code is, that does the quantity calculations…
Corrrect is
0°C equals 273.15K
And [°C] - [°C] = [K], where it is decideable if you first calculate and then convert or first convert and then calculate, what I suggest.

And yes, even some may not believe: 215°C - 215°C = 273,15°C… This is correct :slight_smile:

In between I will correct my code that it first converts to Kelvin and then starts the calculation, as this will result correctly also :slight_smile:

Yes, the behaviour was changed, see Adjust QuantityType calculations for temperatures by mherwege · Pull Request #3792 · openhab/openhab-core · GitHub

The PR contains a description about the why and how.

@Mherwege points to the what changed and why (tl;dr temperature calculations were not working correctly before). And because it’s fixed, you no longer need to mess with Kelvin any more.

You code though is a bit more complicated than it needs to be.

deltaTempGradient = Quantity('-87.15' + ' °C'); // if you want to stick with °C do so'deltaTempGradient: ' + String(deltaTempGradient)));
deltaTimeGradient = Quantity('80' + ' min');'deltaTimeGradient: ' + String(deltaTimeGradient)));
heatingGradient = deltaTempGradient.divide(deltaTimeGradient); // both temp and time gradients are already Quantities'heatingGradient: ' + String(heatingGradient)));
actualSetpoint = items.getItem('Remote_tutesOpenHAB_Gateway_Item_PLC_11_TSoll_Hauptschmelze').quantityState; // state is always a String, if you are going to do math get the quantityState'Setpoint: ' + String(actualSetpoint)));
actualTemperature = items.getItem('remote_PLC_11_Item_PLC_11_TIst_Hauptschmelze').quantityState;'actualTemperature: ' + String(actualTemperature)));
actualDeltaTemp = actualSetpoint.subtract(actualTemperature); // if you use quantityState you don't need to parse the Strings back into Quanties'actualDeltaTemperature: ' + String(actualDeltaTemp)));
timeNeededToHeat = actualDeltaTemp.divide(heatingGradient); // the result of a calculation with Quantities is a Quantity

You can also force the units to be what you want them to be

deltaTempGradient = Quantity('-87.15' + ' °C'); // if you want to stick with °C do so'deltaTempGradient: ' + String(deltaTempGradient.toUnit('°F'))); // converts to °F for logging