Skip to content

Conversation

mherwege
Copy link
Contributor

@mherwege mherwege commented Aug 12, 2025

See discussion: https://community.openhab.org/t/javascript-persistance-totals-off/165099

Many of the persistence extensions are internally converting to the system unit to make them indepent of unit offsets. The Quantity results returned where in this system unit, and not in the item unit. Typically, persisted values are stored in the item unit.

This PR tries to return the item unit (or a unit derived from it) as much as possible.

  • sumSince, sumUntil and sumBetween will now always return the result in the item unit.
  • riemannSumSince, riemannSumUntil riemannSumBetween will return the result in item unit * s.
  • sumSince, sumUntil and sumBetween will now always return the result in the item unit.
  • varianceSince, varianceUntil and varianceBetween return the result in item unit ^2.
  • deviationSince, deviationUntil and deviationBetween return the result in item unit.

Depending on the item unit, the resulting unit may get simplified automatically. This does not happen for pre-defined composed units (such as kWh/s will not automatically eliminate the time dimension, but kW * h / s will).

The changes in this PR are changing the results of the riemannSum, variance, and deviation extensions for Temperatures as they will now be calculated using the item unit, and not the system unit for temperatures (K). I thought long about this, but I believe most people will expect these calculations not to be done in K. Note that evolutionRate was already doing the calculation in the item unit, making the persistence extension inconsistent.

@mherwege mherwege requested a review from a team as a code owner August 12, 2025 14:00
@openhab-bot
Copy link
Collaborator

This pull request has been mentioned on openHAB Community. There might be relevant details there:

https://community.openhab.org/t/javascript-persistance-totals-off/165099/19

@wborn wborn requested a review from Copilot August 12, 2025 20:43
Copilot

This comment was marked as outdated.

@wborn wborn requested a review from Copilot August 13, 2025 06:09
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR updates persistence extensions to preserve the item unit in calculation results instead of converting to system units. Previously, methods like riemannSum, variance, and deviation were converting quantities to system units (e.g., Kelvin for temperatures) before calculations, which resulted in outputs in system units rather than the original item units.

Key changes include:

  • Modified variance and deviation calculations to use item units instead of system units
  • Updated Riemann sum calculations to preserve item units in results
  • Removed Kelvin offset handling from test utilities
  • Added comprehensive test coverage for EnergyPrice quantities

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

File Description
TestPersistenceService.java Removes Kelvin offset handling and simplifies test value generation
PersistenceExtensionsTest.java Updates test expectations to verify item units are preserved and adds EnergyPrice test coverage
PersistenceExtensions.java Modifies core calculation logic to use item units instead of system units and adds documentation notes

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@openhab-bot
Copy link
Collaborator

This pull request has been mentioned on openHAB Community. There might be relevant details there:

https://community.openhab.org/t/persistence-in-rule/166016/3

Signed-off-by: Mark Herwege <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants