Virtual sensor update via MQTT

This can also be used to verify that Domoticz and Mosquitto MQTT broker are working fine together. Another example is the use of commands like this to update or control virtual sensors or switches on Domoticz from another device like Arduino or ESP. It can be handy if you are making some custom devices that will communicate with Domoticz. Basically, it is a virtual sensor update via MQTT.

Environment that we work on

What we have here is a Domoticz V4.10717 and Mosquitto 1.6.8 x64 installed on the Microsoft Windows server 2016. Why is that important? Well if you have these running on the Linux, commands may be a little different. The principle is the same, but some characters are handled in a different way.

How is this gonna work?

So, we are going to show three examples of Mosquitto commands. One for updating a virtual switch that has only “On” and “Off” options. The second one will be updating a virtual temperature sensor. The third one is an update of the Temperature + Humidity virtual sensor. We will also try to explain what Domoticz expects as input for different sensors, and how to fulfill that with Mosquitto commands.

Updating the virtual switch with MQTT

First, open the command prompt and navigate to a directory where Mosquitto is installed. In my case, it is “C:\Program Files\mosquitto\”. Then go to Domoticz and check the IDX of the virtual switch that you want to update. There are few examples in other Domoticz related articles so if needed you can look there for more info. In this case, IDX of the virtual switch is 3, so here is the command: mosquitto_pub -h localhost -t “domoticz/in” -m “{ \”idx\”:3,\”nvalue\”:1}” .

Take the command apart

The mosquitto_pub is the app. -h is an option that is used to specify the address of MQTT broker, and since it is the same machine we use “localhost”. -t is an option to specify the topic and we use default “domoticz/in”. Finally -m is an option to specify the MQTT message.

Something about MQTT message

Let us peek into what we are sending as a message. “{ \”idx\”:3,\”nvalue\”:1}” For easier understanding here is what Domoticz expects: Topic: domoticz/in, Message: { “idx”:3,”nvalue”:1} . To say it short whatever part is between “” it is a string data type. If something is not between double quotes, it means that it is an int data type. Let`s go back to message that we are sending “{ \”idx\”:3,\”nvalue\”:1}”. there is a backslash before every double quote. In essence, we need to use special character ” as a regular one, so we prepend it with a backslash like this \” That is called escaping a character. I hope it is a little easier to understand this command now. Here are screenshots of sensor status and command execution together. It is obvious that if we send 1 as nvalue virtual switch goes to On status and with 0 it goes to Off.

Virtual switch update with MQTT.
mosquitto_pub -h localhost -t “domoticz/in” -m “{ \”idx\”:3,\”nvalue\”:1}”
Virtual switch update with MQTT.
mosquitto_pub -h localhost -t “domoticz/in” -m “{ \”idx\”:3,\”nvalue\”:0}”

Updating the virtual temperature sensor with MQTT

We will start with the command. mosquitto_pub -h localhost -t “domoticz/in” -m “{ \”idx\”:1,\”nvalue\”:0,\”svalue\”:\”18.4\”} One thing different now is svalue since it is expected to be sent as a string comparing to nvalue that is an integer. What I`m talking about is that 0 is without double quotes and 18.4 is between them. Let us see how the sensor and commands look like.

Virtual sensor update with MQTT.
mosquitto_pub -h localhost -t “domoticz/in” -m “{ \”idx\”:1,\”nvalue\”:0,\”svalue\”:\”18.4\”}

Updating the virtual temperature + humidity sensor with MQTT

The command: mosquitto_pub -h localhost -t “domoticz/in” -m “{ \”idx\”:2,\”nvalue\”:0,\”svalue\”:\”1.2 ; 33 ; 1\”} . Now everything in svlaue is a string that looks like value than space than ; than space than value than space than ; than space and 1 at the end. That is how Domoticz expects iput for this sensor. To be honest, I do not know why is there a third value (1) here, I can only assume that it could be for the battery level or something like that. To summarize important things here 1.2 is the temperature, and 33 is the humidity percentage. Let`s see this one`s screenshot.

Virtual sensor update with MQTT.
mosquitto_pub -h localhost -t “domoticz/in” -m “{ \”idx\”:2,\”nvalue\”:0,\”svalue\”:\”1.2 ; 33 ; 1\”}

That`s all, folks. Now you can manipulate virtual sensors manually or as we said before, perform virtual sensor update via MQTT. Thank you for reading.

March 8th, 2020 by