Once the MQTT broker is installed and tested, it is time to integrate the protocol into Domoticz. The reader may want to consult the more recent post: MQTT with Domoticz instead of reading this rather old document.
Table of Contents
- Domoticz MQTT Hardware
- Publishing MQTT Messages
- Subscribing and Receiving MQTT Messages
- Less Prolixity
- Food for Thought
Domoticz MQTT Hardware
The first step is to add the Domoticz MQTT "hardware". What will actually be done is to enable the MQTT protocol. This basically adds the "translation layer" that I was describing at the end of the previous post. Adding hardware is done in the usual fashion:
- Click on the Setup tab.
- Click on the Hardware menu item.
- Select
MQTT Client Gateway with LAN interface
for Type. - Set the Name to something meaningfull such as
MQTT
. - Set Publish Topic to
out
. You may want to change this latter. - Everything else should be ok, so click on the button at the bottom.
If everything is ok, the new hardware should appear at the top of the table of installed hardware and a message saying that the connection with the broker was established should flash momentarily near the bottom of the window.
Publishing MQTT Messages
What does the MQTT hardware do? It does only two things from what I can figure out. One of them is to publish messages to the MQTT broker... lots of messages.
Check it out. Open a terminal window and subscribe to all topics "#".
Then turn a lamp on and off with
Try with other switches. If you have a weather information installed, then lots of other messages will appear periodically.
Not unexpectedly, the message topic is domoticz/out
, meaning Domoticz
publishing (as in "sending out") the message, and the
message itself is JSON formatted list of key and value pairs. Presumably,
an MQTT device subscribed to the domoticz/out
topic will parse all
incoming messages on that topic, and when it finds the correct information, it will act on
it. That doesn't sound like much fun, but lets leave that for tomorrow.
Subscribing and Receiving MQTT Messages
The second thing that the MQTT hardware does is to subscribe to
domoticz/in
messages and process these as they come in.
And how that works, we know pretty well.
Pick up the idx of a switch in the Devices
table (remember how?
click on the Setup tab and then on the
Devices menu item.) and open a terminal
window to publish an MQTT message:
The icon for the Dresser lamp should reflect that it has been turned on. Furthermore, the lamp should in fact be lit and if the Subscribe terminal is still open, the following should have been received:
Lets do this again, first turn off the Dresser lamp and then turn it on at 25% brightness:
Less Prolixity
I feel for our little Wifi modules that will have to process all these messages from Domoticz most of which will have nothing to do with a single module. How to cut down on the traffic?
One way is to change Publish Topic
from "out" (called Flat) to "/" (meaning Hierarchical)
in the MQTT Client Gateway...
Try it (don't forget to click on the
under the table of installed hardware). You may be surprised to see that
nothing is published. The reason is that a message will be published only
if the device has been placed on a floor plan.
If all the devices are not placed on floor plans then it is possible
to change Publish Topic to "out + /"
(called Combined) which will accomplish our goal while
paradoxically increasing the total number of published messages. With this
choice all messages will be published under the topic domotics/out
and all messages pertaining to devices placed on a floor plan will be
published again under the topic domoticz/out/${floorplan name}/${plan name}
as in the following example:
domoticz/out/Top Floor/Master Bedroom
topic
which will considerable diminish the number of messages it will get
If all MQTT devices are placed on floor plans then setting
the Publish Topic to "/" (Hierarchical).
In that case, duplication is removed, there will be no messages posted to the
top level domoticz/out
topic.
I find the floor plan capabilities of Domoticz a bit
flaky. That is probably because of a combination of the somewhat underpowered
Raspberry Pi running the server and my own impatience. Right now, I wish
it would be possible to have yet another Publish Topic
choice:
domoticz/out/${roomplan}
.
Another drastic possibility is to disable publishing by MQTT Client Gateway
altogether and to use a script file. It will be possible to considerably simplify the
output in that case because it will not be necessary to publish a universal JSON record.
Here is an example of a possible bash script for switches (download).
The On Action and Off Action
for the corresponding switch should contain something like
script:///home/pi/domoticz/scripts/bash/mqtt_switches.sh 28 1
where the first parameter is the device idx number and the second is the nvalue 1 (for on) or 0 (for off).
script:///home/pi/domoticz/scripts/bash/mqtt_switches.sh 28 0
This subject requires more thought and I will return to it as I install real MQTT hardware and decide how to go about it.
Food for Thought
While not positive, I think I read somewhere that MQTT messages are processed more quickly than
HTTP requests by the Domoticz server. It might be worth investigating and
if the difference is appreciable, the bash scripts used by the Heyu bridge and
the Away
button could be rewritten.