MQTT for Domoticz
Step-by-step how-to guide install and use MQTT with the latest Domoticz.
For this how-to we will be using the Mosquitto MQTT instance in combination with Linux Raspbian (we assume you are using a Raspberry Pi but this guide will work on most other Linux distro’s).
MQTT is a simple messaging protocol, designed for constrained devices with low-bandwidth. So, it’s the perfect solution for Internet of Things applications and Domoticz. It allows you to send commands to control outputs, read and publish data from sensor nodes and much more.
If you are new to MQTT or if you want to know more about it, this video is a very clear and complete explanation (thanks Opto 22!). For Domoticz, you can disregard the ‘keep alive timer’ part, this is less relevant for us.
In this guide we do not further protect the broker and make it accessible for the complete network. We do it this way because the information shared is not that secret and if you are new to MQTT, this can help you to get it working and gain some knowledge and experience with this technology. By default, the Mosquitto broker is not protected.
Also, when we connect the MQTT broker to Domoticz, Domoticz also publishes it’s own device update to the broker. You can switch this off but it can be very useful to debug any issues. And it does not do any harm as MQTT is very lightweight technology.
Domoticz works a bit different with MQTT than other home automation platforms. It only uses 2 channels for MQTT communication, one for receiving updates and one for updating it’s own device changes.
With that, it does not follow the conventional “Homie” standard like OpenHab or Home Assistant. Domoticz makes MQTT incompatible this way, really a missed opportunity for Domoticz (I hope this changes in the future)…
More information about the Homie standard:
https://homieiot.github.io/
Prerequisites
- Running Domoticz installation with stable Raspbian Buster release and SSH access. See my previous post for a how-to:
https://www.sancla.com/domoticz/raspberry-pi-4-with-domoticz/ - Basic knowledge about network and MQTT (see video)
Tested with
- Raspberry Pi 4 (MEM 2GB with 16GB sd-card)
- Raspbian Buster Lite 4.19, Februari 2020
- Domoticz Stable 2020.1 (compile date 22-3-2020)
Step 1: Install the MQTT broker
To install the Mosquitto broker and client run the following commands:
sudo apt update -y && sudo apt install mosquitto mosquitto-clients -y && sudo reboot
After these commands, the raspberry pi reboots and the new MQTT broker should be up and running. To check if Domoticz is properly running after the reboot, run the following command:
sudo systemctl status mosquitto.service
You should see something similar like this:
Step 2: Link MQTT to Domoticz
Next is to connect the MQTT broker to Domoticz. You can add the MQTT broker by going to the Domoticz menu and then the Hardware menu.
Add the MQTT broker to Domoticz with the following settings:
- Name: MQTT Gateway
- Type: MQTT Client Gateway with LAN interface
- Date Timeout: Disabled
- Remote Address: localhost
- Port: 1883
- Username: Leave this empty
- Password: Leave this empty
- Prevent Loop: True
- Publish Topic: Out
You can also set this to ‘none’ if you do not want Domoticz to publish changes back to the domoticz/out channel on the MQTT broker. - CA Filename: Leave this empty
- TLS Version: tlsv1
Like this for example:
Step 3: Adding a MQTT device
Once you have configured the MQTT hardware device in Domoticz, it’s time to add some devices to Domoticz that we can use. For this, we use Dummy devices. Dummy devices are not directly linked to any hardware or plugin, not even to the new installed MQTT broker. Instead, it makes it possible to update values through the MQTT broker. You’ll understand this process a bit better after the following steps. Don’t worry, we’ll get there…
Create a new Dummy hardware devices with the following settings:
- Name: MQTT Devices
- Type: Dummy (Does nothing, use for virtual switches only)
- Date Timeout: Disabled
Next is to create a specific virtual sensor:
For this example, we create a temperature sensor called “temp01”:
Step 4: Test the MQTT sensor from the command line
Now that we have set up the broker and connected it to Domoticz with a dummy device, let’s update that sensor true Domoticz. To do so, we need to know the IDX value of the sensor. Look it up in the Domoticz menu under devices. In our case, the IDX value of the newly created device is “1”
As we also installed the Mosquitto client, we can now update the sensor with a new value from the console. Use the following command for that, but change the value for IDX for the value of your own sensor. In this example, we use the IDX value 1 that we looked up in Domoticz. We set the temperature to 27 ℃.
mosquitto_pub -h localhost -m '{ "idx" : 1, "nvalue" : 0, "svalue" : "27.0" }' -t 'domoticz/in'
As you can see in Domoticz, the sensor has now been updated with a temperature of 27.0 ℃.
Other Clients
As we can connect multiple MQTT clients to the broker and subscribe to topics, this can be extremely useful if you want to see what’s going on with the broker. And there are multiple different clients, for different scenarios.
With all options, you can subscribe to the # channel. This will give you a complete overview of all MQTT messages going true the broker. Including the messages that Domoticz will publish itself with device updates. With this information you can further identify and analyze your Domoticz configuration. Of course you can also subscribe to specific topics, that’s up to you.
Keep in mind:
- MQTT Gateway or broker: IP address of your Raspberry Pi
- Port: 1883
- No username or password necessary
- Channel # for all MQTT messages on the broker
- Channel domoticz/in for messages from anything to Domoticz
This is the channel you publish to, to update Domoticz sensors. - Channel domoticz/out fom messages from Domoticz to broker
This is where Domoticz publishes it’s own updates to the broker, for other MQTT subscribers or clients to read.
Linux console
As we also installed the Mosquitto client you can subscribe to channels from the MQTT broker with the command below. Keep in mind that if you want to both publish and subscribe to a MQTT channel, you are going to need 2 SSH sessions. One to publish and one to read the results.
mosquitto_sub -v -t '#'
Windows (or Linux)
One MQTT client that I really like in particular is the MQTT.fx client by Jens Deters. It has a clean and intuitive interface and is also available for the Linux Desktop.
Direct download link for the latest 64-bit Windows version:
http://www.jensd.de/apps/mqttfx/1.7.1/mqttfx-1.7.1-windows-x64.exe
More information can be found on the corresponding website:
https://mqttfx.jensd.de/
Android
For the Android phone I found the the MyMQTT app extremely useful. Not only does this give a quick and easy insight in all messages and the app is very intuitive.
https://play.google.com/store/apps/details?id=in.dc297.mqttclpro
Apple iPone
For the Apple iPhone I found the MQTTool from Brent Petit.
Unfortunately I did not have an Apple product at hand when I wrote this to take it out for a test drive. But it seems rather complete by the looks of it.
Grumpf
Very clear, thank you so much.
Jasper
Hi,
I coppied the line mosquitto_pub -h localhost -m ‘{ “idx” : 1, “nvalue” : 0, “svalue” : “27.0” }’ -t ‘domoticz/in’ and before I could change the idx to my number i probably hit enter, Now my sensor 1 has a double value and therefor no longer working.
how can i restore this command?
SANCLA
Hi Jasper,
Thank you for your patience.
You should be able to edit the domoticz database en remove that record.
I expect that would restore your sensor without affecting the history of that sensor.
Marco
Hi,
i found your guide and it helped me a lot to understand the system. Thank you very much.
I have a question and I hope you can help me:
I am trying to bring the values of a SmartMeter to Domoticz.
But to stay in your example, what and where do I have to set that the virtual sensor Temp01 automatically adjusts itself when the value changes.
best regards
johan
How is it possible to view only one specific device in the domoticz/out queue, I mean how do you select that specific device when domotic is publishing all the domoticz devices with the topic domioticz/out with only the idx in the body
iamadmin
Try to filter on the IDX, that should do the trick!
ruben
Hi, I have tried to use your instruction. Installation looks to be good. But when I runn the “sudo systemctl status mosquitto.service” I gett this error.
pi@raspberrypi:~ $ sudo systemctl status mosquitto.service
● mosquitto.service – Mosquitto MQTT Broker
Loaded: loaded (/lib/systemd/system/mosquitto.service; enabled; vendor pre>
Active: failed (Result: exit-code) since Thu 2021-11-11 21:06:01 CET; 1min>
Docs: man:mosquitto.conf(5)
man:mosquitto(8)
Process: 581 ExecStartPre=/bin/mkdir -m 740 -p /var/log/mosquitto (code=exi>
Process: 582 ExecStartPre=/bin/chown mosquitto /var/log/mosquitto (code=exi>
Process: 583 ExecStartPre=/bin/mkdir -m 740 -p /run/mosquitto (code=exited,>
Process: 584 ExecStartPre=/bin/chown mosquitto /run/mosquitto (code=exited,>
Process: 585 ExecStart=/usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf>
Main PID: 585 (code=exited, status=1/FAILURE)
CPU: 86ms
Nov 11 21:06:01 raspberrypi systemd[1]: mosquitto.service: Scheduled restart jo>
Nov 11 21:06:01 raspberrypi systemd[1]: Stopped Mosquitto MQTT Broker.
Nov 11 21:06:01 raspberrypi systemd[1]: mosquitto.service: Start request repeat>
Nov 11 21:06:01 raspberrypi systemd[1]: mosquitto.service: Failed with result ‘>
Nov 11 21:06:01 raspberrypi systemd[1]: Failed to start Mosquitto MQTT Broker.
iamadmin
Hi Ruben,
Seems Mosquito is unable to start. Let diagnose that and take a look at the log files.
Please run this command to check the last 20 log file lines:
sudo tail -n20 -f /var/log/mosquitto/mosquitto.log
Ruben
Hi,
It would be much appreciated if you could help.
The Pi was started fresh for the purpose of using Domoticz.
First I installed Domoticz and then Mosquitto.
When I run the command I get this:
pi@raspberrypi:~ $ sudo tail -n20 -f /var/log/mosquitto/mosquitto.log
1636661160: Opening ipv6 listen socket on port 1883.
1636661160: Error: Address already in use
1636661160: mosquitto version 2.0.11 starting
1636661160: Config loaded from /etc/mosquitto/mosquitto.conf.
1636661160: Starting in local only mode. Connections will only be possible from clients running on this machine.
1636661160: Create a configuration file which defines a listener to allow remote access.
1636661160: For more details see https://mosquitto.org/documentation/authentication-methods/
1636661160: Opening ipv4 listen socket on port 1883.
1636661160: Error: Address already in use
1636661160: Opening ipv6 listen socket on port 1883.
1636661160: Error: Address already in use
1636663188: mosquitto version 2.0.11 starting
1636663188: Config loaded from /etc/mosquitto/mosquitto.conf.
1636663188: Starting in local only mode. Connections will only be possible from clients running on this machine.
1636663188: Create a configuration file which defines a listener to allow remote access.
1636663188: For more details see https://mosquitto.org/documentation/authentication-methods/
1636663188: Opening ipv4 listen socket on port 1883.
1636663188: Error: Address already in use
1636663188: Opening ipv6 listen socket on port 1883.
1636663188: Error: Address already in use
I tried a listening commando and got this.
pi@raspberrypi:~ $ netstat –tcp –listening –programs –numeric
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:1883 0.0.0.0:* LISTEN –
tcp 0 0 127.0.0.1:1884 0.0.0.0:* LISTEN 1523/mosquitto
tcp 0 0 0.0.0.0:5900 0.0.0.0:* LISTEN –
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN –
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN –
tcp6 0 0 ::1:1883 :::* LISTEN –
tcp6 0 0 :::443 :::* LISTEN –
tcp6 0 0 ::1:1884 :::* LISTEN 1523/mosquitto
tcp6 0 0 :::6144 :::* LISTEN –
tcp6 0 0 :::5900 :::* LISTEN –
tcp6 0 0 :::8080 :::* LISTEN –
tcp6 0 0 :::22 :::* LISTEN –
tcp6 0 0 ::1:631 :::* LISTEN
SANCLA
Hi Ruben,
It seems there is already a mosquitto service running.
First, reboot your pi, just to be sure.
Check the Domoticz installation for the config etc.:
https://www.domoticz.com/wiki/MQTT#Installing_Mosquitto
Third, just work with it.
Do some tests with it, and see if mosquitto is running and is accepting external connection.
if not, do this:
sudo nano /etc/mosquitto/mosquitto.conf
add:
allow_anonymous true
ruben
Reboot gave me the same error.
Trygd to go thru the installation again but at least with my knowledge it is the same.
Trygd then to work withe as it is and maid at virtual switch in Domoticz to se if I got a reading on pi terminal. Then I got this:
pi@raspberrypi:~ $ mosquitto_sub -h 127.0.0.1 -v -t “domoticz/out”
domoticz/out {
“Battery” : 255,
“RSSI” : 12,
“description” : “”,
“dtype” : “Light/Switch”,
“hwid” : “3”,
“id” : “00014051”,
“idx” : 1,
“name” : “Test Switch”,
“nvalue” : 0,
“stype” : “Switch”,
“svalue1” : “0”,
“switchType” : “On/Off”,
“unit” : 1
Looks to be okay….?
Then I tried to connect my smart energy meeter, but then I’m getting this error message in Domoticz.Error: Error parsing http request address: ::ffff:192.168.1.26