Openhab-js 5 persistence broken?

Hi guys,
Just updated to OH 4.2.0M2 and all my js rules are broken. It seems that with the latest openhab-js the item.history is no more available and replaced with item.persistence but averageSince return an empty object…
This is a demo code, FYI, AC_Power is a numeric value

this.evaluation_start = new Date(new Date() - 1200000); // Take latest 20m, 1200s * 1000ms
this.solar_radiation = items.AC_Power.persistence.averageSince(evaluation_start);

console.log(this.solar_radiation);

When I run it it print on the console:

2024-05-18 10:48:48.391 [INFO ] [nhab.automation.script.ui.scratchpad] - {}

Any ideas?
Best
Alessio

See here

So, in other words, it’s broken at the moment… ok I will stay on a previous version… thanks :slight_smile:

I don’t know how the package versioning works on jsscripting, but I thought the support for the persistence changes was already merged here [items] ItemHistory: Adjust to breaking changes & Add new methods for future states by florian-h05 · Pull Request #331 · openhab/openhab-js · GitHub and it is included in the version 5.0.0 https://www.npmjs.com/package/openhab?activeTab=versions

@jimtng but the persistence addon or core functions is not yet ready in Oh4.2m2 only in snapshot builds

correct

1 Like

@JimT is there a plan for a oh4.2m3 release?

It’s perfectly fine to use the current snapshot. I suspect m3 will be out either this weekend or the next weekend.

1 Like

It was never broken in any milestone release.
The only time it was broken was for a few snapshot builds while I was working on addressing the core changes inside the openhab-js library.

BTW in openhab-js 5, averageSince will return a PersistedState object, which will be shown as {} inside the logs. Instead of trying to log the object, log a property, e.g. averageSince(…).numericState.

@agalliazzo Can you please check your openhab-js version?

Create and run this JS script:

console.log(utils.OPENHAB_JS_VERSION);

From time to time if there are enough changes I release a new version of openhab-js, with the new version number following semver.
This will be published to npm, and possible not be fully compatible with any openHAB version older than the current snapshot.
The addon itself always comes with a version that js fully compatible to openHAB itself, so as long as you don’t manually install openhab-js, you don’t have to care about versioning and compatibility.

FYI milestone 3 is scheduled for this Sunday.

So for any given openhab version, you’d get a specific fixed version of jsscripting, and the only way to have it upgraded is by upgrading openhab itself? (Assuming of course you’re not manually installing / managing the npm yourself.)

In other words, the openhab-js version is married to the openhab version, just like how the addons/bindings version is married to the openhab they were distributed with?

Is the openhab-js bundled inside the addon binary?

Hi Florian, thanks for your reply, ok I will give a try in the afternoon, I miss the property numericState on the ItemPersistence object and my lack of knowledge in JS made me misunderstood the log output, that was probably my mistake…

1 Like

Exactly. As long as you don’t work with npm yourself, you cannot upgrade openhab-js.

I know that with JRuby this is different because you somehow can install Gems during runtime … I haven’t found a way yet to install npm packages programmatically from the add-on.
The only thing that I currently can think of is to provide an option to provide links to bundled libraries and then have the bundles downloaded by the add-on.

Exactly. At least as long as you use the included version and don’t install it manually wit

Yes, during the add-on build a version of openhab-js specified in the add-on POM is downloaded from npm, and then bundled using webpack. The bundle is then copied into the resources/node_modules directory and shipped inside the add-on JAR (or KAR).

Is it possible to install an npm package from inside a js script?

Not really. Of course you could call the system command line and use npm on it, but there is no „native“ way to install npm packages from inside JS.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.