March 8th, 2020 by Chief

pin up

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.


Posted in *.*, Home Automation Tagged with: , ,

Domoticz and Mosquitto on Windows
March 1st, 2020 by Chief

pin up

This guide is about how to set up a functional Domoticz environment on Windows. Moreover, it is Domoticz and Mosquitto on Windows. As I figured out most of the Domoticz instances are running on Linux-es\Raspberry-es. In contrast, I just want to show how to do it this way. Since I already have a PC running 24/7 and believe it or not I like Windows, it was a logical choice.

Few practical advice’s

Windows Firewall

I know that it is not a best practice, but before you begin I would suggest that you turn off windows firewall for good (just temporary all networks). Finally, when everything is up and running you can turn it back on and configure it to allow ports needed for these apps to work. We will mention that again later.

Microsoft Visual c++ redistributable

Another thing is Microsoft Visual c++ redistributables. If you ask me, download them all from 2013 to the latest version in both x86 and x64 versions and install them from the oldest to the newest one by one. It may sound stupid, but it is not taking much space and many apps rely upon those redistributables. Domoticz is one of them! Finally, if it is hard to find all of them just write a comment for this article, I do have one archive to rule them all and I can send it to you. 🙂

Install Domoticz on Windows

First of all, download the installation from here. Setup is as easy as it can be so there is almost nothing to explain. Just run it and click next. The only thing that I can suggest is that you chose to run Domoticz as a service as it is the right thing to do. Explaining why is not part of this article but let’s just say that due to its nature and purpose Domoticz should be running all the time, 24/7.

Port numbers

In case you are ok with default port numbers (and there are not many reasons not to be) just leave them be and finish the installation. Hence, everything went fine fastest way to check is it working is to open the browser on the same computer where you just installed Domoticz and open this address: http://localhost:8080. You should get something like this.

Service Status

In case nothing opens or page is not available most probably Domoticz service is not running. Consequently, you need to figure out what is happening. Type services in Windows search and open it. Find Domoticz service and check it`s status. The service should be running.

The service status should look like this. If service is not running try to start it. In case service starting ends with failed, or pause or stoped, there is a problem. Most likely you did not turn off a firewall or did not install those Microsoft Visual c++ redistributables that I mentioned before. It is now wise to visit Windows Event viewer and check Windows application logs for errors or warnings regarding Domoticz service. Now, let`s continue with Mosquitto installation.

Posted in *.*, Home Automation Tagged with: , , , ,