Syntax on Merging Similar Rules


I have a large number of similar rules which wait for an item to change and then call a function passing the changed item ans a similar named item and an integer like this.

rule "LivingRoomTV_Power_Change"
when Item LivingRoomTV_Power_Raw changed then
        filter.apply(LivingRoomTV_Power_Raw, LivingRoomTV_Power,10)

rule "Server_Power_Change"
when Item Server_Power_Raw changed then
        filter.apply(Server_Power_Raw, Server_Power,5)

All the items I am checking if changed are in a single group. I can change there names too if needed. Is there anyway I can merge all these rules with some wildcards or something?



If they are in a group, I would create a rule based on the group change. Using persistence, you can determine which item caused that change. An example here: Which Item updated my Group Item?

Yes, since OH2.2 you can use the triggeringItem.

But you still need to think about the different values you want to use in your function (x). You could use a map based on the itemnames (x=transform(“MAP”, “”, perhaps, or simply static unbound items.

From the triggeringItem it is most simple to add some postfix, but you could also split the name of the triggeringItem. Your Item ServerPower could renamed here into ServerPower_Raw_NOT_RAW.

rule "Power_Change"
    Item LivingRoomTV_Power_Raw changed or
    Item ServerPower_Raw changed
        val  notraw = gNotRawPowerGroup.members.filter[i|"%s_NOT_RAW",].last
        filter.apply(triggeringItem, notraw, x)

Another alternative is to trigger the one Rule by all your Items and then the triggeringItem variable will be the Item that caused the Rule to trigger.

This eliminates the dependency on persistence and timing.

There is work underway to be able to identify the Item that caused a Rule to trigger when using a Group to trigger the Rule, but it is not yet merged.