Page 16 of 19
Re: [90] MQTT Support
Posted: Sunday 05 November 2017 12:15
by mvveelen
SweetPants wrote: ↑Sunday 05 November 2017 11:33
mvveelen wrote: ↑Saturday 04 November 2017 19:14
The problem I'm facing: I have a Sonoff with Tasmota which is switched from Domoticz using MQTT (at least, I THINK it works that way, because I don't have any http URL's in the On / OFF settings of the switch). Sometimes the Sonoff loses the connection to my WiFi network and when Domoticz tells the Sonoff to switch, this switching isn't issued when the Sonoff comes back online in the WiFi network. I would expect it to issue the last switch command , but it doesn't.
How to fix ?
You have to send the message as a retained message so the broker can send it after re-connect. Domoticz itself does not know that your sonoff was disconnected
Care to elaborate on how one can do this exactly ?
Re: [90] MQTT Support
Posted: Sunday 05 November 2017 12:38
by SweetPants
mvveelen wrote: ↑Sunday 05 November 2017 12:15
Care to elaborate on how one can do this exactly ?
Using mosquitto_pub, add the -r option. The broker will remember the last published message and re-send it when the client re-connects. I use it for all my lights (Sonoff and ESP DIY hardware).
Re: [90] MQTT Support
Posted: Sunday 05 November 2017 12:42
by mvveelen
Sounds exactly like I want, but how?
At the moment I have a (dummy) switch with a certain IDX and in the Sonoff I have added this IDX, so where should I use this 'publish' command? Is the setup I use just the wrong setup maybe?
Re: [90] MQTT Support
Posted: Sunday 05 November 2017 12:53
by SweetPants
I run a bash script for the ON/OFF action like: script:///home/pi/scripts/switch ESP_0CA9E1 On
#!/bin/bash
TOPIC=/WiFi/$1/switch
COMMAND=$2
mosquitto_pub -h localhost -p <your MQTT port> -u <username> -P <password> -r -t $TOPIC -m $COMMAND
make sure the script is executable
Re: [90] MQTT Support
Posted: Sunday 05 November 2017 15:08
by mvveelen
Hmmm, not sure if I understand this correctly:
You have 1 script for all your devices which you use to communicate with MQTT ? What is the name of the script?
I use:
Code: Select all
root@DiskStation:/volume1/@appstore/mosquitto/bin# ./mosquitto_pub -h 192.168.0.124 -t 'domoticz/in' -m '{"command": "switchlight", "idx": 4128, "switchcmd": "On"}'
So should the script be something like:
Code: Select all
#!/bin/bash
TOPIC=/domoticz/$1/switch
COMMAND=$2
/volume1/@appstore/mosquitto/bin/mosquitto_pub -h localhost -p 1883 -u <username> -P <password> -r -t $TOPIC -m $COMMAND
Don't think this is right though......
Re: [90] MQTT Support
Posted: Sunday 05 November 2017 18:11
by SweetPants
add option -r into your script and voilla
Re: [90] MQTT Support
Posted: Sunday 05 November 2017 20:18
by mvveelen
SweetPants wrote: ↑Sunday 05 November 2017 18:11
add option
-r into your script and voilla
Well, not yet. What would the complete command look like when I don't want to use the IDX anymore, but instead I use the topic?
FYI:
Every Sonoff has it's own unique Topic. Like: sonoff46, sonoff81, etc.
Not using a user and/or password either.
Code: Select all
/volume1/@appstore/mosquitto/bin/mosquitto_pub -h 192.168.0.124 -p 1883 -u '' -P '' -r -t 'sonoff46' -m '{"command": "switchlight", "switchcmd": "Off"}' -r
doesn't do anything, so I'm guessing the dommand is not OK
Re: [90] MQTT Support
Posted: Wednesday 10 January 2018 13:29
by welby
Just learning MQTT and am struggling a bit with the domoticz/in commands. I can update and switch stuff using commands as described in the MQTT wiki section such as.
{"command": "switchlight", "idx": 2450, "switchcmd": "On" }
But maybe i'm confused and thought you could send /json commands also such as
/json.htm?type=command¶m=switchlight&idx=99&switchcmd=On
Or am i totally confusing myself and need to read more about it?
Re: [90] MQTT Support
Posted: Wednesday 10 January 2018 14:55
by SweetPants
welby wrote: ↑Wednesday 10 January 2018 13:29
Just learning MQTT and am struggling a bit with the domoticz/in commands. I can update and switch stuff using commands as described in the MQTT wiki section such as.
{"command": "switchlight", "idx": 2450, "switchcmd": "On" }
But maybe i'm confused and thought you could send /json commands also such as
/json.htm?type=command¶m=switchlight&idx=99&switchcmd=On
Or am i totally confusing myself and need to read more about it?
You can do both. The JSON method tents to be faster then the MQTT way.
Re: [90] MQTT Support
Posted: Thursday 11 January 2018 6:54
by welby
I've been trying to send the json commands via mqtt which i'm guessing is wrong. They need to be sent via http post maybe?
Re: [90] MQTT Support
Posted: Thursday 11 January 2018 7:17
by SweetPants
welby wrote: ↑Thursday 11 January 2018 6:54
I've been trying to send the json commands via mqtt which i'm guessing is wrong. They need to be sent via http post maybe?
You cannot send the (HTTP) JSON commands through MQTT, both are separate API's see the Wiki
https://www.domoticz.com/wiki/MQTT and
https://www.domoticz.com/wiki/Domoticz_API/JSON_URL's
Re: [90] MQTT Support
Posted: Wednesday 28 February 2018 23:26
by madrian
Hmm, you are saying that Json is faster?
Homebridge-edomoticz project recommends MQTT for speed improvement. I can confirm that, using homebridge with MQTT all devices in Domotics are reacting/updating lighting fast. You can't compare the speed with the normal HTTP+Json.
Re: [90] MQTT Support
Posted: Wednesday 07 March 2018 11:29
by ivovangastel
Hi All,
I see a strange behaviour in Domoticz MQTT implementation when i change the appearance of a selector switch in my switches tab. When i change the bulb icon to a different icon, the switch it stops sending MQTT messages. this happens also when i hide the off state, or make any other changes.
I am using stable version V3.8153 with mosquitto (both on a raspberry pi) and a self programmed ESP8266-12F (programmed with Arduino IDE)
when i create a selector switch in domoticz and use it without modifications, it sends it MQTT messages as it should. my ESP is recieving all messages (i check by printing all messages to a serial monitor of arduino). but when i change the icon of the selector switch, i do not recieve any messages of this switch anymore. but i still do recieve messages from all my other switches, heating, weather, etc.
i can make similar changes to a normal light switch, these keep sending messages as normal. so it is only related to a selector switch. could this be a bug in domoticz? if so, if this already known?
Re: [90] MQTT Support
Posted: Sunday 18 March 2018 17:40
by hichemsystem
Dear I need help on (wb-mqtt-smart bus), I have to control the Smart G4 device system through domotiz interface
https://libraries.io/github/contactless ... t-smartbus
Re: [90] MQTT Support
Posted: Monday 02 April 2018 11:37
by GCB2016
Hi everyone, semi-beginner here.
Is have a nodemcu sensor that sends Temp & Hum via MQTT. I can read the data in Node-red. Output example:
Code: Select all
{"state":"OFF","color":{"r":255,"g":255,"b":255},"brightness":255,"humidity":"49.70","motion":"motion detected","ldr":"0","temperature":"12.10","heatIndex":"5.35"}
I used the example by Bruh Automation:
https://github.com/bruhautomation/ESP-M ... ultisensor. He is using more sensors but I only need Temp & Humidity at the moment.
Everything is setup and running: Domoticz / MQTT / Node-red / nodemcu.
How do I get the sensor data into Domoticz? I want to be able to read the TEMP & HUM in Domoticz. When I simply add a output MQTT using domoticz/in the log in Domoticz says the MQTT data is invalid. I am guessing I need to do something with the msg.payload and maybe convert it via the function node? But this is where my skills end for now.
After it is sent to Domoticz I think I know how to create Dummy sensors etc.
Can you please help me out? Thanks a lot
Re: [90] MQTT Support
Posted: Monday 02 April 2018 14:40
by mrf68
@GCB2016
Maybe this little tutorial can help. It's probably not the best or most efficient solution, but it works for me.
First, your msg.payload needs to be converted into a json object. Put a json block between the mqtt node and the debug node like this:
Then look at the result in the debug window. I'll show the result of one of my temp sensors. It's looks like this:
You can then use the names to get a particular value. Make a change in the debug node by adding the name of the value you want. In my case I use svalue1 as it holds the temperature:
The next time the sensor sends its data, the debug window shows:
Create a dummy temp sensor in Domoticz and look for the idx. I created one and the devices tab shows:
Now I have to create a msg.payload that is understood by Domoticz. There are some examples in the wiki. I create a function node where I construct the message as follows:
You see that I can use the msg.payload.svalue1 from the input message and use it in a new to build msg.payload. And the idx of the dummy sensor is used. When you check the msg.payload with a debug node (remember to remove svalue1 if you use the original one) the new message looks like this:
Domoticz can't use this, it needs to be a json object, so we put another json block in between.
Now the output shows:
Domoticz can use this as an input message so feed this to the mqtt broker (to domoticz/in). After an update of the sensor you'll see this in Domoticz log:
And it gave my virtual sensor an update:
I hope this info is of any help.
Re: [90] MQTT Support
Posted: Monday 02 April 2018 14:44
by Maj75
Can anybody help me post a new topic, I have been trying to do so for 0,5hrs now and I just can't find how! Please help
Re: [90] MQTT Support
Posted: Monday 02 April 2018 19:56
by GCB2016
Hi mrf68, super thanks for your reply. I'll try it out and let you know how it went. Thanks again!
EDIT: My response disappeared... But: IT WORKS

Super happy with it. Thanks a lot!
Re: [90] MQTT Support
Posted: Wednesday 11 April 2018 7:01
by Lars65
I am trying to get a blind working, and in the function would I like to have level instead of ON/OF:
msg.topic = "domoticz/out/device/"+msg.payload.name;
if (msg.payload.svalue1 == 1) {
msg.payload = "ON"}
else
{
msg.payload = "OFF"
}
return msg;
How do I do that)
Re: [90] MQTT Support
Posted: Wednesday 09 May 2018 15:55
by gropi
@mrf68: thanks for the tutorial!
