Javascript: how to dynamically add items to groups?

How do I add an item to a group (or remove it) using Javascript? (I managed to add a group name to an item but the group is not be updated with this member)

I am trying to dynamically add and remove items to a group. The group should only contain items for which a timer has timed out. I tried to follow the advise in

https://community.openhab.org/t/how-to-access-oh-rest-api-from-javascript/119204/2?u=gonzo666

using

var triggeritem = ir.getItem(event.itemName);
triggeritem.addGroupName("gDoorOverdue");

and

triggeritem.removeGroupName("gDoorOverdue");

This seems to add or remove the group name gDoorOverdue to the item as

triggeritem.groupNames

shows. However, the group itself does not show the items that I add this way. It seems I am encountering the problem of inconsistency if I do not add the item also to the group as is mentioned in this post:

https://community.openhab.org/t/solved-datetime-items-compare-against-fixed-datetimes/58615/28?u=gonzo666

The commands given in the post do not work for me. I can get a list of group members with “group.members” but I have not found a way to add or remove an item to/from a group using Javascript.

How can I achieve this in JS?

1 Like

It seems I figured it out. But there is still a little problem with updating the status of the group item.

I need to do both of the following to keep the group membership information on the item and the group item in sync:

  • add/remove group name to/from item (uses itemName as parameter)
  • add/remove item to/from group item (uses item as parameter)

At first I tried to use “itemName” also for adding an item to the group-item - and of course failed. I found the relevant information on how to add or remove an item to a group item here:

https://next.openhab.org/javadoc/latest/org/openhab/core/items/groupitem

This code is now working for me:
(Rule is created in UI of OH3 running on Docker/RPi4)

"use strict";
var logger = Java.type("org.slf4j.LoggerFactory").getLogger("org.openhab.rule." + ctx.ruleUID);

var dynamicGroup = ir.getItem("gDoorOverdue");
var triggeritem = ir.getItem(event.itemName);

if (event.itemState == "ON") {
  triggeritem.addGroupName("gDoorOverdue");
  dynamicGroup.addMember(triggeritem);
  dynamicGroup.setState(ON);
  
} else {
  triggeritem.removeGroupName("gDoorOverdue");
  dynamicGroup.removeMember(triggeritem);
}

Trigger for the rule is a group with two switch items as members.

There is still one problem though: the status of the group is not properly updated.

Starting point: both switches are “OFF” and the group status is “OFF”.
Group item is defined in .items-file as: Group:Switch:OR(ON,OFF) gDoorOverdue “[%s]”

If I switch on one, or both, of the switch-items the status of the group remains off. If both switches are on and one switches off then the group changes to “ON” which is correct since the second switch is still on. For some reason the status update is not triggered for the group-item when adding a member to a group. But it is triggered when it is removed. This is what I can see from the log:

[hab.event.GroupItemStateChangedEvent] - Item 'gDoorOverdue' changed from ON to OFF through Test_Timer_Trigger_1

There is no corresponding entry when switching a group member item to “ON”. For now I fixed it by simply adding “dynamicGroup.setState(ON)” whenever a switch triggers “ON”. But I would like to understand what is going on and why that it does not work automatically.

There is no event generated when group membership changes. So there is nothing to tell the aggregation function that it has changed membership to take into consideration. Also note that the way rule triggers are created the rule won’t assist adjust to the change in membership either.

Tl;dr is dynamically changing groups isn’t going to do a whole lot for you beyond keeping some items together.

There is plenty of value in tweaking the groups if your rules use them and you need the group members to change say on a schedule… It is disturbing though that the two methods are both required to complete the task rather than each method updating both the Group and the Item… it leaves the nasty potential for a mismatch in the config.