Read stateDescription options

Dear community

I want to read the label out of the stateDescription of a selection item.
This are the options in the stateDescription of the item:

{“pattern”:“%s”,“readOnly”:false,“options”:[{“value”:“IFL218GsCqgPhf1”,“label”:“Ambiente Blau”},{“value”:“TnheiSwPueTvpNn”,“label”:“Ambiente Grün”},{“value”:“WnH4JlYFzmw6ASC”,“label”:“Ambiente Kaltweiss”},{“value”:“FbO0NX3x–GhVZn”,“label”:“Ambiente Orange”},{“value”:“qLLUjvqa5vEL33e”,“label”:“Ambiente Warmweiss”},{“value”:“iPxxSi6YEmdWoCs”,“label”:“Ambiente gedimmt”},{“value”:“D79AqWq6zoTeKc2”,“label”:“Frühlingsblüten”},{“value”:“f0HdwQd9S5ANvmf”,“label”:“Nordlichter”},{“value”:“CEv0LDFzsKOMmsh”,“label”:“Reserve 2”},{“value”:“grxV4mDivP3u94W”,“label”:“Reserve 3”},{“value”:“pSBzMyzKVSftfqZ”,“label”:“Sonnenuntergang Savanne”},{“value”:“YHAC-QItz3sLFaF”,“label”:“Tropendämmerung”}]}

I read these options as follows: {{getItem(config.scenes_1).stateDescription}}

But now I want to read and display the “active” label.
Unfortunately, the command “{{getItem(config.scenes_1).stateDescription.options.label}}” doesn’t work.

How can I read and display the label of the active scene?

Kind regards
Carlo Caprez

getItem(config.scenes_1).stateDescription.options returns an array of objects (everything between the [...]), not a single object. You first have to find which element in the array is the one with the matching value using something like the findIndex method:

Once you have that element’s index, then you can get the label of that individual element with

getItem(config.scenes_1).stateDescription.options[element_index].label

Hi Justin

Many thanks for your answer.
You’re right. If I specify an index for options (.stateDescription.options[0].label) I get a label back.

That means without javascript it won’t be possible?
Or is there also a possibility in openhab?

Justin and I are assuming you mean to do this in a MainUI widget. Are you trying to get this from somewhere else, like a rule perhaps? If in a rule, which language?

No, I’m still working with the habpanel.
I was hoping to be able to read the label directly from the json string.

Can I write the javascript directly into the widget?

HABPanel uses angularjs so you can look up examples of that. I believe that angularjs should be able to handle js array methods.