MQTT, subscribing and parsing

I am having trouble trying to understand how to use MQTT for none Homie/HA devices. On the screenshot below, I would like to subscribe to the room/* topics and the device/* topics and then parse those topics in Jython. I can’t figure out how to set a OH item to catch those base topics and pass the full topic and the value.


Install MQTT binding

Create MQTT Broker

Create Generic MQTT Thing
Add Channel
TEXT Value
MQTT State Topic - room/*

Then Click on Channel and Add Link to item - Create new item

To elaborate on Jame’s answer a bit.

First, if it is Homie then the Thing can be automatically discovered by the binding. Just open the Inbox, click + and choose Homie. You don’t have to create a Generic Thing. When scanning for things, each device in that hierarchy will be discovered as a separate Thing and each Thing will have Channels for all the sensors and actuators. Those Channels you care about are linked to Items. No parsing of topics nor parsing of the telemetry nor anything like that is required. That’s kind of the whole point of bindings after all.

If you don’t want to take advantage of the automatic discovery of devices through Homie, you can. Follow the steps James outlined to create your MQTT Things with some modifications. The general preference is to do one Thing per device represented in the MQTT hierarchy. You will probably want to use the regex transform to extract each of the pieces if information you want from the array of name/value pairs that you are interested in. Each would get its own Channel. So for example, one Channel would subscribe to espresense/rooms/office with a regex .distance":"(.)".* for distance and another Channel would subscribe to the same topic with regex .speed":"(.)".* for speed.

You can open the code tab and once you create one Channel it’s really easy to copy/paste edit to create the rest.

You also have two options for wild card subscriptions. You can follow James’ approach to create a Channel linked to a String Item that receives the messages on any topic below rooms. However, the actual full topic name will be lost. You’ll just get the message itself which might not be sufficient to identify which Items the data belongs with.

Another alternative is to create an Event Channel on the MQTT Broker Thing itself. Use the wild card subscription and add a divider character. The event passed to the rule will have the full topic name as well as the message separated by that character so you can easily do a split to separate the two.

The MQTT Even Bus Subscription rule template work that way. MQTT Event Bus Subscription


@rlkoshak and @denominator thank you for the reply - have it working.

There is a new presence sensor using esp32’s that looks very interesting. Very simple to install (web based firmware load to esp32).

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.