Skip to content
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

[jsscripting] divide using quantity provides wrong results #16372

Closed
MikeTheTux opened this issue Feb 4, 2024 · 2 comments
Closed

[jsscripting] divide using quantity provides wrong results #16372

MikeTheTux opened this issue Feb 4, 2024 · 2 comments
Labels
bug An unexpected problem or unintended behavior of an add-on

Comments

@MikeTheTux
Copy link
Contributor

When dividing Temperature Quantities (°C or °F) the result is unexpected:

My test code:

console.log("OK:  " + Quantity('13.57 W').divide('25 W'));
console.log("NOK: " + Quantity('13.57 °C').divide('25 °C'));
console.log("NOK: " + Quantity('13.57 °F').divide('25 °F'));
console.log("OK:  "  + Quantity('13.57 Wh').divide('25 Wh'));

Expected Behavior

4 times 0.5428

Current Behavior

2024-02-04 19:37:03.491 [INFO ] [hab.automation.script.file.energy.js] - OK:  0.5428
2024-02-04 19:37:03.495 [INFO ] [hab.automation.script.file.energy.js] - NOK: 0.96166359215160154284755995304376983552
2024-02-04 19:37:03.498 [INFO ] [hab.automation.script.file.energy.js] - NOK: 0.976416943487321270142571234035529257646688812319034944739032056175667696
2024-02-04 19:37:03.501 [INFO ] [hab.automation.script.file.energy.js] - OK:  0.5428

Your Environment

openHAB 4.1.1 Release Build

@MikeTheTux MikeTheTux added the bug An unexpected problem or unintended behavior of an add-on label Feb 4, 2024
@mherwege
Copy link
Contributor

mherwege commented Feb 4, 2024

This is not a bug. You cannot divide temperatures unless you are working in K. °C and F are not on an absolute scale.
QuantityTypes also allow dividing e.g. °C by F, and the result would become unpredictable.

Here is a PR with a discussion on this and related topics, that intended to fix unexpected behaviour: openhab/openhab-core#3792

@MikeTheTux
Copy link
Contributor Author

Thanks for the info. It explains well the recognized behavior.

Quantity('13.57 K').divide('25 K') provides the expected result OK: 0.5428

In my examples, °F and °C have been implicitly transformed to K before doing the divide operation. This also explains the result >= 0,9.

Actually in my use case I'm dividing two Δ temperatures with each other - now I know, that I need to implement the entire formula in K.

const min = Quantity("40 °C");
const max = Quantity("66.5 °C");
let water_SOC_wrong = water.subtract(min).divide(max.subtract(min)).toUnit('%');
let water_SOC_right = water.toUnit("K").subtract(min.toUnit("K")).divide(max.toUnit("K").subtract(min.toUnit("K"))).toUnit('%');

I hope the discussion helps others who have the same issue.

closing the ticket.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug An unexpected problem or unintended behavior of an add-on
Projects
None yet
Development

No branches or pull requests

2 participants