openHAB 2.5, MQTT and Shelly Plus devices

Hi all,

I’m still on openHAB 2.5 - I know I should look at upgrading to 3.x, but I don’t have the time at the moment…

I do have one minor query about integrating the new Shelly Plus range, which has massively complicated their MQTT data compared to the nice, simple, easy previous versions :frowning:

I’m using an i4, which has 4 inputs, and I wish to use them for triggering existing item(s) I have for opening my garage door and gate, as well as toggling on/off a compressor (the i4 won’t be the only way to switch this on/off via openHAB - I might use the app, or Alexa etc).

The MQTT I’m getting is like this:
topic: shellyi4plus-xxxxx/events/rpc

{
  "src": "shellyplusi4-xxxxx",
  "dst": "shellyplusi4-xxxxx/events",
  "method": "NotifyEvent",
  "params": {
    "ts": 1652210605.17,
    "events": [
      {
        "component": "input:0",
        "id": 0,
        "event": "btn_down",
        "ts": 1652210605.17
      }
    ]
  }
}

Followed by:

{
  "src": "shellyplusi4-xxxxx",
  "dst": "shellyplusi4-xxxxx/events",
  "method": "NotifyEvent",
  "params": {
    "ts": 1652210605.34,
    "events": [
      {
        "component": "input:0",
        "id": 0,
        "event": "btn_up",
        "ts": 1652210605.34
      }
    ]
  }
}

Followed by:

{
  "src": "shellyplusi4-xxxxx",
  "dst": "shellyplusi4-xxxxx/events",
  "method": "NotifyEvent",
  "params": {
    "ts": 1652210605.67,
    "events": [
      {
        "component": "input:0",
        "id": 0,
        "event": "single_push",
        "ts": 1652210605.67
      }
    ]
  }
}

or

{
  "src": "shellyplusi4-xxxxx",
  "dst": "shellyplusi4-xxxxx/events",
  "method": "NotifyEvent",
  "params": {
    "ts": 1652210605.67,
    "events": [
      {
        "component": "input:0",
        "id": 0,
        "event": "double_push",
        "ts": 1652210605.67
      }
    ]
  }
}

I’m (currently) only interested in the single_push event - of course once I get the syntax down, then I can apply it to any other message types (double-push, long-push etc)

All 4 inputs will send a message on the same topic, the only identifier being the id within the JSON.

I have looked at other topics here (such as Shelly plus i4) but these are referencing openHAB 3 and its UI-based MQTT configuration, and I’m not entirely sure if it’s totally relevant.

I got this far:
mqtt="<[mosquitto:shellyplusi4-a8032ab1ca7c/events/rpc:state:JSONPATH($.params.events[:1].event.single_push)]"

However the log shows the following error:

Error processing MQTT message.
java.util.regex.PatternSyntaxException: Unmatched closing ')' near index 19
1].event.single_push)
                   ^

As far as I can tell, I’ve got the corresponding number of opening and closing brackets of all types!

Any advice or assistance anyone can give will be greatly appreciated to get this going!

Thank you!

I expect you’ll have to escape that colon, the MQTT v1 config parser uses colons as delimiters.

1 Like

Yes, that colon did need escaping, becoming events[\\:1] (and this will pull the first entity in the events array.

I’ve bypassed that, though, as I found that I needed to actually retrieve multiple bits of information - the ID and the Event to determine exactly which of the 4 inputs have been activated, which I’ve done like this:

String Item_Name { mqtt="<[mosquitto:shellyplusi4-xxxxx/events/rpc:state:JSONPATH($.params.events[?(@.id == 0 && @.event == 'single_push')].event)]", expire="1s,state=NULL" }

I think I’ll then need to set up a Rule which is triggered on Item_Name changing to single_push to do what I want (in this case, activate another Switch item).

Quite why Shelly decided to complicate their MQTT implementation so much…