Removing metadata in Javascript (ECMAScript 2021+)?

Using an incorrect, intermediate javascript, I accidently added a value to the (“widgetOrder”) metadata field of all(!) my items :slightly_frowning_face:. In order to resolve this I am trying to create a script that removes this metadata from all items.

So far I succeeded in updating the “widgetOrder” metadata value to be “” for all items. Unfortunately I cannot find a function in the javascript library here to completely remove metadata from an item.

I did notice a remove_metadata function in Jython, however there is no function in Jython to obtain all openhab items (like getItems() in javascript).

As I have several thousands of items, it is not a reasonable option to manually remove this metadata field for each item.

Any suggestion on how I can resolve this? Will a javascript function to remove metadata from an item be added in the (near) future?

The metadata registry does have a remove method, but, as you point out, the helper libraries don’t provide access to it yet. That just means that in your script you need to get access to the registry yourself. There are plenty of examples on the forum here (you’ll just need to make sure that you’re using the correct javascript version). The ECMAScript-2021 version should look roughly like this:

var FrameworkUtil = Java.type('org.osgi.framework.FrameworkUtil');
this.ScriptHandler = Java.type('org.openhab.core.automation.module.script.rulesupport.shared.ScriptedHandler');
var _bundle = FrameworkUtil.getBundle(ScriptHandler.class);
var bundle_context = _bundle.getBundleContext();
var MetadataRegistry_Ref = bundle_context.getServiceReference('org.openhab.core.items.MetadataRegistry');
var MetadataRegistry = bundle_context.getService(MetadataRegistry_Ref);

The remove method takes a MetadataKey parameter, so you’ll have to get access to that as well:

var MetadataKey = Java.type('org.openhab.core.items.MetadataKey');

Then, removing the widgetOrder metadata should be as simple as calling:

MetadataRegistry.remove(new MetadataKey('widgetOrder','Item_Name_Here'));

The library does have an easier way to get at OSGI services.

var MetadataRegistry = osgi.getService('org.openhab.core.items.MetadataRegistry');

That seems like a worthy thing to file an issue to add. It’s not a challenging add and I don’t think it would be controversial. Heck, I’ll even add it myself once I get a few of these other PRs I’m slowly working on off my plate.

This should do it:

    allItems = ir.getItems()

IIRC the Item registry is available be default in a script (you have to do something special to access it from a module though).

If you just want the names of the all the Items, the keys on the items map are the names of all the Items. I think you can get at them with something like

items.getKeys()

Only if an issue is filed and someone volunteers to implement it. -

Thank you, both! I successfully removed the “widgetOrder” metadata using the following script:

var MetadataRegistry = osgi.getService('org.openhab.core.items.MetadataRegistry');
var MetadataKey = Java.type('org.openhab.core.items.MetadataKey');

// Remove Wiget Order Metadata for all items
for each (var item in items.getItems()) {
          MetadataRegistry.remove(new MetadataKey("widgetOrder", item.name));
          console.log("Item updated", item.name);
          };

1 Like