Adding members to group in Jython (JSR223)

I have recently started working on understanding the use of Jython with OpenHAB and I’m trying to streamline some of my rules on a test system. There are a few things I’m a little stuck on though, one of them being dynamic groups in Jython.

I have rules in OH that add and remove members from groups depending on the state of lights and other items and I’d like to replicate this in Jython if possible. So I’m looking to find out how to add and remove members from groups dynamically.

In the rules DSL I’d do something like this:
AllFloorsLightsOn.addMember(item)

But that doesn’t seem to work in Jython. Is there a way to accomplish this currently?

As a side note I’ve been doing a lot of digging and while there’s a good set of information on getting started (hats off to the guys who put all of this together), I’m trying to find documentation on all of the methods that are available to the core functionality. Is there somewhere that core functions (like the item registry, methods available to groups, etc) is documented? If not is there somewhere I can see the source code for the OH integration into Jython? I can parse the code, I don’t have a problem with that, just need to know where to look to find the integrations.

Thanks in advance for any help.

There is usually a better way than manipulating groups, but you asked :slightly_smiling_face:.

Add the methods on the GroupItem…

itemRegistry.getItem("AllFloorsLightsOn").addMember(item)
itemRegistry.getItem("AllFloorsLightsOn").removeMember(item)

It sounds like you are asking for the openHAB-core code. At some point, a good place will be found for the JavaDoc too.

It is really easy to test things by just putting them into a script. I also have a screen ssh’d into my OH server that is tailing the log file so that I can see the logs from the test script. Actually, I have 6 screen sessions tailing 5 log files (I have multiple appenders). The dir built-in (mentioned in the HL docs) is one of the special gems in Python that you don’t find in other languages and is a tool that every scripter should take advantage of, even if they choose to use another language for their rules!

For example, to answer your question about adding/removing group members, I had an idea of where to look, so I ran the script below to view the methods of a GroupItem…

# pylint: disable=anomalous-backslash-in-string, invalid-encoded-data, wrong-import-position
from core.log import logging, LOG_PREFIX#, log_traceback
LOG = logging.getLogger("{}.TEST".format(LOG_PREFIX))

import sys
sys.version_info.micro, sys.version_info.releaselevel))
LOG.warn("Jython version [{}]".format(sys.version))

import platform
LOG.warn("Java version [{}]".format(platform.platform()))

#from core.rules import rule
#from core.triggers import when
###########################################################################

LOG.warn("{}".format(dir(ir.getItem("gTest"))))

###########################################################################

There are also addGroupName and removeGroupName methods on the Items.

I actually did put it in a script and it threw an exception. I’ve been using logging and trying to test things one piece at a time, and I did test by trying that method but I will go back and test again, maybe I missed a typo.

I tail the logs all the time, it’s the best resource for testing my code and I use logging all over the place. I’ll check out the other logger method you mentioned though.

I’m slowly going through my code and finding new and better ways to accomplish what I’m currently doing and I’m thinking I might abandon adding and removing things from the groups in the near future, preferably to use collections and the like but I’m still getting used to Python as it’s a language I’ve never used. I’m actually from the industrial automation industry so I’m used to a whole different set of languages.

Out of curiosity, what methods might you use as an alternative to manipulating groups? I’m thinking of using collections instead but like I said, I’m migrating slowly.

Note that I used that method in the example on a group. It is not available on non-GroupItems. As i mentioned at the bottom, there are also methods for regular Items… add GroupName and removeGroupName.

Groups are great storage containers, where the members can be filtered to get Items with a particular state, tags, metadata, etc. The only place I move Items around in groups is in the OpenWeatherMap script, in order to use group aggregation functions to calculate the daily forecasts. In all other scenarios, I keep my group members static. What is your use case where you need to change group membership?

I’m toying around with some new ideas.

As of now I have dynamic groups for each room that keep track of what lights are on. So basically if I have a rule that runs that monitors the lights in the room and if a light changes it checks to see if any lights are on, if they are they get added to the group. I can then add this group to a sitemap so I can get a list of everything that’s on and turn off anything I might not want on.

This has the added benefit of simplifying my “All Off” rules. When I leave the house I just run through those groups and turn off anything in the group.

Some of those features I can implement using collections in the Python language so I’m toying with going that direction. But I’m also thinking of implementing state machines so I’m debating at what level to do that.

I’m thinking of a state machine at the room level but then I can’t really control the lights in the room from an app or google home without it falling out of sync with the state machine and I’m trying to figure out how to get around that at the moment.

You can turn off all lights in a group by just sending OFF to the group, so I’m not sure why you’d need to manipulate the grouip members. In Basic UI, you can add each of your lights and use visibility to hide all of the ones that are OFF. That all seems old-school though. Have you seen this?

One thing I don’t mention is that the Habpanel panel is interactive, so we can turn on/off the lights by touching a dashboard/tablet/phone/computer. You can also use Basic UI.

I’m familiar with how the groups work, the reason I chose to do things this way was due to the fact that I’m using Insteon for parts of my lighting. Insteon has this terrible issue where sending commands over the network will very easily bog it down and things stop responding. So that never worked right for me. With the new binding that issue may have been resolved, so maybe my design can change a little bit without having to make compromises and adding delays.

Having said that I’m going to continue to read the information you’ve posted here as well as all the information on design principles I’ve found on this forum and I’ll go from there. I’m looking forward to moving things to Jython and simplifying many of my rules to reduce complexity.

1 Like