HistoricItem not working as expected in OH 3.4.1

Hello,

I’m using OH 3.4.1 with Javascript ECMAScript-2021 rules.

I wish to retrieve the last other previous state of an item, it should return HistoricItem or NULL.

Persistance is working fine. Entries are in the DB, using MySQL.

The item is a string.

var Duco_Ventilatie_Trigger = items.getItem("Duco_Ventilatie_Trigger");
var previousState = Duco_Ventilatie_Trigger.history.previousState(true);
console.log(previousState);
console.log(previousState.state);
console.log(previousState.timestamp);

Outputs:

badkamer
undefined
undefined

Other functions which should return a HistricItem also return just the state name.

Im using this documentation as my source: openhab-js/README.md at main · openhab/openhab-js · GitHub

There were changes in how openhab-js handles the persistence functions recently.

When you go to the OH Docs, pay attention to the version you are looking at. In the upper left you will see “Stable (3.4.1)” and “Latest (4.0.0)”. You are running 3.4.1 so you must be sure to look at the docs for that version where you will see that in 3.4.1, previousState returns just a string.

.previousState(skipEqual, serviceId) ⇒ string | null

Ah so I see, I was reading topics of 2021 and earlier about previousState().state ext so I thought this was already available in JS but I was probably looking at DSL based topics. Thanks!

I’ve solved my issue on 3.4.1 by directly using the previousState() of the PersistenceExtensions. I then get a full item returned. I will use this until 4 becomes stable.

var PersistenceExtensions = Java.type('org.openhab.core.persistence.extensions.PersistenceExtensions');
var previousState = PersistenceExtensions.previousState(Duco_Ventilatie_Trigger.rawItem, true);