Skip to content

[Persistence] Forecast scheduling fails after persisting TimeSeries at runtime with InfluxDB #6039

Backlog

Description

@TeddeBaer

Expected Behavior

When a TimeSeries containing future states is persisted for an Item configured with the forecast persistence strategy, openHAB should automatically schedule the corresponding Item state updates without requiring a restart.

This should work regardless of whether historical or future persistence records already exist.

Current Behavior

The TimeSeries is successfully stored in InfluxDB, but the corresponding Item state updates do not occur.

Restarting openHAB while future persistence records exist correctly restores the current Item state and schedules subsequent state updates.

However, persisting a new TimeSeries at runtime fails to initialize or refresh the forecast schedule.

The issue is reproducible with an isolated Switch Item without bindings, group memberships or additional persistence strategies.

Steps to Reproduce (for Bugs)

  1. Create a test Item
Switch ForecastTest "Forecast test"
  1. Configure persistence

In influxdb.persist, add the following Item configuration:

ForecastTest : strategy = forecast

No other persistence strategies should apply to this Item.

Ensure that the persistence configuration has been loaded before executing the script.

  1. Execute the following JavaScript
const now = time.toZDT();
const series = new items.TimeSeries('REPLACE');

series.add(now, 'OFF');
series.add(now.plusMinutes(2), 'ON');
series.add(now.plusMinutes(4), 'OFF');

items.ForecastTest.persistence.persist(series, 'influxdb');
  1. Observe the Item state

Expected:

  • Shortly after persistence: OFF
  • After two minutes: ON
  • After four minutes: OFF

Actual:

When using a newly created Item, its state remains NULL throughout the test.

All three records are successfully stored in InfluxDB.

Context

The issue affects automations that dynamically create or replace schedules using forecast persistence.

In my original use case, an EV charging automation successfully executed an initial charging schedule but silently failed to execute the remaining charging periods after the schedule was recalculated.

The persistence database contained the correct replacement schedule, but the corresponding Item state updates did not occur.

Your Environment

  • openHAB versions tested: 5.2.3 and 5.2.5
  • Installation: Docker
  • Persistence service: InfluxDB 2.10.1
  • Scripting: JavaScript Scripting (openhab-js)
  • Host operating system: Debian 13
  • Time zone: Europe/Helsinki (UTC+2 during testing)

Tests Conducted

All database timestamps are in UTC, while openHAB log timestamps are in local time (EET, UTC+2).

Test A: Successful forecast execution after restart

The following records were stored before restarting openHAB:

2026-11-29T15:03:41.178Z,0
2026-11-29T15:13:41.178Z,1
2026-11-29T15:15:41.178Z,0

The Item state remained NULL after persisting the TimeSeries.

After restarting openHAB at approximately 17:06 local time, the Item state was OFF.

Both subsequent scheduled state changes occurred successfully:

2026-11-29 17:13:41.180 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'ForecastTest' changed from OFF to ON (source: org.openhab.core.persistence)
2026-11-29 17:15:41.180 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'ForecastTest' changed from ON to OFF (source: org.openhab.core.persistence)

Both state changes occurred approximately 2 ms after their scheduled timestamps.

Test B: Persisting a TimeSeries after the forecast schedule has completed

After the last scheduled state update in Test A, no future records remained in the database.

A new TimeSeries was persisted without restarting openHAB:

2026-11-29T15:16:42.575Z,0
2026-11-29T15:18:42.575Z,1
2026-11-29T15:20:42.575Z,0

All records were successfully stored, but no corresponding Item state changes occurred. The Item remained OFF.

Test C: Persisting a TimeSeries while future records already exist

To determine whether the issue was limited to an exhausted forecast schedule, two records approximately 24 hours in the future were persisted:

2026-11-30T15:24:43.521Z,0
2026-11-30T15:26:08.121Z,0

openHAB was then restarted. After the restart, the Item state was OFF.

After startup, another TimeSeries was persisted using the JavaScript example above.

The database contained the following records:

2026-11-29T15:29:14.226Z,0
2026-11-29T15:31:14.226Z,1
2026-11-29T15:33:14.226Z,0
2026-11-30T15:24:43.521Z,0
2026-11-30T15:26:08.121Z,0

The expected state change at 17:31:14 local time did not occur. The Item remained OFF.

This demonstrates that the issue also occurs when future persistence records already exist.

The creation of a scheduled job for the next day's records was not independently verified in the TRACE log.

Related Issues and Previous Fix

This issue appears to be closely related to:

Although PR #5403 addressed forecast scheduling following persistence updates, the problem described here remains reproducible with InfluxDB persistence in openHAB 5.2.3 and 5.2.5.

Furthermore, the issue is not limited to situations where all previously persisted forecast records have expired.

Possible Root Cause

The test results suggest that forecast scheduling is correctly initialized during openHAB startup but fails to initialize or refresh correctly following TimeSeries persistence at runtime.

One possible explanation is a race condition between asynchronous InfluxDB writes and the persistence queries performed following handleExternalPersistenceDataChange().

If the persistence service is queried before the newly written records become available, the forecast scheduler may find no future records or schedule an outdated future state.

It has not yet been established whether the issue is specific to InfluxDB or also affects other persistence services.

Workaround

Restarting openHAB after persisting the TimeSeries successfully initializes the forecast schedule.

However, restarting openHAB whenever a forecast schedule changes is not a practical workaround for runtime automation.

Activity

  1. added
    bug
    on Nov 29, 2026
  2. TeddeBaer commented on Nov 29, 2026

    @TeddeBaer
    Author

    Update: Working workaround without restarting openHAB

    I conducted an additional test in openHAB 5.2.5 by explicitly calling handleExternalPersistenceDataChange() five seconds after persisting the TimeSeries.

    I appended the following JavaScript code immediately after the persist() call in the original test script:

    const { osgi, items } = require('openhab');
    
    setTimeout(() => {
        const manager = osgi.getService(
            'org.openhab.core.persistence.PersistenceManager'
        );
    
        const registry = osgi.getService(
            'org.openhab.core.persistence.PersistenceServiceRegistry'
        );
    
        const service = registry.get('influxdb');
        const item = items.getItem('ForecastTest').rawItem;
    
        manager.handleExternalPersistenceDataChange(service, item);
    
        console.info('Forecast refresh requested.');
    }, 5000);
    

    This successfully restored forecast scheduling without restarting openHAB. The subsequent scheduled Item state change occurred automatically:

    2026-11-29 21:12:50.226 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'ForecastTest' changed from OFF to ON (source: org.openhab.core.persistence)
    

    This result provides further evidence supporting the hypothesis of a race condition between asynchronous InfluxDB writes and the persistence queries performed when updating the forecast schedule.

    It appears that explicitly invoking handleExternalPersistenceDataChange() after allowing sufficient time for the database write to complete enables the existing forecast scheduling mechanism to work as intended.

    I have not yet tested shorter delays or an immediate second invocation of the method.

    This provides a practical workaround without restarting openHAB or implementing separate scheduling logic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions