-
-
Notifications
You must be signed in to change notification settings - Fork 429
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adjust QuantityType calculations for temperatures #3792
Conversation
Signed-off-by: Mark Herwege <mark.herwege@telenet.be>
I remember we discussed that in the past, maybe even in ESH times and I think something was changed back then. It would be good to find that discussion and see why it is the way it is now (or we broke it again because of insufficient tests). |
@J-N-K I think the discussion about it is #2386 and eclipse-archived/smarthome#5697. It still leaves me with an uncomfortable feeling about the decision. I also see the behaviour changed over time as the UOM library got updates. And I think there is a relatively easy solution to solve it to make it more intuitive for the typical smarthome calculations. If you look at the linked discusison from @rkoshak tests, it clearly show we leave the rules builder with a big issue. And it is because we do not properly define if the quantity should be interpreted absolute or relative. We can solve that. An alternative approach from what I do could be to define quantities absolute by default in constructing them (now it is relative). Only when adding or substracting, make the second argument relative. It would avoid the problem of potentially having to convert back to relative at the end of calculation. Defining as absolute by default should not make a difference for all units (except temperature) we typically use in the smarthome. And it would make it easier to understand for the person building a rule. Also, I see what got implemented as a workaround for this is only implemented in profiles, not calculations for rules. |
@J-N-K I actually think that with the change proposed here, the specific work around for temperatures in the SystemOffsetProfile Line 118 in e1d2b88
This workaround calls the add method I propose to correct here. |
Signed-off-by: Mark Herwege <mark.herwege@telenet.be>
Signed-off-by: Mark Herwege <mark.herwege@telenet.be>
Not required, as new QuantityType objects are created for the end result, and these default to relative. |
I now removed the workaround in the offsetProfile and added all tests I had in this PR. All tests (both old and new in QuantityType and old in SystemOffsetProfile) run through fine. |
Signed-off-by: Mark Herwege <mark.herwege@telenet.be>
Signed-off-by: Mark Herwege <mark.herwege@telenet.be>
The last commit improved associativity for adding temperatures. Now (A + B) + C = A + (B + C). Communautivity is also guaranteed. Distributivity will not work for temperatures (but will work for other units with only a scale factor): (A + B) * factor is not equal (A * factor) + (B * factor), e.g. (1 °C + 2°C) * 2 = 279.15 °C while 1 °C * 2 + 2 °C * 2 = 552.3 °C. The difference is because in the second example, the 2 °C are treated absolute in the multiplication. |
That all looks reasonable to me. The behaviors are much more intuitive now. |
Do we have a test for |
Signed-off-by: Mark Herwege <mark.herwege@telenet.be>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
Just added a test like that, confirmed that works. |
Special handling for temperature differences in °F and °F/%, DPT 9.002 and 9.003, need to be adapted due to change in core. Refs openhab/openhab-core#3792 Implementation is valid for 4.0 and 4.x snapshot. Fixes openhab#15567. Signed-off-by: Holger Friedrich <mail@holger-friedrich.de>
Special handling for temperature differences in °F and °F/%, DPT 9.002 and 9.003, needs to be adapted due to change in core. Refs openhab/openhab-core#3792. Implementation is valid for 4.0 and 4.x snapshot. Fixes openhab#15567. Signed-off-by: Holger Friedrich <mail@holger-friedrich.de>
Special handling for temperature differences in °F and °F/%, DPT 9.002 and 9.003, needs to be adapted due to change in core. Refs openhab/openhab-core#3792. Implementation is valid for 4.0 and 4.x snapshot. Fixes #15567. Signed-off-by: Holger Friedrich <mail@holger-friedrich.de>
I don't understand this change. Now I got this:
How is this making sense? |
Interesting. So getting the addition right breaks multiplication? |
I have a rule that calculates an estimated change in temperature based on a constant rate of change per hour (e.g. 1.8°C / hour). So the change in temp is rate of change * number of hours. The change here broke that calculation. I'll have to convert to float, multiply by the hours, then convert back to °C before subtracting. Here's the results of various calculations in rulesdsl
|
20 C = (273.15 + 20) K = 293.15 K 293.15 K / 2 = 146.575 K = -126.575 C It seems that units are mixed up. |
If you simply replace the change of 1.8 °C per hour by 1.8 K per hour it will work as expected. The issue with temperatures is that all of the calculations so far explicitly assumed we were always working with relative values. But internally, sometimes it was converted to absolute. This PR tries having it standardized. The impact is only with temperatures.
I do think it does make sense. Multiplying temperatures is only done on the absolute scale, which is K. I know what you intend to do is multiplying temperature differences and adding them to a starting temperature, but for formulas it is a guess that’s your intention. And a choice needed to be made. It was not consistent across units before. The easy solution to keep your formulas is multiplying with Kelvin where absolute is equal to relative. Also, before there was a difference between multiplying with a BigDecimal or a Dimensionless QuantityType. Results where different. Have a look at the original issue linked in this. Assume you have an item that gets updated with °C from one source and °F from another source. QuantityTypes will take proper care off it. But you can not reliably calculate with the states of the item anymore if you do not first convert to a base unit. With this PR, you can. |
This PR addressed the following problems: 1. Operates under the rhs unit ``` a = QuantityType.new("20 °C") b = QuantityType.new("9 °F") logger.warn a + b # => 45 °F - this should be 25 °C logger.warn b + a # => 25 °C - this should be 45 °F ``` self gets converted to other's unit + other converted to self.unit then added to self. This gave the wrong result in the wrong unit. The lhs just needs to be converted to the unit defined by the block, and rhs needs to be converted using `to_unit_relative` to the unit defined by the block. 2. Inside a unit block ``` a = QuantityType.new("20 °C") b = QuantityType.new("9 °F") unit("°C") do logger.warn a + b # => 7.2222 °C => This should be 25 °C logger.warn b + a # => 7.2222 °C => correct. 9 °F + 20 °C -> -12.78 °C + 20 °C logger.warn 2 + b # -> - 20.999 °F => Should be 2°C + 5°C = 7 °C logger.warn 2 - b # => 24.999 °F => Should be 2 - 5 = -3 °C end ``` 3. Multiplications inside a unit block didn't take up the block's unit ``` kw = 5 | "kW" unit("W") do logger.warn (kw * 2).format("%.0f %unit%") # => 10 kW => Should turn into 10000 W logger.warn (2 * kw).format("%.0f %unit%") # => 10 kW => Should turn into 10000 W end ``` 4. +/- against non-quantity type is still allowed. It takes up the other operand's unit. This should raise an exception instead. ``` kw = 5 | "kW" logger.warn kw + 5 # => 10kW logger.warn 5 + kw # => java.lang.NullPointerException: Cannot read field "quantity" because "state" is null ``` 5. Fix failing specs due to core changes in openhab/openhab-core#3792 --------- Signed-off-by: Jimmy Tanagra <jcode@tanagra.id.au>
Special handling for temperature differences in °F and °F/%, DPT 9.002 and 9.003, needs to be adapted due to change in core. Refs openhab/openhab-core#3792. Implementation is valid for 4.0 and 4.x snapshot. Fixes openhab#15567. Signed-off-by: Holger Friedrich <mail@holger-friedrich.de> Signed-off-by: AndreasV <andreas.viborg@gmail.com>
Special handling for temperature differences in °F and °F/%, DPT 9.002 and 9.003, needs to be adapted due to change in core. Refs openhab/openhab-core#3792. Implementation is valid for 4.0 and 4.x snapshot. Fixes openhab#15567. Signed-off-by: Holger Friedrich <mail@holger-friedrich.de>
Special handling for temperature differences in °F and °F/%, DPT 9.002 and 9.003, needs to be adapted due to change in core. Refs openhab/openhab-core#3792. Implementation is valid for 4.0 and 4.x snapshot. Fixes openhab#15567. Signed-off-by: Holger Friedrich <mail@holger-friedrich.de>
Special handling for temperature differences in °F and °F/%, DPT 9.002 and 9.003, needs to be adapted due to change in core. Refs openhab/openhab-core#3792. Implementation is valid for 4.0 and 4.x snapshot. Fixes openhab#15567. Signed-off-by: Holger Friedrich <mail@holger-friedrich.de>
Special handling for temperature differences in °F and °F/%, DPT 9.002 and 9.003, needs to be adapted due to change in core. Refs openhab/openhab-core#3792. Implementation is valid for 4.0 and 4.x snapshot. Fixes openhab#15567. Signed-off-by: Holger Friedrich <mail@holger-friedrich.de> Signed-off-by: querdenker2k <querdenker2k@gmx.de>
Special handling for temperature differences in °F and °F/%, DPT 9.002 and 9.003, needs to be adapted due to change in core. Refs openhab/openhab-core#3792. Implementation is valid for 4.0 and 4.x snapshot. Fixes openhab#15567. Signed-off-by: Holger Friedrich <mail@holger-friedrich.de> Signed-off-by: querdenker2k <querdenker2k@gmx.de>
This pull request has been mentioned on openHAB Community. There might be relevant details there: https://community.openhab.org/t/openhab-4-1-release-discussion/152252/87 |
This pull request has been mentioned on openHAB Community. There might be relevant details there: https://community.openhab.org/t/working-with-number-temperature-items-in-rules/116197/29 |
This pull request has been mentioned on openHAB Community. There might be relevant details there: |
Special handling for temperature differences in °F and °F/%, DPT 9.002 and 9.003, needs to be adapted due to change in core. Refs openhab/openhab-core#3792. Implementation is valid for 4.0 and 4.x snapshot. Fixes openhab#15567. Signed-off-by: Holger Friedrich <mail@holger-friedrich.de> Signed-off-by: Jørgen Austvik <jaustvik@acm.org>
This pull request has been mentioned on openHAB Community. There might be relevant details there: https://community.openhab.org/t/blockly-script-unable-to-divide-or-multiply-temperatures/157871/5 |
Signed-off-by: Mark Herwege mark.herwege@telenet.be
EDIT 05/09/23: Summary updated with analysis, conclusions and proposed behaviour.
In tests performed for a Blockly issue, it was identified that calculations with temperatures in °F produce strange results: openhab/openhab-webui#2001 (comment)
Notably:
The issue is that all quantities in OH are considered relative. This is fine for most dimensions and units that have the same zero value, but leads to issues with temperatures in general and with °F in particular (°C is easier to interpret as Δ 1° C = Δ 1 K).
This PR changes the behaviour by converting the quantities to absolute before doing the calculations, i.e.
Note that this has been discussed before: #2386 and eclipse-archived/smarthome#5697. It has lead to creating special treatment for temperatures in the SystemOffsetProfile, but no adjustment for rule languages in general. The proposed fix eliminates the need for a specific fix in the SystemOffsetProfile and brings the behaviour in rules in line with the offset profile behaviour.
With the proposed changes:
I did tests with this in a scratchpad using DSL. I believe this gives much more predictable and expected results than before. These tests are now also part of the Java unit tests.
Here is the DSL script used for the test:
And the result:
If this is an acceptable approach, I can convert the test DSL script to unit test.Open question for me still: should the result of calculations be converted to relative again. I would be happy to hear views on this.