# Syntax on Merging Similar Rules

**URL:** <https://community.openhab.org/t/syntax-on-merging-similar-rules/39129>\
**Category:** Scripts & Rules\
**Created:** [January 22, 2018, 10:13pm UTC](https://community.openhab.org/t/syntax-on-merging-similar-rules/39129 "2018-01-22T22:13:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![brownjl](https://community.openhab.org/letter_avatar_proxy/v4/letter/b/ecc23a/32.png) [@brownjl](https://community.openhab.org/u/brownjl)\
**Post date:** [January 22, 2018, 10:13pm UTC](https://community.openhab.org/t/syntax-on-merging-similar-rules/39129/1 "2018-01-22T22:13:37Z")

</div>

Hello,

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.

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

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

```

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?

Thanks

James

---

<div class="post-metadata">

**Author:** ![luckymallari](https://community.openhab.org/user_avatar/community.openhab.org/luckymallari/32/15259_2.png) [@luckymallari](https://community.openhab.org/u/luckymallari)\
**Post date:** [January 22, 2018, 10:30pm UTC](https://community.openhab.org/t/syntax-on-merging-similar-rules/39129/2 "2018-01-22T22:30:26Z")

</div>

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?](https://community.openhab.org/t/which-item-updated-my-group-item/25273)

---

<div class="post-metadata">

**Author:** ![job](https://community.openhab.org/user_avatar/community.openhab.org/job/32/37738_2.png) [@job](https://community.openhab.org/u/job)\
**Post date:** [January 22, 2018, 10:38pm UTC](https://community.openhab.org/t/syntax-on-merging-similar-rules/39129/3 "2018-01-22T22:38:15Z")

</div>

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”, “powermap.map”, triggeringItem.name)) 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.

```php
rule "Power_Change"
when 
    Item LivingRoomTV_Power_Raw changed or
    Item ServerPower_Raw changed
then
        val notraw = gNotRawPowerGroup.members.filter[i|i.name==String::format("%s_NOT_RAW",triggeringItem.name)].last
        filter.apply(triggeringItem, notraw, x)
end

```

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [January 22, 2018, 10:39pm UTC](https://community.openhab.org/t/syntax-on-merging-similar-rules/39129/4 "2018-01-22T22:39:07Z")

</div>

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.
