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@] >[OFF:POST:http://admin:admin@]" }
Switch Relay2 "Relay2" (g_MKTronicRelayAll) { http=">[ON:POST:http://admin:admin@] >[OFF:POST:http://admin:admin@]" }
Switch Relay3 "Relay3" (g_MKTronicRelayAll) { http=">[ON:POST:http://admin:admin@] >[OFF:POST:http://admin:admin@]" }
Switch Relay4 "Relay4" (g_MKTronicRelayAll) { http=">[ON:POST:http://admin:admin@] >[OFF:POST:http://admin:admin@]" }
Switch Relay5 "Relay5" (g_MKTronicRelayAll) { http=">[ON:POST:http://admin:admin@] >[OFF:POST:http://admin:admin@]" }
Switch Relay6 "Relay6" (g_MKTronicRelayAll) { http=">[ON:POST:http://admin:admin@] >[OFF:POST:http://admin:admin@]" }
Switch Relay7 "Relay7" (g_MKTronicRelayAll) { http=">[ON:POST:http://admin:admin@] >[OFF:POST:http://admin:admin@]" }
Switch Relay8 "Relay8" (g_MKTronicRelayAll) { http=">[ON:POST:http://admin:admin@] >[OFF:POST:http://admin:admin@]" }

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"
    Item Relay1Status changed
       switch (Relay1Status.state) {
           case 0: { Relay1.postUpdate(OFF) }
           case 1: { Relay1.postUpdate(ON) }

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"
    Member of g_MKTronicRelayStatusAll changed
       switch (triggeringItem.state) {
           case 0: { Relayx.postUpdate(OFF) }
           case 1: { Relayx.postUpdate(ON) }

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"
    Member of g_MKTronicRelayStatusAll changed
    var relayName ="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) }

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

1 Like

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