Union of two groups? Jython Rules for groups

I’m writing rules in Jython.

I’ve read through the DP for Groups (Thank you @rlkoshak) But I’m not sure how to solve this issue: I have two groups: SecurityGroup_Away and SecurityGroup_Home … Those groups contain sensors and contacts that I care about monitoring if I am Home and the other set if I am Away. There is overlap between the groups. Then I have the Rule:

@rule("Security Check", description="Security alarm for Away and Home setting")
@when("Member of SecurityGroup_Away changed")
@when("Member of SecurityGroup_Home changed")

The problem is: For a particular sensor, I will get a trigger for each Group that contains it, therefore get two triggers in some cases. One solution I was thinking is just to have a group that contains ALL possible sensors that I might care about and have the Rule trigger on changes to that group, then test to see which group that trigger is a member of. Then I can take actions for the Away or Home group.

Is there a better way to do this?

I guess the other obvious way is to simply split it into two rules, then check which mode I’m in… should have thought about the problem longer :slight_smile: before posting.

1 Like

Other ideas:

  • Set a timer for half a second or so for a given Item that has triggered the rule. When the rule triggers and a timer exists, exit. That will cause the rule to ignore the second trigger.

  • Just have one Group and dynamically adjust the membership of that Group when you change from home to away. However, this approach has a limitation that the Rule itself will have to be reloaded because a Member of trigger unrolls to a separate trigger for each member of the Group at load time. I’ve an openhab-rules-tools library that shows how to do that but it’s written for OH 2.5 and I don’t yet know if it will work for OH 2.x. I’ve not been able to update my Python libraries for 3.x yet.

  • You don’t say what the rule does. Maybe it can be constructed such that it doesn’t matter if it runs twice.

1 Like

Thanks those are good suggestions too … I hadn’t thought about dynamically adjusting the list, I’ll take a look at what you wrote.

Ultimately the rule sends a notification to my Telegram account and/or sets an in-house alarm … it was getting two back to back notifications that was undesired.

Sounds like a job for rate limiting. Unfortunately the Python version of that library still uses Joda DateTime and will need modifications to work in OH 3. I’ll happily accept PRs though. :wink: But I’d like to maintain OH 2.x compatibility which makes it a little trickier than just replacing Joda DateTime with ZonedDateTime.

I use that library for things like low battery alerts or “fill the humidifier” alerts so they only get sent once a day. But it supports times from milliseconds to days.

Bingo, there will be other cases where I will/might get multiple triggers for the same class events in some period of time.