Link many items to one mqtt channel - how to format mqtt output message

Hello people! I am having some troubles with formatting mqtt messages sent from my device. I am quite a beginner with openhab 2.5 so please consider that I migh be missing something very obvious, nevertheless, here is my issue.

I set-up an MQTT connection between my device and my openHAB server and everything works, but I can’t seem to upscale it.

current implementation (few sensors, one topic per sensor):

A couple of sensors are attached to my raspberry-pi (say: temperature, humidity, battery_level, …). This raspberry-py sends data to my openHAB server via MQTT protocol. Every sensor is a Thing, every Thing is linked to a Channel, and from the topic name I know what the sensor is streaming.

when I read the mqtt messages from my python client I get something like this:

  • topic = Temperature
  • payload = “56”

So far so good, everything works.

desired solution (one topic for many sensors)

Now I am going to add 9 more different temperature sensors, 9 more humidity sensors, and so on.
I created the related Things (so fe, Temperature_2, Temperature_3, …) and want to send their data along with the same MQTT message that I am already using for the temperature. The goal is to produce an MQTT message like this:

  • topic = Temperature
  • payload = {sensor1: 56, sensor2: 45, …}

problem

I tried adding all my Temperature Things to the temperature channel, but the payload doesn’t change and only reports the numerical temperature value of whatever sensor is streaming data.

I think I should be using the Outgoing Value Transformation. I have installed the JSONPATH extension and set the “outgoing value transofrmation” to “JSONPATH:$.device.status.temperature”
to see if it did something, but it cannot parse the experssion and thus no message is sent.

how can I fix this? How can I set the payload of the MQTT message?

Thank you very much in advance!

Just to check:

  • You have a bunch of sensors publishing values to your MQTT broker on various topics.
  • In openHAB you have a bunch of Things with Channels, subscribed to these topics.
  • For each Channel, you have linked an Item.
  • You now want (in openHAB) to take, for example, all the values of your temperature Items and publish them back to the MQTT broker in a single JSON string?

Is that right?

If so, look at using the publish action in a rule. Build your JSON string in the rule, then publish using the action.

A more standard approach would be to create one Thing to represent the device. Each sensor of the Device would have it’s own Chanel on that Thing.

Usually the topic has more than that. It’s rare that one would only have one temperature sensor so you need a topic hierarchy to separate them. For example:

raspi1/temp

For what purpose? This smells like an XY Problem. What is it you are really hoping to achieve and why do you think aggregating all the readings into one message will get you there?

If I’m reading this correctly, all of your sensors are publishing the temp readings to the same topic? If so how is openHAB supposed to know which sensor is publishing which values?

What you are asking about is how to aggregate the messages received on multiple different topics into one message (ignoring the potential other problems I just mentioned). In that case hafniumzinc has the approach. Each device needs to publish to its own topic. There should be a Channel for each device (e.g. temperature1, humidity2, etc. or even better raspi1/temperature, raspi2/humidity). Each of those is linked to an Item. When any one of those Items change, trigger a rule to build the JSON and publish using the MQTT Action.

But there is just enough oddness here to make me thing something else is going on and you are trying to solve an unstated problem in a way that isn’t going to actually work the way you want it to.