JS ReplaceMetadata

Hi all,

tried a lot, but did not manage to get that working.

I just want to update the value of a item meta data, defined in the items file

Running Version 5.0.0.M2

Items

Rollershutter                     EG_Kueche_Beschattung_West                 "Terrassentür"                  <blinds>         (Groups_Beschattung_EG, EG_Kueche_Beschattung_gAll, EG_Kueche_Beschattung)    ["OpenState", "Level"]              {autoupdate="false", widgetOrder="0", channel="knx:device:bridge:Jalousieaktor_D04:Kanal_H", sollwertItem="EG_Kueche_Beschattung_West_Soll", expectedState="0", windowContactItem="EG_Kueche_Fenster_West", windowContactGekipptItem="EG_Kueche_Fenster_West_Gekippt"}

JS Rule

expectedState = event.receivedState;

// 1st try
rollershutterItem.replaceMetadata( 'expectedState', expectedState);

// 2nd try
items.metadata.replaceMetadata(rollershutterItem, 'expectedState', expectedState);

// 3rd try
items.metadata.replaceMetadata(rollershutterItem.itemName, 'expectedState', expectedState);

try

items.EG_Kueche_Beschattung_West.replaceMetadata('expectedState','new Value')

Thank you for your quick response.

I still get the error:

Could not update element with key expectedState:EG_Kueche_Beschattung_West in ManagedMetadataProviderImpl, because it does not exist.

This sounds logical to me, as your Item is defined in an .items file and not a UI managed Item.

1 Like

ah something rings a bell for me…

but what’s the best practice, when I don’t want to create my items via UI?

Can I initialize the metadata via rule? Or check if metadata is existing and create it if not?

No, you need to edit your .items file manually.

I can’t define it in the items File, because the value is calculated in a rule

Then you need to make it a managed Item by configuring it through the UI.

JRuby allows you to set/change metadata even on Items from .items file.

It is transient by default (i.e. not persisted to jsondb), but you can set the option to make it persistent.

This would be relatively new. Since the Item Metadata is stored in a different registry from the Item, it used to be OK to have .items files but be able to change the Item metadata from a rule. You could even change the metadata for stuff defined in .items files. Of course the next time the .items file was is loaded the metadata gets reset, but it wasn’t read only.

The same applies to Links. You can (could?) define the Item in a .items file but link the items to Channels through the UI.

There were some discussions in GitHub years ago about changing this behavior, but I argued against that (I thought).

Maybe it’s only the metadata that is in the .items file that is marked read only?

This is definitely something new and not something I like at all.

The error is somewhat odd but the mention of ManagedMetadataProviderImpl implies the error is coming from core.

But there is no reason why one could not and should not be able to define new managed metadata on a read-only Item.

Ok, I stand corrected, did not know that.

It’s not exactly what you’re asking, but I have a successful setup defining a bunch of items in Javascript and setting all aspects of them including Metadata. I have them set in functions that are called for each room where I want the items, so it’s a bit generic but hopefully you get the idea.

itemName = ac.names.enableFeedTemperature;
            metadata = {
                listWidget: metaListWidgetVisibleWithMode('oh-toggl
e-item'),
                defaultWidget: metaListWidgetVisibleWithMode('oh-to
ggle-item'),
            };
            const channel = `${thingNames[room.name]}:acEnableRules
`;
            // console.log(`channel=${channel} METADATA=${JSON.stri
ngify(metadata, null, 2)}`);
            items.replaceItem({
                name: itemName,
                type: 'Switch',
                // category: 'button',
                groups: [ac.names.groupName],
                channels: channel,

                label: `${helperAcRoomNiceName(room.name)} Enable F
eed Temperature`,
                tags: [...commonTags],
                metadata,
            });

I’m not changing things dynamically so this just runs once per room when I start up openHAB or change the Javascript file, but it seems to work just fine either way.

FWIW I have these functions that change Metadata. I don’t seem to be using them anywhere. I wrote this code a couple years ago and don’t remember why I settled on the replace item function calls, but thought it would be helpful to see some working code examples.

const helperSetDefaultListWidgetType = (itemName, defaultValue) =>
{
    const item = items.getItem(itemName);
    item.replaceMetadata('listWidget', defaultValue);
}

const helperSetWidgetOrder = (itemName, position) => {
    const item = items.getItem(itemName);
    item.replaceMetadata('widgetOrder', position);
}