Obenhab3 testing metadata exist

I’ would have a simple question on using metadata.

  • How to test a metadata exists?
  • How to test the metadata has a configuration item?

I’ have tried the usual approach;

Testing metadata exist:
labelText: '=(loop.item.metadata.stateDescription) ? loop.item.metadata.stateDescription.value : "xyz"'
or
labelText: '=(loop.item.metadata.stateDescription == " ") ? loop.item.metadata.stateDescription.value : "xyz"'

but none of them seams to work.

any hint is appreciated

thanks Ernst

This is in a UI widget I assume? I think you test for undefined.

=(loop.item.metadata.stateDesceiption !== undefined) ? ...

Thanks for the replay.

Yes, it is an UI widget.

I’ tried the statment

typeText: ‘=(loop.item.metadata.stateDescription !== undefined) ? loop.item.metadata.stateDescription.value : “xyz”’

this works if the metadata is defined, but not for the if there is no definition.

In case there is no definition the the following text is inserted:
TypeError: undefined has no properties

If you are only fetching the stateDescription metadata then any item that does not have that particular metadata will also have undefined metadata. You have to test for that (or that and stateDescription if you have fetched more than one metadata namespace):

=(loop.item.metadata !== undefined && loop.item.metadata.stateDescription !== undefined)?(...

thank you, this works for me.

for my understanding:

the first therm: loop.item.metadata !== undefined
is to test whether or not the metadata returned has any content at all

the second therm loop.item.metadata.stateDescription !== undefined
will check if the requested metadata namespace exist. This will return an error if no metadata namespace was returned.

is this correct?
thanks Ernst

Yes, that’s correct. The second one returns an error if there is no metadata at all, because expressions are allowed to return undefined as a value but you cannot ask the parser to perform any actions on an undefined value (how would it know what action to perform if there is no context from a value type?). So if there’s any possibility that a value you are trying to work with can be undefined, you always want to test that it exists before you try and do anything with it.