Skip to content

Persistence timeseries fix updating Item state#5403

Merged
holgerfriedrich merged 20 commits intoopenhab:mainfrom
mherwege:persistence_timeseries
Mar 15, 2026
Merged

Persistence timeseries fix updating Item state#5403
holgerfriedrich merged 20 commits intoopenhab:mainfrom
mherwege:persistence_timeseries

Conversation

@mherwege
Copy link
Copy Markdown
Contributor

@mherwege mherwege commented Mar 5, 2026

Fixes #4462
Closes #4662

It has been reported a few time that a timeSeries did not update the Item state when passing the timeSeries entry timestamp.

The issue is that persisting a timeSeries from the persistence extension persist method did not get the forecast job running. As long as there were future values in the timeSeries, updating the timeSeries would keep the logic going. But once the timeSeries runs out, a new timeSeries provided through persistence extensions did not start the job anymore. Restarting or resaving the Item would also solve this and get it running again.
This issue is fixed with this PR.

Second, as noted in the second issue to be closed, loading a timeSeries with values in the past would not update the Item state. Restarting openHAB afterwards would if a restoreOnStartup strategy is configured.
While I think it is a bad idea to blindly set the Item state with values from a timeSeries that is being loaded, there certainly is improvement possible. My proposed solution is: if a timeSeries is loaded with values in the past, look for the last value in the past. If the timestamp of that value is later then the Item's last update timestamp, use the value from the timeSeries to set the Item state. As persistence is not involved in this check (it is only the Item's internal state and timestamps), this is only dependent on the Item's state and not if it is persisted or not.
This makes using timeSeries for updating past values more consistent, and will also make sure the item still gets updated when a timeSeries with forecast data is loaded late, after the previous values ran out.

As a logical consequence of 2, I also implemented logic to respect UPDATE and CHANGE strategies for other persistence services (not the one getting the new FORECAST with elements in the past). If the Item state gets updated because a new past state replaced the (older) state of the item, other persistence services then the one from the FORECAST will get their UPDATE or CHANGE strategy applied and a record will be persisted if needed.

Also see https://community.openhab.org/t/a-few-issue-with-time-series/168678
Also see https://community.openhab.org/t/energy-forecast-binding/168567/30

mherwege added 6 commits March 5, 2026 16:28
Signed-off-by: Mark Herwege <mark.herwege@telenet.be>
when persisting or removing

Signed-off-by: Mark Herwege <mark.herwege@telenet.be>
timeseries

Signed-off-by: Mark Herwege <mark.herwege@telenet.be>
Signed-off-by: Mark Herwege <mark.herwege@telenet.be>
manager

Signed-off-by: Mark Herwege <mark.herwege@telenet.be>
Signed-off-by: Mark Herwege <mark.herwege@telenet.be>
@mherwege mherwege requested a review from a team as a code owner March 5, 2026 15:42
Signed-off-by: Mark Herwege <mark.herwege@telenet.be>
@mherwege mherwege changed the title Persistence timeseries fix updating item State Persistence timeseries fix updating Item state Mar 5, 2026
@mherwege mherwege marked this pull request as draft March 5, 2026 16:04
Signed-off-by: Mark Herwege <mark.herwege@telenet.be>
@mherwege mherwege marked this pull request as ready for review March 5, 2026 16:24
@wborn wborn requested a review from Copilot March 5, 2026 16:38
Copy link
Copy Markdown

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.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

mherwege added 2 commits March 5, 2026 18:38
Signed-off-by: Mark Herwege <mark.herwege@telenet.be>
Signed-off-by: Mark Herwege <mark.herwege@telenet.be>
@mherwege mherwege requested a review from Copilot March 5, 2026 19:43
Copy link
Copy Markdown

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.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@mherwege mherwege requested a review from Copilot March 5, 2026 20:33
Copy link
Copy Markdown

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

Copilot reviewed 5 out of 5 changed files in this pull request and generated 6 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Signed-off-by: Mark Herwege <mark.herwege@telenet.be>
Copy link
Copy Markdown

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

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@mherwege mherwege marked this pull request as draft March 6, 2026 06:33
Signed-off-by: Mark Herwege <mark.herwege@telenet.be>
Copy link
Copy Markdown

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

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 456 to 472
public void handleExternalPersistenceDataChange(PersistenceService persistenceService, Item item) {
if (!(persistenceService instanceof QueryablePersistenceService)) {
return;
}
persistenceServiceContainers.values().stream()
.filter(container -> container.persistenceService.equals(persistenceService) && container
.getMatchingConfigurations(FORECAST).anyMatch(itemConf -> appliesToItem(itemConf, item)))
.forEach(container -> container.scheduleNextPersistedForecastForItem(item.getName()));
.filter(container -> container.persistenceService.equals(persistenceService) && Stream
.concat(container.getMatchingConfigurations(UPDATE),
container.getMatchingConfigurations(FORECAST))
.distinct().anyMatch(itemConf -> appliesToItem(itemConf, item)))
.forEach(container -> {
container.scheduleNextPersistedForecastForItem(item.getName());
PersistedItem persistedItem = container.getPersistedItem(item);
if (persistedItem != null) {
container.restoreItemState(item, persistedItem);
}
});
}
Copy link

Copilot AI Mar 6, 2026

Choose a reason for hiding this comment

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

The handleExternalPersistenceDataChange method has been significantly expanded (now includes getPersistedItem query and restoreItemState) but there are no tests for it in PersistenceManagerTest. Consider adding a test that verifies:

  1. The early return for non-queryable persistence services
  2. The item state restoration behavior when external persistence data changes
  3. The forecast scheduling after an external data change

The PersistenceExtensionsTest only verifies that handleExternalPersistenceDataChange is called on the mock, but doesn't verify the internal behavior of the method itself.

Copilot uses AI. Check for mistakes.
Signed-off-by: Mark Herwege <mark.herwege@telenet.be>
mherwege added 3 commits March 6, 2026 09:23
Signed-off-by: Mark Herwege <mark.herwege@telenet.be>
Signed-off-by: Mark Herwege <mark.herwege@telenet.be>
Signed-off-by: Mark Herwege <mark.herwege@telenet.be>
Copy link
Copy Markdown

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

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Signed-off-by: Mark Herwege <mark.herwege@telenet.be>
Copy link
Copy Markdown

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

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Signed-off-by: Mark Herwege <mark.herwege@telenet.be>
Copy link
Copy Markdown

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

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Signed-off-by: Mark Herwege <mark.herwege@telenet.be>
Signed-off-by: Mark Herwege <mark.herwege@telenet.be>
Copy link
Copy Markdown

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

Copilot reviewed 6 out of 6 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@mherwege mherwege marked this pull request as ready for review March 8, 2026 17:29
Copy link
Copy Markdown
Member

@holgerfriedrich holgerfriedrich left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@holgerfriedrich holgerfriedrich merged commit 775f2b3 into openhab:main Mar 15, 2026
5 checks passed
@holgerfriedrich holgerfriedrich added the bug An unexpected problem or unintended behavior of the Core label Mar 15, 2026
@holgerfriedrich holgerfriedrich added this to the 5.2 milestone Mar 15, 2026
@mherwege mherwege deleted the persistence_timeseries branch March 15, 2026 12:03
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 the Core

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Possible inconsistency on persistence Layer / possible Item State inconcistency [persistence] Forecast Items only update whilst future states exist

3 participants