Adding item with metadata config in JSScripting

Does anyone know how to add metadata to items using JSScripting .updateMetadataValue to add a pattern and options?

I’ve been trying variations of this:

items.getItem(newName).updateMetadataValue('stateDescription, {config: {pattern: "' + pattern + '"}}');

See this recent discussion

Thanks for that. I had a read through of that, but can’t quite figure out how to apply it to my scenario. This one looks like he is trying to copy the state description of an item to apply it to another. I want to apply a new state description.
You also seem to use a more complex method than just using .updateMetadataValue. Is this necessary?

If you follow the link in that thread to the github issue, you will find 1) a discussion of how to add metadata config to an item config object which you should be able to replicate and 2) You will see that the issue has been recently closed because a very new PR has greatly improved the JSScripting metadata handling.

The slightly more complex example is certainly still viable and works for adding metadata in addition to updating. But, you can try the item config object method or try the newest JSScripting libraries (there are instructions somewhere here on the forum for how to update just the libraries if you don’t want to update to a recent snapshot, but I have never tried this).

Thanks, but I’m not sure I understand. I’ve read through all of this and am still very confused. Are these new changes you mention only a few days old? I’m using 3.4 now and don’t see any milestone updates available.

I don’t know if the itemConfig approach will work because I’d have to redeclare every property. I can’t just do

  metadata: {
    stateDescription: {
      config: {
        pattern: '%d%%'

I just can’t wrap my head around the example in the post you linked to break it down for my use.

replaceMetadata(namespace, value, configuration) from Item - Documentation looks perfect for me, but I can’t get it to do anything. I don’t don’t know how I’m supposed to be writing it.

Yes. I suspect you need to be on a very recent 4.0 snapshot for these new metadata functions to be a part of the installed binding. But, as I mentioned, there are ways to install the newest version of the library that would have these changes without updating the binding. You’ll just have to search the forums for those library update methods if you want to go that route.

That is one of the new functions. Unless you have the newest openhab-js library you don’t have that function available to you.

In the OH core, the item metadata is all handled through the OH specific java classes. The first three lines of the example just import the metadata registry service and the underlying java classes that will be required to interact with the metadata service. Once you have all those pieces in place (which you only need to do once in a rule no matter how many times you then subsequently work with the metadata), the fourth line accesses the already existing stateDescription metadata for an item. The next line updates the pattern configuration in the stateDescription and the last line send the modified metadata back to the item via the metadata registry.

This is the most simplified version of these steps, not a recommendation that you use exactly those lines of code. For the most robust code you’d want to add some other pieces e.g., checks in case thestateDescription namespace hasn’t been added to the item yet.

I updated my test OpenHAB to snapshot 4.0.0.
I’m trying

    let itemToEdit = items.getItem(newName);
    itemToEdit.replaceMetadata('stateDescription', '', { pattern: pattern } );

but I’m getting an error

TypeError: itemToEdit.replaceMetadata is not a function

it looks like the JSScripting binding is up to date as well

I haven’t looked through the repo, it’s possible that you’re just on the cutting edge of features here and this change is newer than the most recent rebuild of the library.

@florian-h05 should be able to tell us when you can expect to be able to use the new metadata functions.

Your code looks good and should work, but @JustinG is right that you need the latest version of the library to use the refactored metadata methods.

There is currently no release that includes that recent change, I am currently not sure when I will release a new version because there are a few things I’d like to get into the next version.

Anyway, you can download the very latest library directly from GitHub as described here: GitHub - openhab/openhab-js: openHAB JavaScript Library for JavaScript Scripting Automation. You need the latest development version.

Please note that due to a recent change in the JS Scripting addon you eventually need to go to the openHAB Settings → JavaScript Scripting and disable usage of the Included openHAB JavaScript Library.

Thanks. I managed to install the new JSScripting and this is now working.

Do you mean this setting?

Should I change this now? Or when there is a new milestone update?

No, this setting should stay enabled.
When the setting I meant appears, you’ll find an additional setting below the first one.