Logging changes/events to remote MQTT server as JSON payloads

The documentation on this isn’t clear at all (it really isn’t), but what i’m looking to do is log all the state changes and information coming out of OpenHAB to a remote MQTT broker (Mosquitto or HiveMQ). I want it to be a secondary connection (copy, or listener) and not local persistence, which should be different (Mongo or other NoSQL localised DB).

The remote cloud broker has other clients subscribing to it to record the messages to a centralised database, and is reacting to them to different ways which are not related to the specific OpenHAB device.

Let’s say i have 3 network devices (things) i want to ping every 60 seconds. I want the result of that ping to be sent as a JSON message to a remote Mosquitto instance on a VPS (i’m aware i’ll need a transformation potentially).

I have 3 Switch items set up cycling every 60s:

  • Network Device 1 (Thing) -> Online (Channel) --> (link) -> Device1_Online_Status (Item)
  • Network Device 2 (Thing) -> Online (Channel) --> (link) -> Device2_Online_Status (Item)
  • Network Device 3 (Thing) -> Online (Channel) --> (link) -> Device3_Online_Status (Item)

What i want is OpenHABian to keep its own persistence information, but for the remote MQTT Broker to receive 3 messages to 3 different topics (acting as both stateTopics and commandTopics), published to it by OpenHAB: Note the names need to be dynamic if possible.

  • room-name/network/device1/status --> state or command (R/W) as JSON payload
  • room-name/network/device2/status --> state or command (R/W) as JSON payload
  • room-name/network/device3/status --> state or command (R/W) as JSON payload

I also want ALL stateTopics and commandTopics sent to room-name/all.

I can seem to find a guide anywhere to this which doesn’t end in a maze of local/remote MQTT stuff. The goal is to create a stream of OpenHAB events monitored remotely, while allowing it to be configured as it wishes on a local level.

Is there a way to do this in PaperUI? All the examples with MQTT seem to involve config files.

In Paper UI create a generic thing

You can create a different channel for each topic.

Link each channel to the item you want the status of. You can use output transformation to get it to display in jason format.

This one is more difficult and I would use a rule action for this.

Probably not, at least not easily. In OH 3 it’s totally possible but PaperUI doesn’t support all the needed rule triggers.

What you are looking for is MQTT 2.5 Event Bus

Or you can set up and configure an Generic MQTT Channel for each Item individually and link them to all your Items using the follow profile. But that would be a whole lot more work.

I had a look through that article before but i got slightly lost in how it was applicable to my use case. I just saw you updated it so i’ll look again. Is it deprecated now in favour of the remote binding?

Could you quickly run through the procedure to set up the generic MQTT channel for each item? That in itself would be useful for testing.

There are certain use cases it can do that the remote binding cannot so it’s not deprecated by any means.

It’s standard MQTT stuff most of which is also covered in the EventBus tutorial. Create the Broker Thing. Create a Generic MQTT Thing. For each Item create and configure a Channel on the Generic MQTT Thing. Link the Channel to the Item using the Follow profile. If you stick to PaperUI it’s a whole lot of clicking the blue + icon and filling out forms, examples of which are in that tutorial.

1 Like

OK, for anyone looking to do this and not deal with the insane amount of documentation parsing, this is the short version of how you get an item to publish its state to a remote MQTT broker. An elaboration on Rich’s notes.

  1. Create a MQTT Broker thing (Inbox > Add > MQTT Broker). Fill in the remote details and give it a better name, like “Mosquitto Server”

  2. Create a Generic MQTT Thing (Inbox > Add > MQTT Generic Thing). Select the remote MQTT Broker you created in 1) as the Bridge. Give it a better name like “MQTT Transmitter”. Save.

  3. Figure out which Item you want to be sending and what type it is (Example: bulb color, Color).

  4. In your Generic MQTT Thing, create a new channel, and pick its type (e.g. Color).

  5. In the new window, give it an ID/name (e.g. bulb-1-color-update). Specify the topic on the remote server to publish updates to (stateTopic, /something/device-id/attributes/color) and its topic for commands (commandTopic). Also whether it should be retained, changed to JSON etc. Save.

  6. Back in the Generic MQTT Thing screen where you created the channel, hit the “target” icon of the channel as normal, and create a link. The “type” from the first dropdown should be “Follow”, and the item you want to publish its data.

  7. Change the item’s state, and in the MQTT broker the state of the item will be published into the queue, e.g. stateTopic-name = itemtype, or all-bulbs-color-update=126,210,5.

To send the payload as JSON, specify a JSON string object in the “Outgoing Value Format” with the vars found in the formatBeforePublish section for your item type: https://www.openhab.org/addons/bindings/mqtt.generic/#format-before-publish

{"value": [%1$d,%2$d,%3$d]}

Message received in Mosquitto:

{"value": [250,217,110]}

Or for a Hue color value (example):

{"hsb" : {"hue": %1$d,"saturation": %2$d,"brightness": %3$d}}

Received (formatted):

  "hsb": {
    "hue": 162,
    "saturation": 200,
    "brightness": 179

So to run through again:

A. Create connection to a remote Mosquitto Server as a MQTT Broker.
B. Create a MQTT Transmitter/Bridge as a local Generic MQTT Thing. Make a new channel for the Item you want it to talk to which has the remote topic info and transformation in it.
C. Set the new channel on the Transmitter/Bridge you created to (listen to) Follow the Item.

Thank you @rlkoshak!

1 Like