I just tested essentially what you presented and it’s working fine for me.
var MetadataRegistry_Ref = bundle_context.getServiceReference("org.openhab.core.items.MetadataRegistry");
var MetadataRegistry = bundle_context.getService(MetadataRegistry_Ref);
var Metadata = Java.type("org.openhab.core.items.Metadata");
var MetadataKey = Java.type("org.openhab.core.items.MetadataKey");
var metadata = MetadataRegistry.get(new MetadataKey("mTest", "TestSwitch"));
metadata.configuration['name'] = 'written metadata';
// metadata.configuration.put('name', 'written metadata'); // this also works
logger.info(metadata.toString());
And viewing the metadata in the item settings shows the updated value as well. What I’m wondering, is the metadata you’re trying to modify defined in .items file(s)? You can not modify metadata that’s defined in .items files from the UI or in code.
Interestingly you can define metadata for items defined in .items files through the UI, however I just tested and you can not modify this using this method either. What you can do though is remove the metadata:
MetadataRegistry.remove(new MetadataKey("mTest", "TestSwitch")); //only works if the metadata was defined through UI
Which would then allow you to construct new metadata
The metadata was created using openhab UI. I Forgot to mention that I’m on Openhab version 3.1.0
I’ve added custom metadata “Scene”
value: value
config:
sceneItem: Scene_Diningroom
sceneTriggerCounter: 0
sceneToSet: Dinner
When I run your code
var logger = Java.type("org.slf4j.LoggerFactory").getLogger("org.openhab.model.script.Run From");
var FrameworkUtil = Java.type("org.osgi.framework.FrameworkUtil");
var _bundle = FrameworkUtil.getBundle(scriptExtension.class);
var bundle_context = _bundle.getBundleContext()
var MetadataRegistry_Ref = bundle_context.getServiceReference("org.openhab.core.items.MetadataRegistry");
var MetadataRegistry = bundle_context.getService(MetadataRegistry_Ref);
var Metadata = Java.type("org.openhab.core.items.Metadata");
var MetadataKey = Java.type("org.openhab.core.items.MetadataKey");
var metadata = MetadataRegistry.get(new MetadataKey("Scene", "ZWaveDiningDoubleRelaySwitch2x15kW_Switch1"));
metadata.configuration['sceneToSet'] = 'written metadata';
logger.info(metadata.toString());
which is essentially the same I’ll get :
java.lang.UnsupportedOperationException: null
at java.util.Collections$UnmodifiableMap.put(Collections.java:1457) ~[?:?]
at jdk.nashorn.internal.scripts.Script$Recompilation$19824$\^eval\_$cu1$restOf.:program(<eval>:11) ~[?:?]
at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:655) ~[jdk.scripting.nashorn:?]
at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:513) ~[jdk.scripting.nashorn:?]
at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:527) ~[jdk.scripting.nashorn:?]
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:456) ~[jdk.scripting.nashorn:?]
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:413) ~[jdk.scripting.nashorn:?]
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:409) ~[jdk.scripting.nashorn:?]
at jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:162) ~[jdk.scripting.nashorn:?]
at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:264) ~[java.scripting:?]
at org.openhab.core.automation.module.script.internal.handler.ScriptActionHandler.lambda$0(ScriptActionHandler.java:62) ~[?:?]
at java.util.Optional.ifPresent(Optional.java:183) ~[?:?]
at org.openhab.core.automation.module.script.internal.handler.ScriptActionHandler.execute(ScriptActionHandler.java:59) ~[?:?]
etc.
but, after some fiddling around I found out that this works: