-
Notifications
You must be signed in to change notification settings - Fork 781
RfC: How to treat addition/substraction for temperature values #5697
Conversation
Signed-off-by: Kai Kreuzer <kai@openhab.org>
At least one answer there is:
This is pretty much what the RIs and derived implementations like uom-se do here. However for a more detailed analysis I added a Unit Test for Temperatures. |
It depends why one want to add temperatures. A value in °C or K may be an "absolute temperature" (T), in which case 20°C = 293.15 K. But the same value may also be a change of temperature (ΔT), in which case 20°C = 20°K. So how interpret 20°C + 2°C? It depends if the formula that we are trying to apply is:
There is no way to know if a JSR-363 One possible fix would be to introduce a new object which represent explicitly the difference between two
Other operations need more though. If we want to be consistent with addition rules, it may be:
|
I guess for your smarthome/iot application we are in the T + ΔT situation most of the time. As for the offset profile we just want to shift the value by an offset. The offset may be defined in any compatible unit though. |
The question here is: Do we need to implement proper offset handling ourselves? Or does the uom library support us in any way? |
They seem very specific to Temperature, I'm not sure, if the JSR should ever support that. We have extension modules like https://github.com/unitsofmeasurement/uom-domain (e.g. support for MicroProfile Metrics and other use cases like Prometheus, etc.) so hard to say, if an API element like |
This is not specific to temperature. This is an issue for all quantities having a non-zero offset in the conversion formula from "system unit". Temperature is the main example, but not the only one. We have at least:
I'm familiar with density sigma-t because it is used in oceanography, but I guess that other fields in science have similar units too. To repeat myself: any unit having a "+" or "-" operation in their definition will have this problem with current JSR-363 API. |
I could not see JScience offering any approach to this either, at least not in the final v 4.x.
JScience does have a |
|
Added an issue on unitsofmeasurement/unit-api#95 |
Please use the |
The Thus, the following tests works, because it adds 1°C and 1K and results in 2°C. If you would change it to add 1°C and 1°F (this is not an absolute value but a relative offset), I would expect that it results in 1°C + 5/9°C = 1.55555555556°C But instead it calculates 256.9277777777778°C. I have now implemented it in: https://github.com/eclipse/smarthome/pull/5679/files#diff-7971e9673feb59b9446980a49b516693R166 like this: private static final @Nullable QuantityType<Temperature> ZERO_CELSIUS_IN_KELVIN = new QuantityType<>(0, SIUnits.CELSIUS).toUnit(SmartHomeUnits.KELVIN);
private static final @Nullable QuantityType<Temperature> ZERO_FAHRENHEIT_IN_KELVIN = new QuantityType<>(0, ImperialUnits.FAHRENHEIT).toUnit(SmartHomeUnits.KELVIN);
private @Nullable QuantityType<Temperature> handleTemperature(QuantityType<Temperature> qtState,
QuantityType<Temperature> offset) {
QuantityType<Temperature> finalOffset;
// do the math in kelvin and afterwards convert it back to the unit of the state
QuantityType<Temperature> kelvinState = qtState.toUnit(SmartHomeUnits.KELVIN);
QuantityType<Temperature> kelvinOffset = offset.toUnit(SmartHomeUnits.KELVIN);
if (offset.getUnit().equals(SIUnits.CELSIUS)) {
finalOffset = kelvinOffset.add(ZERO_CELSIUS_IN_KELVIN.negate());
} else if (offset.getUnit().equals(ImperialUnits.FAHRENHEIT)) {
finalOffset = kelvinOffset.add(ZERO_FAHRENHEIT_IN_KELVIN.negate());
} else {
// offset is already in kelvin
finalOffset = offset;
}
kelvinState = kelvinState.add(finalOffset);
return kelvinState.toUnit(qtState.getUnit());
} |
In the current version of uom-se this looks like a good workaround. |
Thank you all for the good discussion. I think with the solution @triller-telekom implemented we can now close here. We are looking forward to the next version of the unit API. |
Thanks for the update. Depending on whether the solution by @triller-telekom involves the Lambda sum() note, we plan to refactor that into the next version of Common Library (which uom-se also needs, thus you already use it under the hood) so other implementations like Eclipse UOMo, SIS, etc. may use it independently of the RI. See unitsofmeasurement/unit-api#100 |
This pull request has been mentioned on openHAB Community. There might be relevant details there: https://community.openhab.org/t/rule-broke-with-latest-nest-binding/57106/2 |
This pull request has been mentioned on openHAB Community. There might be relevant details there: https://community.openhab.org/t/rule-broke-with-latest-nest-binding/57106/4 |
I came across this when thinking about the System Offset Profile: If a sensor value is not correct, you might want to adjust it by e.g. adding "0.5 °C" to it using that profile.
I have tried this within some rules for the moment. Adding
20 °C + 2 °C
resulted in the expected22 °C
.Considering that we cannot rely on a specific unit, it can definitely happen that temperature values with two different units are added. Doing a
20 °C + 2 K
, the result is a surprising-251.15 °C
. Clearly the calculation first converts the K to °C and then calculates the sum - but it does not really reflect the expectation of22 °C
here.Reading https://physics.stackexchange.com/questions/132720/how-do-you-add-temperatures, the issue is that temperatures cannot be really added, one can only add/substract "temperature differences" - which unfortunately cannot be cleanly identified.
Nonetheless, when such a calculation is done, the only logical interpretation hence seems to be to treat the second operand as a temperature difference. And if we do so, we have to treat the above result as a bug - the added unit test (which fails) shows this situation.
As the code in question is in tec-uom and not in ESH - @keilw, could you please comment and either tell me where my reasoning is wrong or confirm that it is a bug that needs to be reported at tec-uom? Thanks!
Signed-off-by: Kai Kreuzer kai@openhab.org