Get Members of a Group within a Rule

I am trying to get a list of Items belonging to a group so I can perform different actions on each Item in the group depending on its associated metadata. Basically: FOR group GET list of Items. FOR each Item (Get Item’s metadata, DO something in accordance with metadata). The problem I’m having is that if I try using GroupItem(group).getMembers(); it returns an empty Object. If I use ir.getItem(group).getMembers(); it returns an Object containing all the members of the group along with its associated data. However, so far I have been unable to access the object eg Object.keys throws an exception TypeError. So my question is how do I access the information contained in this object? Or otherwise get the names of Items contained within a group?

Where I’m having the problem is the code after the comment:
//
//
// Following Code tries to get the Items in a group
//
//
contained within the following code:

logger.info("JS Test Rule Called");
var MetadataPredicates = Java.type("org.openhab.core.items.MetadataPredicates");

var SemanticModelNamespace = "semantics";            // Metadata Namespace containing semantic data
var LocationOfItem = "hasLocation";                  // Key name for "has Location"
var MotionControlParametersNamespace = "OccupancyParameters";
var OccupiedDelay = "OccupiedDelay";
var UnoccupiedDelay = "UnoccupiedDelay";
var Trigger = event.itemName;                        // Name of event that triggered the rule
var RoomMetadataKeys = MetadataRegistry.get(new MetadataKey(SemanticModelNamespace, Trigger));
if (LogingEnabled)logger.info("RoomMetadataKeys are: " +RoomMetadataKeys);

var Room = RoomMetadataKeys.getConfiguration()[LocationOfItem];
var RoomMetadata = MetadataRegistry.get(new MetadataKey(MotionControlParametersNamespace, Room));
if (LogingEnabled)logger.info("Trigger Event was: " +Trigger +"  Room is: " +Room +"Metadata is: " +RoomMetadata);

var KeyPairs = RoomMetadata.getConfiguration();
if (LogingEnabled)logger.info(MotionControlParametersNamespace +" Namespace Key Pairs(Type: " +typeof KeyPairs +") are: " +KeyPairs);
var Value = RoomMetadata.getValue();
if (LogingEnabled)logger.info(MotionControlParametersNamespace +" Namespace Value is: " +Value +"\n\n");
//
//
// Following Code  tries to get the Items in a group
//
//
var ItemsInGroup = new GroupItem(Room).getMembers();
if (LogingEnabled)logger.info("ItemsInGroup(Method 1) type is: " +typeof ItemsInGroup);
if (LogingEnabled)logger.info("Group members of " +Room +" (Method 1) are: "  +ItemsInGroup +"\n\n");

ItemsInGroup = ir.getItem(Room).getMembers();
if (LogingEnabled)logger.info("ItemsInGroup(Method 2) type is: " +typeof ItemsInGroup);
if (LogingEnabled)logger.info("Group members of " +Room +" (Method 2) are: "  +ItemsInGroup +"\n\n");

if (LogingEnabled)logger.info("Ojbect Keys are: " +Object.keys(ItemsInGroup));

and the log output when I run the code is:

2021-01-23 19:44:00.999 [INFO ] [org.openhab.rule.1ac199dff6         ] - JS Test Rule Called

2021-01-23 19:44:01.121 [INFO ] [org.openhab.rule.1ac199dff6         ] - RoomMetadataKeys are: Metadata [key=semantics:PIR01_MotionIntrusion, value=Point, configuration=[hasLocation=gStudy]]

2021-01-23 19:44:01.222 [INFO ] [org.openhab.rule.1ac199dff6         ] - Trigger Event was: PIR01_MotionIntrusion  Room is: gStudyMetadata is: Metadata [key=OccupancyParameters:gStudy, value=Test Namespace, configuration=[OccupiedDelay=0.0, UnoccupiedDelay=4.0]]

2021-01-23 19:44:01.278 [INFO ] [org.openhab.rule.1ac199dff6         ] - OccupancyParameters Namespace Key Pairs(Type: object) are: {OccupiedDelay=0.0, UnoccupiedDelay=4.0}

2021-01-23 19:44:01.340 [INFO ] [org.openhab.rule.1ac199dff6         ] - OccupancyParameters Namespace Value is: Test Namespace

2021-01-23 19:44:01.414 [INFO ] [org.openhab.rule.1ac199dff6         ] - ItemsInGroup(Method 1) type is: object

2021-01-23 19:44:01.453 [INFO ] [org.openhab.rule.1ac199dff6         ] - Group members of gStudy (Method 1) are: []

2021-01-23 19:44:01.662 [INFO ] [org.openhab.rule.1ac199dff6         ] - ItemsInGroup(Method 2) type is: object

2021-01-23 19:44:01.745 [INFO ] [org.openhab.rule.1ac199dff6         ] - Group members of gStudy (Method 2) are: [PIR01_MotionIntrusion (Type=SwitchItem, State=OFF, Label=PIR 01 Motion Intrusion, Category=Motion, Tags=[Point], Groups=[gStudy]), ExtensionLead01_3_Switch (Type=SwitchItem, State=OFF, Label=Extension Lead 01 Socket 3, Category=poweroutlet_uk, Tags=[Point], Groups=[gStudy]), gStudyStatus (Type=StringItem, State=Unoccupied, Label=Occupancy Status, Category=status, Tags=[Point], Groups=[gStudy]), InLineSwitch01_Switch (Type=SwitchItem, State=OFF, Label=In Line Switch 01, Category=switch, Tags=[Point], Groups=[gStudy]), ExtensionLead01_1_Switch (Type=SwitchItem, State=OFF, Label=Extension Lead 01 Socket 1, Category=poweroutlet_uk, Tags=[Point], Groups=[gStudy, Test]), ExtensionLead01_2_Switch (Type=SwitchItem, State=OFF, Label=Extension Lead 01 Socket 2, Category=poweroutlet_uk, Tags=[Point], Groups=[gStudy, Test]), Desk (Type=GroupItem, Members=0, State=NULL, Label=Desk, Category=office, Tags=[Location], Groups=[gStudy])]

2021-01-23 19:44:01.808 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID '1ac199dff6' failed: TypeError: [PIR01_MotionIntrusion (Type=SwitchItem, State=OFF, Label=PIR 01 Motion Intrusion, Category=Motion, Tags=[Point], Groups=[gStudy]), ExtensionLead01_3_Switch (Type=SwitchItem, State=OFF, Label=Extension Lead 01 Socket 3, Category=poweroutlet_uk, Tags=[Point], Groups=[gStudy]), gStudyStatus (Type=StringItem, State=Unoccupied, Label=Occupancy Status, Category=status, Tags=[Point], Groups=[gStudy]), InLineSwitch01_Switch (Type=SwitchItem, State=OFF, Label=In Line Switch 01, Category=switch, Tags=[Point], Groups=[gStudy]), ExtensionLead01_1_Switch (Type=SwitchItem, State=OFF, Label=Extension Lead 01 Socket 1, Category=poweroutlet_uk, Tags=[Point], Groups=[gStudy, Test]), ExtensionLead01_2_Switch (Type=SwitchItem, State=OFF, Label=Extension Lead 01 Socket 2, Category=poweroutlet_uk, Tags=[Point], Groups=[gStudy, Test]), Desk (Type=GroupItem, Members=0, State=NULL, Label=Desk, Category=office, Tags=[Location], Groups=[gStudy])] is not an Object in <eval> at line number 110

Use ir.getItem(“bla”).getMembers().stream().foreach(function(item){});

@querdenker2k Cheers thats helped me move towards the next challenge :grinning: :joy: