[SOLVED] Using regex with Aqara wireless button

Good evening.

I’m running into a few issues, most likely because I’m terrible at Regex.

Here’s the issue, I have a switch that can report 5 different things, the payload from Zigbee2MQTT looks like this:

{"battery":100,"voltage":3005,"linkquality":99,"action":"shake"}'
{"battery":100,"voltage":3005,"linkquality":102,"click":"double"}'
{"battery":100,"voltage":3005,"linkquality":105,"click":"single"}'
{"battery":91,"voltage":2985,"linkquality":123,"action":"hold"}'
{"battery":91,"voltage":2985,"linkquality":139,"action":"release"}'

Generally I would use jsonpath to find the action, however the button switching between “click” and “action” in the payload makes this approach throw errors. Instead I wanted to use regex to just parse the entire payload and extract the relevant bit.

Here, my approach works: regexr.com/4p28c

But in openhab, this always returns null:

var action = "Placeholder"

rule "Wireless button press"
when
    Item WirelessSwitch_WirelessSwitchButton received update
then
    
    var action = transform("REGEX", "\bshake\b|\bdouble\b|\bsingle\b|\bhold\b|\brelease\b", WirelessSwitch_WirelessSwitchButton.state.toString)
    logInfo("debug", action)

    switch action{
        case action = "single":
         if( KontorLys_.state == ON ) {
        KontorLys_.sendCommand(OFF)  
        }
       else
       {
          KontorLys_.sendCommand(ON)
       }
    


    }
end

Where am I going wrong?

In openHAB, your REGEX expression must match the full message and the part that get’s returned is in the first capture group (first set of parens).

.*[action|click]\":\"(.*)\".*

You are a god among men, Rich.