Getting members of group with file based ecmascript-2021

hello I would like to know the syntax to know the members who compose a group, I can only do it in “rules dsl” and with “blockly”, could you help me?

here is blockly script:

var logger = Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.rule.' + ctx.ruleUID);
logger.info(Java.from(itemRegistry.getItem('House').members));

with files based in automation folder i have always error
thanks a lot

Always refer to the reference documentation when trying to know how to do something in a language you are not familiar with. JavaScript Scripting - Automation | openHAB

Blockly renders to ECMAScript 5.1 which isn’t going to be much help. But from the reference docs above you’ll see that the ECMAScript 2021 equivalent would be something like

console.log(items.getItem('House').members);

here is what i get :

 [

  {

    "rawItem": {},

    "history": {

      "rawItem": {}

    },

    "semantics": {

      "rawItem": {}

    }

  }

]

how a can get label of name ?
thanks for your help

I can only repeat what Rich said: Always refer to the reference documentation, before asking on the forum, that saves us all some time.

The reference docs at JavaScript Scripting - Automation | openHAB say:

Calling getItem(...) returns an Item object with the following properties:

  • Item : object
    • .type ⇒ String
    • .name ⇒ String
    • .label ⇒ String
    • .history ⇒ ItemHistory
    • .state ⇒ String
    • .rawState ⇒ HostState
    • .members ⇒ Array.<Item>
    • .descendents ⇒ Array.<Item>
    • .isUninitialized ⇒ Boolean
    • .groupNames ⇒ Array.<String>
    • .tags ⇒ Array.<String>
    • .getMetadataValue(namespace) ⇒ String
    • .updateMetadataValue(namespace, value) ⇒ String
    • .upsertMetadataValue(namespace, value) ⇒ Boolean
    • .updateMetadataValues(namespaceToValues)
    • .sendCommand(value)
    • .sendCommandIfDifferent(value) ⇒ Boolean
    • .postUpdate(value)
    • .addGroups(…groupNamesOrItems)
    • .removeGroups(…groupNamesOrItems)
    • .addTags(…tagNames)
    • .removeTags(…tagNames)

This means:
The members of your House group are returned as an Array (you can also see this in your log message with the brackets: []).
To log the label of each group member (as an example), which is an element of the Array, there are some possibilities:

// the array with the members of the „House“ group
var members = items.getItem('House').members;

// this logs the label of each member to a new line
members.forEach((item) => console.info(item.label));

// this uses the map function of Array to generate a new array with the names of the group members
// and joins the names into a single string
console.info(members.map((item) => item.name).join(', '));
1 Like

Thanks a lot this is more clear to me now , thanks !

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.