Retrieving data from MQTT in MessagePack format

I have a BLE scanning device that can push data to a http endpoint or mqtt topic. It does so in a big blob of MessagePack data, that I’ve previously been decoding using this node module via a small ExpressJS server.

I’d really like to turn off this tiny server and instead have a OpenHAB JS rule grab the data from MQTT. I can see the data in OpenHAB, having setup a Generic MQTT thing. It looks like a big blob of text and binary. For the life of me I can’t decode it though. With the help of various LLMs, I’ve tried:

  • decoding the messagepack data with raw JS
  • encoding the MQTT string as Base64 (in the Thing as a JS(scriptname.js) transform
  • using a msgpack2json binary (again as a transform)

None of them seem to recognise the format correctly. Seeing as none of the Channel types in the Generic MQTT Thing natively support binary (except Image - but it seems to try to actually interpret it as an image), is this even possible? I’ve used a Text type at the moment, but it’s possible/likely there is a bunch of casting to a String that is breaking the data before I see it right? Can anyone confirm either way? Any suggestions how I might do this OR do I go back to just running an Express app?

There is no Item that can store something like MessagePack which is a mix of text and binary. If you use an Event Channel to subscribe to the topic the messages will be converted to a String before they are sent to the rule.

In short, you either need to figure out how to convert the String that shows up in the rule back into the binary format that is MessagePack, and then you can use the third part node module to parse the data out. That’'s why none of your attempts have succeeded. What you get in the rule isn’t MessagePack any more, it’s a String.

I don’t know. I suspect it might be possible, but it will take some work to figure out exactly what the MQTT binding does to the message to convert it to a String and then figuring out how to reverse that in the rule before using the third party libraries to parse it. It might take reviewing the MQTT binding code and possibly the HiveMQ code that the MQTT binding uses. I don’t know exactly where the conversion to String happens.

It might be as simple as converting the String to an array of bytes.

I really couldn’t get it to work natively in OH.

I’ve create a small Docker container to act as a proxy instead, taking the MessagePack data and pushing it into MQTT so OH can consume it. It’s here if anyone wants to push Bluetooth device presence data from the April Brother Gateway V4 to MQTT - GitHub - alackmann/ble-gateway-mqtt-proxy