Howto get stateDescription from item in a rule

Hello,
when I use ‘Developer Tools/Api Explorer’ for an item it lists ‘stateDescription’ (s. below).

{
“link”: “http://192.168.178.177:8080/rest/items/Manfreds_Music_Current_Media_Id”,
“state”: “0”,
“stateDescription”: {
“pattern”: “%s”,
“readOnly”: false,
“options”: [
{
“value”: “coA07B6A9DDA0027F1”,
“label”: “Album Artist”
},
{
“value”: “coD60E6B3D9901865F”,
“label”: “Artist”
},
{
“value”: “coC2C844CDDBB1CFC8”,
“label”: “Album”
},
}
]
},
“commandDescription”: {
“commandOptions”: [
{
“command”: “coA07B6A9DDA0027F1”,
“label”: “Album Artist”
},
{
“command”: “coD60E6B3D9901865F”,
“label”: “Artist”
},
{
“command”: “coC2C844CDDBB1CFC8”,
“label”: “Album”
},

],
“groupNames”: [
“Manfreds_Music”
]
}

When I try to read these information in a rule, it returns ‘null’.

var vSuchBegriff, vSuchErgebnis, vitemData;

function getItemMetaConfigValue(itemName, namespace, prop) {
  console.info('itemName: ' + itemName);
  console.info('namespace: ' + namespace);
  if (items.metadata.getMetadata(itemName, namespace) === null) {
    console.info('namespace leer: ');
     return '';
  };
  let props = prop.split('.');
  let value = items.metadata.getMetadata(itemName, namespace).configuration;
  console.info('value: ');
  console.info(value);
  props.forEach(property => {
    value = value[property];
    console.info('value: ');
    console.info(value);
  });
  return value;
}

vSuchErgebnis = items.getItem('Manfreds_Music_Current_Media_Id').state.toString();
console.info('vSuchErgebnis: ');
console.info(vSuchErgebnis);
console.info(vSuchErgebnis);
vitemData = getItemMetaConfigValue('Manfreds_Music_Current_Media_Id', 'stateDescription', 'options');
console.info('vitemData');
console.info(vitemData);

What is wrong in my code?
Thanks
Manfred

Found a solution by using the REST-Api.

const itemName = 'Manfreds_Music_Current_Media_Id';
const url = `http://192.168.178.177:8080/rest/items/${itemName}`;
console.info('url: ' + url);
try {
  const json = actions.HTTP.sendHttpGetRequest(url);
  const data = JSON.parse(json);

  if (data.stateDescription && data.stateDescription.options) {
    const options = data.stateDescription.options;

    console.log(`Gefundene Optionen für ${itemName}:`);
    options.forEach(opt => {
      console.info(`  Wert: ${opt.value}, Label: ${opt.label}`);
    });

    // Optional: Ergebnisse in ein anderes Item schreiben
    // items.getItem('Some_Target_Item').postUpdate(JSON.stringify(options));

  } else {
    console.warn(`Keine Optionen gefunden für ${itemName}.`);
  }
} catch (error) {
  console.error("Fehler beim Abrufen der REST-Daten:", error);
}

You shouldn’t need to get anything ever from REST API when your code is already running on the core JVM. The information is already available within core.

The jsscripting library doesn’t provide convenience methods for it but it’s still accessible through:

JS:

items.Manfreds_Music_Current_Media_Id.rawItem.getStateDescription()

JRuby:

Manfreds_Music_Current_Media_Id.state_description

Both would return a StateDescription object.

2 Likes

Thanks, I tested this, but used the wrong syntax. Now it works,