Using part of an item's name to reference another item in a rule

I have an 8 relays board and they each have a relay status:

Switch Relay1 "Relay1" (g_MKTronicRelayAll) { http=">[ON:POST:http://admin:admin@192.168.2.20/FF0101:ON] >[OFF:POST:http://admin:admin@192.168.2.20/FF0100:OFF]" }
Switch Relay2 "Relay2" (g_MKTronicRelayAll) { http=">[ON:POST:http://admin:admin@192.168.2.20/FF0201:ON] >[OFF:POST:http://admin:admin@192.168.2.20/FF0200:OFF]" }
Switch Relay3 "Relay3" (g_MKTronicRelayAll) { http=">[ON:POST:http://admin:admin@192.168.2.20/FF0301:ON] >[OFF:POST:http://admin:admin@192.168.2.20/FF0300:OFF]" }
Switch Relay4 "Relay4" (g_MKTronicRelayAll) { http=">[ON:POST:http://admin:admin@192.168.2.20/FF0401:ON] >[OFF:POST:http://admin:admin@192.168.2.20/FF0400:OFF]" }
Switch Relay5 "Relay5" (g_MKTronicRelayAll) { http=">[ON:POST:http://admin:admin@192.168.2.20/FF0501:ON] >[OFF:POST:http://admin:admin@192.168.2.20/FF0500:OFF]" }
Switch Relay6 "Relay6" (g_MKTronicRelayAll) { http=">[ON:POST:http://admin:admin@192.168.2.20/FF0601:ON] >[OFF:POST:http://admin:admin@192.168.2.20/FF0600:OFF]" }
Switch Relay7 "Relay7" (g_MKTronicRelayAll) { http=">[ON:POST:http://admin:admin@192.168.2.20/FF0701:ON] >[OFF:POST:http://admin:admin@192.168.2.20/FF0700:OFF]" }
Switch Relay8 "Relay8" (g_MKTronicRelayAll) { http=">[ON:POST:http://admin:admin@192.168.2.20/FF0801:ON] >[OFF:POST:http://admin:admin@192.168.2.20/FF0800:OFF]" }

String Relay1Status "Relay1 - Status" (g_MKTronicRelayStatusAll) { http="<[MKTronic:2000:REGEX(.*?<relay1>(.*?)</relay1>.*)]" }
String Relay2Status "Relay2 - Status" (g_MKTronicRelayStatusAll) { http="<[MKTronic:2000:REGEX(.*?<relay2>(.*?)</relay2>.*)]" }
String Relay3Status "Relay3 - Status" (g_MKTronicRelayStatusAll) { http="<[MKTronic:2000:REGEX(.*?<relay3>(.*?)</relay3>.*)]" }
String Relay4Status "Relay4 - Status" (g_MKTronicRelayStatusAll) { http="<[MKTronic:2000:REGEX(.*?<relay4>(.*?)</relay4>.*)]" }
String Relay5Status "Relay5 - Status" (g_MKTronicRelayStatusAll) { http="<[MKTronic:2000:REGEX(.*?<relay5>(.*?)</relay5>.*)]" }
String Relay6Status "Relay6 - Status" (g_MKTronicRelayStatusAll) { http="<[MKTronic:2000:REGEX(.*?<relay6>(.*?)</relay6>.*)]" }
String Relay7Status "Relay7 - Status" (g_MKTronicRelayStatusAll) { http="<[MKTronic:2000:REGEX(.*?<relay7>(.*?)</relay7>.*)]" }
String Relay8Status "Relay8 - Status" (g_MKTronicRelayStatusAll) { http="<[MKTronic:2000:REGEX(.*?<relay8>(.*?)</relay8>.*)]" }

Because the relays can be triggered manually from other physical hardware I created a rule that postupdates the actual RelayStatus to the Relay switch on HABPanel UI.

rule "React on Relay1 - Status (Relay1Status) change/update"
when
    Item Relay1Status changed
then
       switch (Relay1Status.state) {
           case 0: { Relay1.postUpdate(OFF) }
           case 1: { Relay1.postUpdate(ON) }
       }
end

This code is repeated 8 times (one for each Relay).

In order to simplify the code (make it generic) I created 2 groups: 1st for the relay command and 2nd for the relay status.

Group:Switch g_MKTronicRelayAll
Group:Switch g_MKTronicRelayStatusAll

Now my rule code is this:

rule "React on Relay1-Relay8 - Status (Relay1Status-Relay8Status) change/update"
when
    Member of g_MKTronicRelayStatusAll changed
then
       switch (triggeringItem.state) {
           case 0: { Relayx.postUpdate(OFF) }
           case 1: { Relayx.postUpdate(ON) }
       }
end

In the switch argument I placed the triggeringitem (added new to openhab 2.3).

The question is how do I reference Relay1-Relay8 (Relayx) each time according to the “triggeringitem”?
Can I extract part of the triggeringitem’s name Relayx from RelayxStatus and use it as an item name?

rule "React on Relay1-Relay8 - Status (Relay1Status-Relay8Status) change/update"
when
    Member of g_MKTronicRelayStatusAll changed
then
    var relayName = triggeringItem.name.replace("Status", "") // Removes Status fron the triggering item name to obtain the relay name
    logInfo("Relay Name: ", relayName  )
    switch (triggeringItem.state) {
        case 0: { postUpdate(relayName, OFF) }
        case 1: { postUpdate(relayName, ON) }
    }
end

Note that we are using the postUpdate ACTION instead of the METHOD
See:

1 Like

For a detailed discussion of Vincent’s recommendation see Design Pattern: Associated Items.