Skip to content

Commit 3476b94

Browse files
committed
fix state initialization in case of timeSeriesUpdated and currentState == UnDefType.NULL
Signed-off-by: Laurent ARNAL <laurent@clae.net>
1 parent 0f354e6 commit 3476b94

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

bundles/org.openhab.core.persistence/src/main/java/org/openhab/core/persistence/internal/PersistenceManagerImpl.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
import org.openhab.core.service.StartLevelService;
7575
import org.openhab.core.types.State;
7676
import org.openhab.core.types.TimeSeries;
77+
import org.openhab.core.types.TimeSeries.Entry;
7778
import org.openhab.core.types.UnDefType;
7879
import org.osgi.service.component.annotations.Activate;
7980
import org.osgi.service.component.annotations.Component;
@@ -339,6 +340,7 @@ public void timeSeriesUpdated(Item item, TimeSeries timeSeries) {
339340
// discard empty time series
340341
return;
341342
}
343+
342344
persistenceServiceContainers.values().stream()
343345
.filter(psc -> psc.persistenceService instanceof ModifiablePersistenceService)
344346
.forEach(container -> Stream
@@ -375,6 +377,33 @@ public void timeSeriesUpdated(Item item, TimeSeries timeSeries) {
375377
}
376378
});
377379
}));
380+
381+
State state = item.getState();
382+
if (UnDefType.NULL.equals(state)) {
383+
Iterator<Entry> iter = timeSeries.getStates().iterator();
384+
Instant entryTs = null;
385+
State entryState = null;
386+
Instant now = Instant.now();
387+
boolean endLoop = false;
388+
389+
while (!endLoop && iter.hasNext()) {
390+
Entry entry = iter.next();
391+
if (entry.timestamp().isBefore(now)) {
392+
entryTs = entry.timestamp();
393+
entryState = entry.state();
394+
} else {
395+
endLoop = true;
396+
}
397+
}
398+
399+
GenericItem genericItem = (GenericItem) item;
400+
if (entryState != null) {
401+
ZonedDateTime zdt = entryTs.atZone(ZoneId.systemDefault());
402+
genericItem.removeStateChangeListener(PersistenceManagerImpl.this);
403+
genericItem.setState(entryState, null, zdt, null);
404+
genericItem.addStateChangeListener(PersistenceManagerImpl.this);
405+
}
406+
}
378407
}
379408

380409
@Override

0 commit comments

Comments
 (0)