OH3 MQTT Subscribing String from a sensor published topic

I am running OH3 in Win10, trying to dabble with MQTT…

I have mosquito MQTT broker setup and running.

One of my sensor publishes a payload to a MQTT topic.

In OH3 I added the MQTT broker as a thing and can added a channel trigger to subscribe to the topic.
Thing:
MQTT Broker
UID: mqtt:broker:ab1234cd

Then Added the Trigger Channel
Config
MQTT Topic: sensor\profile
Separator Character : #
UID: mqtt:broker:ab1234cd:sensorprofile

When ever the sensor publishes to the topic sensor\profile i can see an entry in the events.log

2021-03-22 22:32:35.038 [INFO ] [openhab.event.ChannelTriggeredEvent ] - mqtt:broker:ab1234cd:sensorprofile triggered sensor/profile#3: Away

Now i am stuck with the next bit I want to read the value “3: Away” and display the sensor published MQTT payload.

I have tried to create a String Item from textual reference by

String SensorMQTTMessage “Sensor MQTT Message” {mqtt=">[mqtt:broker:ab1234cd:sensor/profile:state:*:default]"}

On going through community found references for using the receivedEvent but get logs say an error as undefined.

I know i am missing something to read the value from the sensor, can anyone point me in the right direction.

The end goal is to get input from the sensor to trigger some other item based on the payload.

Please read the documentation. The actual MQTT binding needs a broker Thing that acts as a bridge (you have that), this Thing does not need any channels! The MQTT Things which are representing your devices need to be created additionally and be connected to the above bridge.
The linking syntax on your posted items belongs to the version 1 MQTT binding and not to the actual one!

1 Like

Perhaps if you can confirm what your topic is, and the payload that gets published to the topic we can help you.

In general, to use MQTT in openHAB you will need to:

  1. Install an MQTT broker, and connect openHAB to that broker using a Bridge/Broker Thing.
  2. Create a Generic MQTT Thing for your sensor device, then add the topic you wish to subscribe to into a new Channel within the Thing.
  3. Link the Channel to an Item

This step by step in the docs might help.

This bit should be easy: we’d set up a rule which gets triggered when the Item is updated, then perform a command on your other Item depending on the payload.

1 Like

The difficult part here when following the binding docs is to get off page one, which describes trigger channels for the broker Thing.
These are of limited value and not often used.

Following the link to “Things and channels” page gets you the more useful approach to linking MQTT to openHAB Items.

2 Likes

I was missig the Generic MQTT Thing. Misunderstood the Generic MQTT thing as being same as the trigger channel.

  1. MQTT Thing - Broker - Done
  2. Generic MQTT Thing using Broker - Missed doing this
    Once the Step 2 was missed rest all didnt work…
  3. Added Text Channel to Generic MQTT Broker with Topic of the Sensor state.
  4. Linked an String Item to the Generic MQTT Thing’s text channel
  5. Added pattern metadata to the String Item

It worked. OH3 sucessfully subscribed to the sensor topic. Now i am upto building the rules when the String Item is updated triggering other items / things.

Thanks for the help.

I agree may be i was impatient but i skipped the Things and Channels link at the top. May be if it was at end othe page I might have followed (but still my imparience might have got the better of me)