[90] MQTT Support

Use this forum to discuss possible implementation of a new feature before opening a ticket.
A developer shall edit the topic title with "[xxx]" where xxx is the id of the accompanying tracker id.
Duplicate posts about the same id. +1 posts are not allowed.

Moderators: leecollings, remb0

User avatar
mvveelen
Posts: 678
Joined: Friday 31 October 2014 10:22
Target OS: NAS (Synology & others)
Domoticz version: Beta
Location: Hoorn, The Netherlands
Contact:

Re: [90] MQTT Support

Post 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 ?
RPi3b+/RFXCOM rfxtrx433E/Shelly/Xiaomi Gateway/Philips HUE Lights/Atag Zone One/2 SunnyBoy inverters/AirconWithMe/P1 smartmeter/Domoticz latest Beta
SweetPants

Re: [90] MQTT Support

Post 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).
User avatar
mvveelen
Posts: 678
Joined: Friday 31 October 2014 10:22
Target OS: NAS (Synology & others)
Domoticz version: Beta
Location: Hoorn, The Netherlands
Contact:

Re: [90] MQTT Support

Post 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?
RPi3b+/RFXCOM rfxtrx433E/Shelly/Xiaomi Gateway/Philips HUE Lights/Atag Zone One/2 SunnyBoy inverters/AirconWithMe/P1 smartmeter/Domoticz latest Beta
SweetPants

Re: [90] MQTT Support

Post 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
User avatar
mvveelen
Posts: 678
Joined: Friday 31 October 2014 10:22
Target OS: NAS (Synology & others)
Domoticz version: Beta
Location: Hoorn, The Netherlands
Contact:

Re: [90] MQTT Support

Post 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......
RPi3b+/RFXCOM rfxtrx433E/Shelly/Xiaomi Gateway/Philips HUE Lights/Atag Zone One/2 SunnyBoy inverters/AirconWithMe/P1 smartmeter/Domoticz latest Beta
SweetPants

Re: [90] MQTT Support

Post by SweetPants »

add option -r into your script and voilla
User avatar
mvveelen
Posts: 678
Joined: Friday 31 October 2014 10:22
Target OS: NAS (Synology & others)
Domoticz version: Beta
Location: Hoorn, The Netherlands
Contact:

Re: [90] MQTT Support

Post 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
RPi3b+/RFXCOM rfxtrx433E/Shelly/Xiaomi Gateway/Philips HUE Lights/Atag Zone One/2 SunnyBoy inverters/AirconWithMe/P1 smartmeter/Domoticz latest Beta
welby
Posts: 36
Joined: Monday 20 November 2017 3:24
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Australia
Contact:

Re: [90] MQTT Support

Post 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&param=switchlight&idx=99&switchcmd=On

Or am i totally confusing myself and need to read more about it?
SweetPants

Re: [90] MQTT Support

Post 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&param=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.
welby
Posts: 36
Joined: Monday 20 November 2017 3:24
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Australia
Contact:

Re: [90] MQTT Support

Post 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?
SweetPants

Re: [90] MQTT Support

Post 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
madrian
Posts: 231
Joined: Saturday 27 August 2016 1:18
Target OS: -
Domoticz version:
Contact:

Re: [90] MQTT Support

Post 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.
ivovangastel
Posts: 8
Joined: Tuesday 31 January 2017 18:05
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8153
Location: Netherlands
Contact:

Re: [90] MQTT Support

Post 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?
hichemsystem
Posts: 4
Joined: Sunday 18 March 2018 17:17
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: [90] MQTT Support

Post 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
GCB2016
Posts: 33
Joined: Thursday 10 November 2016 22:13
Target OS: Raspberry Pi / ODroid
Domoticz version: V3.9208
Location: Rotterdam
Contact:

Re: [90] MQTT Support

Post 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
Attachments
Screen Shot 2018-04-02 at 11.29.19.png
Screen Shot 2018-04-02 at 11.29.19.png (91 KiB) Viewed 3999 times
mrf68

Re: [90] MQTT Support

Post 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:

Image

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:

Image

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:

Image


The next time the sensor sends its data, the debug window shows:

Image

Create a dummy temp sensor in Domoticz and look for the idx. I created one and the devices tab shows:

Image

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:

Image

Image

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:

Image

Domoticz can't use this, it needs to be a json object, so we put another json block in between.

Image

Now the output shows:

Image

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:

Image

And it gave my virtual sensor an update:

Image

I hope this info is of any help.
Maj75
Posts: 5
Joined: Monday 02 April 2018 13:23
Target OS: Windows
Domoticz version:
Contact:

Re: [90] MQTT Support

Post 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
GCB2016
Posts: 33
Joined: Thursday 10 November 2016 22:13
Target OS: Raspberry Pi / ODroid
Domoticz version: V3.9208
Location: Rotterdam
Contact:

Re: [90] MQTT Support

Post 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!
Lars65
Posts: 41
Joined: Thursday 14 July 2016 20:49
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: [90] MQTT Support

Post 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)
gropi
Posts: 13
Joined: Thursday 26 April 2018 20:03
Target OS: Linux
Domoticz version: 4.11786
Location: Germany
Contact:

Re: [90] MQTT Support

Post by gropi »

@mrf68: thanks for the tutorial! :D
Domoticz Version: 2023.1
Platform: Openmediavault 6; Docker; HP Microserver Gen8
Plugin/Hardware: MySensors Gateway; Philips HUE Bridge; OMG; ZigBee;
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest