I’ve built some custom light controllers controlled via MQTT messages, with a few different functions, eg on/off switch, dimmer values, colour temp values etc.
I searched around, but info is mainly related to incoming MQTT JSON payloads rather than a bi-directional system.
Currently each associated item within OpenHAB is linked to a separate channel with a separate MQTT topic. For various optimisation reasons, I’m experimenting with packing all the data into a single MQTT payload as a JSON.
This thread is more of a brainstorm rather than a troubleshooting exercise, as I have come up with a workable solution, but I’m not sure it’s the default or best way to do it. Since there are other off the shelf products that operate in a similar manner, there must be a few other solutions out there.
To create an outgoing MQTT message, I created a single string item which is linked to a string/text channel which has an associated MQTT command topic. I then wrote a script which is triggered by commands sent to any of the associated switches/dimmer etc. The script creates a JSON object, and converts it to a string, which is then sent to the aforementioned string item, which is then sent to the linked channel and MQTT topic.
Basically the reverse happens for incoming MQTT messages. There’s a string item which receives the incoming MQTT JSON payload, then a script parses the incoming payload into a JSON object, then all the required items are updated from the name/data pairs in the JSON.
The first attempt used the JSONPATH addon in order to update the items directly by extracting the name/data pairs from the MQTT message directly. However when a channel has an update MQTT topic, but no command topic, the item becomes a read-only item which can’t be controlled through the UI.
Is this input/output string method the simplest solution? It’s quite a “native” solution, as it only relies on the Javascript scripting addon which is pretty integral to OH anyways.
Anyways, what are your approaches?