Javascript in OH5.1.3

Hi,

I’m trying to upgrade from 5.0.3 to 5.1.3, but the changes to javascript have broken some of my rules and I don’t understand enough to figure it out.

I have been using things like

items.metadata.itemchannellink.replaceItemChannelLink(name, channeluid)
items.getItem(nameofanitem).replaceMetadata(blah blah)

and others from Home - openHAB JS

When upgrading to 5.1.3, these rules fail when hitting these parts.

Errors like:

Failed to execute rule systemBuilder-Lights: TypeError: undefined has no such function “replaceItemChannelLink”: TypeError: undefined has no such function “replaceItemChannelLink”

This feels like something to do with the new way things are injected? In the javascript settings page of the openhab UI, it looked like everything was set to automatic, but I don’t really understand what I’m looking at.

Can anyone help?

According to the docs for 5.1 (which you linked to):

The docs for 5.0 do not include either of these so if this was supported, it was an undocumented feature.

Based on this the line should be something like

items.itemChannelLink.replaceChannelLink('itemName', 'channeluid', null);

I actually use replaceMetadata quite frequently and nothing has changed with it as far as I can tell or the docs indicate.

Maybe there’s something in the “blah blah” causing problems. It needs to be replaceMetadata(namepspace, value, { }) where the third argument is an Object defining the configuration.

Without seeing the actual line of code :person_shrugging: If it’s the exact line you posted above, well items.metadata indeed doesn’t have a replaceItemChannelLink function.

No. It’s probably you were using an undocumented feature in openhab-js. Perhaps it was a feature that was in work and not done. This undocumented feature changed between when you started using it and when it became a documented feature on OH 5.1.

There’s nothing different about how this stuff is injected.

This determines how the openhab-js helper library is made available in your rule. It controls when the helper library is just there and available or when you need to explicitely import it using a require('openhab');

The wrap option enables the use of return, let and const in managed script conditions (i.e. UI rules). The script actions are automatically wrapped all the time.

The convert event option creates a wrapper around the event Object for managed rules users (i.e. UI users) same as .js file rules users get. See JavaScript Scripting - Automation | openHAB. Which this is disabled, the raw Java event object will continue to be injected into the script actions and script conditions.

If you’ve installed the openhab npm manually the cache option must be turned off.

If you’ve one or more libraries, enable the second option to cause your rules which use a given library to reload when that library changes.

Ok, that was confusing.

For a very long time my rules have been using items.metadata.itemchannellink to access everything in itemChannelLink - openHAB JS

I don’t know how I came to use this, as I must have followed the documents at the time, and it has been working right up until now with OH 5.1.3.

But yes, you are right, not only should it be items.itemChannelLink without the ‘.metadata’, but the case is slightly different between the two: ‘C’ and ‘L’ are uppercase in the correct version.

That’s the thing though, items.metadata and items.itemChannelLink do not appear in the docs for the JS Scripting add-on until OH 5.1. You couldn’t have learned about these from the base level docs. (JavaScript Scripting - Automation | openHAB).

Perhaps by looking at the class documentation or the source code itself. But be aware that those lower level docs are not considered the user facing docs. If it’s not in the add-on readme, it’s subject to change without notice.

The old itemchannellink has been replaced long time I ago with the openhab-js release that was bundled with openHAB 4.0.0 which was released summer 2023.

This has also been alerted back then: