ECMAScript-2021 PreviousStateTimestamp / EpochMilli

Hello,

I’m migrating some DSL scripts to ECMAScript2021, but having some issues with the timestamp and conversion to EpochMilli of the previous state.

In DSL it works like this.

    logInfo("Test","Time_-1:      {}",stromZaehler.previousState(true, "jdbc").timestamp)
    logInfo("Test","Time_-1 Epochi:      {}",stromZaehler.previousState(true, "jdbc").timestamp.toInstant().toEpochMilli())

What would be the same in ECMAscript 2021 ?

    console.info("Test","Time_-1:      {}",stromZaehler.history.previousStateTimestamp(true, "jdbc"));
    console.info("Test","Time_-1 Epochi:      {}",stromZaehler.history.previousStateTimestamp(true, "jdbc").toInstant().toEpochMilli());

This gives an “TypeError: stromZaehler.history.previousStateTimestamp is not a function”. How to calculate the epochi milli I also have no idea.

Thanks!

previousStateTimestamp is actually mentioned here, but I can’t make it working.

https://openhab.github.io/openhab-js/items.ItemHistory.html#previousStateTimestamp

Is it really implemented as it is missing here:

The JS library is shipped in version 3.1.2 when installing the JS Scripting add-on, I guess previousStateTimestamp was added in a later version.
Please note that previousStateTimestamp is likely to be removed in favour of previousState returning an object including state and timestamp - see this issue report.

Thanks for the info. Ok that means in the future we will have:

item.previousState(true, "jdbc").state
item.previousState(true, "jdbc").timestamp

I think it will take till the next OpenHAB release until it is available. Is there currently some kind of workaround for OH3.4 to get the timestamp?

I think you can install the latest version manually (as described here), which should give you previousStateTimestamp (for now).

There is almost never a case where you should be messing with epoch anyway. It’s far better to keep the ZonedDateTimes and Duration to work with date times.

This is true in both Rules DSL and JS Scripting.

Note, the current version in main depends on some changes in OH core. I wouldn’t expect it to necessarily work on older OH 3.x versions.

Don’t. There’s no reason to. If you are comparing two timestamps, compare them directly using isBefore() and isAfter(). If you need the difference between two timestamps, use time.Duration.between(time1, time2).