How to generically catch channel triggers?

I’ve bought a set of shelly devices to control external security lights (about 15 devices in total).

I have momentary switches that will manually turn these lights on, I want to capture the event of presses on those buttons. The devices haven’t actually arrived yet, so I’m doing some preliminary coding ahead of them arriving and me (actually the electrician) installing them.

My reading of the documentation tells me that button presses are captured like this:

rule "Button-1 SHORT_PRESSED"
when
    Channel "shelly:shellybutton1:d8f15bXXXXXX:status#button" triggered SHORT_PRESSED

I’ll have 15 devices, and I believe I can write a generic handler that would handle button presses on all devices rather than duplicating the same code over and over.

However, I don’t see any way to generically receive triggers, for example:

rule "Button-1 SHORT_PRESSED"
when
    Channel "shelly:shellybutton1:*:status#button" triggered SHORT_PRESSED

Or some way to capture a trigger on an item that’s associated with the channel, e.g.:

Group ShellyButtons
String sLoungeStatus (ShellyButtons) { {channel="shelly:shellybutton1:d8f15bXXXXXX:status#button"}
String sDiningStatus (ShellyButtons) { {channel="shelly:shellybutton1:d8f15bXXXXXX:status#button"}

rule "Button-1 SHORT_PRESSED"
when
    Group ShellyButtons triggered SHORT_PRESSED

Is there some method to do this generically, or do I just need to list all the channels individually? It’s not the end of the world, I won’t change how many controllers I have all that often, it just seems untidy.

I think the normal way is to use a group as in your second example.
But you have to use
Member of ShellyButtons triggered SHORT_PRESSED
Then it should work, just try. The items you have to define individually.

In Rules DSL? No, you cannot group Channels/Channel triggers, etc. You have to list them all individually.

There is some advanced stuff you can do in other languages to trigger on any event that occurs on the event bus and then filter down to just those you care about, but that is not generally supported and requires digging down into the raw openHAB JSR223 APIs.

But for an example in a UI rule, see Thing Status Reporting [4.0.0.0;4.9.9.9] which uses a generic trigger to listen for all Thing status change events to trigger the rule. Again, stuff like that is no possible in Rules DSL.

You cannot link Event Channels to Items directly. So you’d have to write rules to convert the event to an Item update which is even worse than the problem you are trying to avoid.

That won’t work for a couple reasons. In addition to the fact that you can’t link an event channel to an Item in the first place, there is no such thing as a triggered rule trigger for Items.

Thanks @rikoshak, I suspected that was the case, but thought I’d ask before I assumed.

In some ways it’ll give me tidier code, as if I make a separate rule for each channel and then use a function I don’t have to parse the name of the channel to work out which device it is. So I end up with:

Group  SensorLounge          "Lounge Sensor Light"   (Lights)                     
Switch swSensorInputLounge   "Lounge Sensor Switch"  (SensorLounge, LightInputs)    {channel="shelly:shelly1plus:XXXXX3:relay#input"}
Switch swSensorOutputLounge  "Lounge Sensor Output"  (SensorLounge, LightOutputs)   {channel="shelly:shelly1plus:XXXXX3:relay#output"}

Group  SensorDining          "Dining Sensor Light"   (Lights)                     
Switch swSensorInputDining   "Dining Sensor Switch"  (SensorDining, LightInputs)    {channel="shelly:shelly1plus:XXXXX3:relay#input"}
Switch swSensorOutputDining  "Dining Sensor Output"  (SensorDining, LightOutputs)   {channel="shelly:shelly1plus:XXXXX3:relay#output"}

rule "Shelly button lounge triggered"
when
  Channel "shelly:shellybutton1:d8f15bXXXXXX:status#button" triggered 
then
  ProcessButtonPress.apply( swSensorLoungeInput, swSensorLoungeOutput, "SensorLounge", event.event)
end

rule "Shelly button dining triggered"
when
  Channel "shelly:shellybutton1:d8f15bXXXXXY:status#button" triggered 
then
  ProcessButtonPress.apply( swSensorDiningInput, swSensorDiningOutput, "SensorDining", event.event)
end


val org.eclipse.xtext.xbase.lib.Functions$Function2<SwitchItem, SwitchItem, String> ProcessButtonPress = [ 
  Switch swInput,
  Switch swOutput,
  String sEvent |
  logDebug( 'Lights', 'Processing button press for ' + swInput.name + ' event was ' + sEvent )

  switch sEvent {
    case SHORT_PRESSED: {}
    case DOUBLE_PRESSED: {}
    case LONG_PRESSED: {}
    case SHORT_LONG_PRESSED: {}
    case TRIPLE_PRESSED: {}
    default: {
      logInfo( 'Lights', 'Received unhandled event ' + sEvent + ' on item ' + swInput.name )
    }
  }

]

Oh, sorry, I oversaw that it is a channel trigger…