Fire Rule when groupmember receives command

Hi folks,
Is there an option to fire a rule, when any of a groups member receives a command?
Let me explain why I want to do this:
I have a few dummy Switchitems for every harmony activity I have, in order to expose them to Alexa:

// Groups
Group:Switch gHarmonyDummy

// Harmony Hub
String harmonyLivingActivity_current "Harmony current activity" (gHistory) {channel="harmonyhub:hub:HarmonyHub:currentActivity"}

// Dummy Items for Alexa
Switch dummyLivingActivity_kodi "Heimkino" (gHarmonyDummy, gHistory) ["Switchable"]
// Switch dummyLivingActivity_kodi_alt1 "Media Center" ["Switchable"]
Switch dummyLivingActivity_ps4 "Playstation" (gHarmonyDummy, gHistory) ["Switchable"]

In order to map them back and forth with the “real” current activity (harmonyLivingActivity_current) I created following rules:

import org.eclipse.xtext.xbase.lib.Functions

// Functions
// dummy2harmony
val Functions$Function3<GenericItem, GenericItem, String, Boolean> dummy2harmony = [ harmonyActivity_current, dummyItem, activityName |

	var desiredActivity = ( if (dummyItem.state == ON) activityName else "PowerOff" )
    logDebug("HarmonyActivityControl", "dummy2harmony: " + + " received command, calling harmony hub witch activity " + desiredActivity)
    true //not sure if neccesary

rule "Harmony Kodi start or stop"
	Item dummyLivingActivity_kodi received command
	dummy2harmony.apply(harmonyLivingActivity_current, dummyLivingActivity_kodi, "Kodi")

rule "Harmony Playstation start or stop"
	Item dummyLivingActivity_ps4 received command
	dummy2harmony.apply(harmonyLivingActivity_current, dummyLivingActivity_ps4, "Playstation")

// harmony2dummy

rule "Harmony state mapping to OpenHAB"
	Item harmonyLivingActivity_current received update
	var currentActivity = harmonyLivingActivity_current.state
	logDebug("Harmony", "Harmony activity update detected, now: " + currentActivity)
	postUpdate(dummyLivingActivity_kodi,  if (currentActivity == "Kodi") ON else OFF )
	postUpdate(dummyLivingActivity_ps4,  if (currentActivity == "Playstation") ON else OFF )

That works like a charm btw.
But I’d rather like to group both dummy2harmony rules into just one rule, without the need to list every activity as a trigger, but just the group. The goal is to be able to just add an Item to the group and getting the functionality of the rule without the need change anything at all in the rules file.
The first step to achieve this is to trigger the rule depending on the group. But if I do so with received update , the rule will be fired because of the harmony2dummy rule, whenever I change the activity from outside of openHAB.

Any Ideas to solve this or should I just live with it?

The short answer is no. You CAN trigger the rule when the Group receives an update. Anytime you send a command to a member of the Group the Group’s state will be updated.

However, your rule will be triggered multiple times from that one command because of the way that Group states are calculated. :frowning:

But even if the rule were only triggered once, you are still faced with the problem of determining which Switch was the one that caused the rule to trigger in the first place. :sob:

All is not lost.

See the Working with Groups in Rules Design Pattern and pay attention to the parts that sort the members of the Group by lastUpdate. Assuming your switches don’t occur too close together you can figure out which Item triggered the rule based on its lastUpdate time (i.e. the most recently updated Item will be the one that received the command.

Unfortunately, your options for dealing with the fact that the rule will trigger multiple times per command are a little more limited. If you never need to support sending the same command twice or more in a row, you can add a check in your lambda and only send the command to harmonyActivity_current if desiredActivity is different from what it already is set to. If that won’t work, you will need to trigger the rule based on the individual Items rather than the Group. You can still use the lastUpdate trick above to centralize the logic into one rule so all you have to do when adding a new Item is to add a new trigger rather than a whole new rule.

Thanks for the quick answer!
Thats a bummer… I’d be keen to try your suggestions and already had a good look into the design pattern but all of that still would not solve the problem with the sendCommand / postUpdate, or am I missing something?

The Problem is, that the lower part of my rules file maps the activity of the harmony to openHAB (the dummy Items), when I change the activity from the remote directly using postUpdate. Thus making sure, that the top part isn’t executed.
If I would change the trigger to received update it would fire the rule.
So in addition to what you pointed out, I’d have to find a way to determine whether the last updated item in the group was changed by a command or by postUpdate.
Is there any information in the persistence about how an Item was changed?

Unfortunately not.

There might be something you can do with the Assocaited Items or Manual Trigger Detection to come up with an alternative overall approach. I’m not sure though. I’d need to see your working rules. And even if they were applicable, I’m not sure it would be worth the effort. It might end up using fewer lines of code but those lines of code might be brittle or overly complex.

You might be able to make something like this work using the JSR223 Rules. The rules triggers provide more information, including I think the actual Item that triggered the Item. I still think you will have to list each Item individually as triggers but the features of Jython or JavaScript might give you more room to come up with something better.

Beyond that, all I can recommend is to post your full set of rules and Items (assuming the above isn’t it) and I can see if I can some up with something clever that you can do in the Rules DSL.

From looking at the code posted I’m really thinking that what you have now may be the most straight forward implementation, as tedious as it may feel.

I think I will fiddle around with this! I enjoy python in general and from the quick research it really seems like theres some serious flexibility added to this approach.
Just have to find a good starting point since nearly all the examples provided here are for OH1 and from what I’ve seen there have been some major changes since (I just started to work with OH recently and never used OH1 at all).

Anyways - thanks for the help and if/when I come up with something clever I will post my approach here!