[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

mvdbro
Posts: 24
Joined: Saturday 31 January 2015 19:28
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: [90] MQTT Support

Post by mvdbro »

When my ESP8266 boots up, i would like to update it's local status on some devices. Is it possible to request Domoticz through MQTT to send out the device status for a given idx?

Like this URL query:

Code: Select all

/json.htm?type=devices&rid=237
I can catch the MQTT JSON message from the devices, but now i have to sit and wait until there's a change...
SweetPants

Re: [90] MQTT Support

Post by SweetPants »

mvdbro wrote:When my ESP8266 boots up, i would like to update it's local status on some devices. Is it possible to request Domoticz through MQTT to send out the device status for a given idx?
I think this would imply the implementation of the MQTT 'retain' option. As soon as a device subscribes to a topic, the broker sends the last message again.
User avatar
gizmocuz
Posts: 2350
Joined: Thursday 11 July 2013 18:59
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Top of the world
Contact:

Re: [90] MQTT Support

Post by gizmocuz »

Okey new command (#2562)

Request Device Info
{"command": "getdeviceinfo", "idx": 2450 }
Quality outlives Quantity!
maxtrash
Posts: 106
Joined: Tuesday 06 August 2013 1:31
Target OS: -
Domoticz version:
Contact:

Re: [90] MQTT Support

Post by maxtrash »

SweetPants wrote:
mvdbro wrote:When my ESP8266 boots up, i would like to update it's local status on some devices. Is it possible to request Domoticz through MQTT to send out the device status for a given idx?
I think this would imply the implementation of the MQTT 'retain' option. As soon as a device subscribes to a topic, the broker sends the last message again.
yes, already thought before that it might be useful to have the retain flag set by Domoticz by default. Afaik there are no downsides to this.
see also: http://www.hivemq.com/mqtt-essentials-p ... -messages/

@gizmocuz: what do you think?

btw: I also like the "getdeviceinfo" message a lot. Retain flag can be used when your mqtt-client (or node-red) goes offline and Domoticz doesn't. If Domoticz was also rebooted or something getdeviceinfo will prove very useful as well
Last edited by maxtrash on Sunday 14 June 2015 12:30, edited 1 time in total.
User avatar
gizmocuz
Posts: 2350
Joined: Thursday 11 July 2013 18:59
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Top of the world
Contact:

Re: [90] MQTT Support

Post by gizmocuz »

I dont see a particle use for this ?

a client logs in after 15 minutes, sees only the last message (temperature update for example)

is this useful ?

this because you only receive the last message send....

but correct me if i miss something ;)
Quality outlives Quantity!
maxtrash
Posts: 106
Joined: Tuesday 06 August 2013 1:31
Target OS: -
Domoticz version:
Contact:

Re: [90] MQTT Support

Post by maxtrash »

especially for light switches or anything it could prove useful to know the current state
User avatar
gizmocuz
Posts: 2350
Joined: Thursday 11 July 2013 18:59
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Top of the world
Contact:

Re: [90] MQTT Support

Post by gizmocuz »

but you will only receive the last message (of one device)
Quality outlives Quantity!
mvdbro
Posts: 24
Joined: Saturday 31 January 2015 19:28
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: [90] MQTT Support

Post by mvdbro »

gizmocuz wrote:Okey new command (#2562)

Request Device Info
{"command": "getdeviceinfo", "idx": 2450 }
This is really usefull. Thanks for the quick development!
(Getting only the last message is not what i could use right now...)
sweetpants01
Posts: 2
Joined: Saturday 16 May 2015 22:05
Target OS: -
Domoticz version:
Contact:

Re: [90] MQTT Support

Post by sweetpants01 »

gizmocuz wrote:but you will only receive the last message (of one device)
This is right, to have retain be usefull you need separate topics per device
User avatar
gizmocuz
Posts: 2350
Joined: Thursday 11 July 2013 18:59
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Top of the world
Contact:

Re: [90] MQTT Support

Post by gizmocuz »

sweetpants01 wrote:
gizmocuz wrote:but you will only receive the last message (of one device)
This is right, to have retain be usefull you need separate topics per device
yep, and making all pretty complex... leaving it like it is now
Quality outlives Quantity!
rtenklooster
Posts: 36
Joined: Tuesday 23 December 2014 16:20
Target OS: Linux
Domoticz version:
Contact:

Re: [90] MQTT Support

Post by rtenklooster »

maxtrash wrote:making a node-red version to create devices didn't turnout to be too complicated. I have some ideas to improve on this, stay tuned
Ha, that's nice.. it's working like a charm :)
Got two temperature sensors working using this method :)
SweetPants

Re: [90] MQTT Support

Post by SweetPants »

gizmocuz wrote:
sweetpants01 wrote:
gizmocuz wrote:but you will only receive the last message (of one device)
This is right, to have retain be usefull you need separate topics per device
yep, and making all pretty complex... leaving it like it is now
Fixed my problem using below node-red flow to generate new topic based on dtype + idx and switch on retain when sending to the broker

Code: Select all

[{"id":"86f0c28c.790f4","type":"mqtt-broker","broker":"192.168.24.250","port":"1883","clientid":""},{"id":"cdb79ec2.d268e","type":"mqtt in","name":"Domoticz Events","topic":"domoticz/out","broker":"86f0c28c.790f4","x":110.53848266601562,"y":42.69232940673828,"z":"33dd3d28.a2ce22","wires":[["d7f0bfbd.a8a99"]]},{"id":"d7f0bfbd.a8a99","type":"function","name":"Convert JSON to Topic","func":"var msgIn = JSON.parse(msg.payload);\n\nvar msg = {};\n\n// Temp + Humidity -> Temp_Humidity\n// \nvar dtype = msgIn.dtype.replace(/\\s\\+\\s/,'_');\nvar idx = msgIn.idx;\n\nmsg.topic = \"domoticz/device/\" + dtype + \"/\" + idx;\nmsg.payload = msgIn;\n\nreturn msg;","outputs":1,"valid":true,"x":293.9229736328125,"y":127.84617614746094,"z":"33dd3d28.a2ce22","wires":[["5eb6c125.d34238"]]},{"id":"5eb6c125.d34238","type":"mqtt out","name":"Domoticz Out","topic":"","qos":"0","retain":"true","broker":"86f0c28c.790f4","x":485.76922607421875,"y":42.538475036621094,"z":"33dd3d28.a2ce22","wires":[]}]
kylegordon
Posts: 26
Joined: Thursday 31 October 2013 23:58
Target OS: Linux
Domoticz version: Trunk
Contact:

Re: [90] MQTT Support

Post by kylegordon »

Evening all,

I think I may have found a Domoticz bug whilst fiddling with my node-red flow.

When node-red failed to calculate something, it generated NaN instead of a number. This got pushed to Domoticz as {"idx":46, "nvalue": 0, "svalue":"NaN;0;0"}, and subsequently all my temperature sensors vanished from Domoticz. Also, all my devices vanished from the hardware list, but switches were still operational.

Had to restore from backup to get the DB back into a working state. Can others replicate this?

You can use this to publish it at the command line...

mosquitto_pub -h yourserver.com -t domoticz/in -m '{"idx":46, "nvalue": 0, "svalue":"NaN;0;0"}'

This is the flow...

Code: Select all

[{"id":"66a92c76.9956d4","type":"mqtt-broker","broker":"homeauto.vpn.glasgownet.com","port":"1883","clientid":""},{"id":"b8f4e8e2.470b18","type":"function","name":"Average temp","func":"numdevices = 7;\n\ntotal = parseFloat(context.global.masterbedtemp) + \nparseFloat(context.global.guestbedtemp) + \nparseFloat(context.global.geeklofttemp) + \nparseFloat(context.global.kitchentemp) +\nparseFloat(context.global.studytemp) +\nparseFloat(context.global.livingroomtemp) +\nparseFloat(context.global.bottomhallwaytemp);\n\nconsole.log(total)\nmsg.payload = (total / numdevices).toFixed(2);\n//msg.payload = msg.payload + 0.5;\n\nreturn msg;","outputs":1,"valid":true,"x":977,"y":124,"z":"d5caab33.2a3558","wires":[["ec3d1fae.13c2e","b75445a3.48abb8"]]},{"id":"ec3d1fae.13c2e","type":"debug","name":"Temperature","active":true,"console":"false","complete":"false","x":1254,"y":116,"z":"d5caab33.2a3558","wires":[]},{"id":"a1ae61bd.5e51a","type":"inject","name":"Repeat","topic":"","payload":"","payloadType":"none","repeat":"300","crontab":"","once":true,"x":561,"y":71,"z":"d5caab33.2a3558","wires":[["b8f4e8e2.470b18"]]},{"id":"a4bce105.5b432","type":"mqtt out","name":"House Average Temperature Domoticz","topic":"domoticz/in","qos":"","retain":"","broker":"66a92c76.9956d4","x":1417,"y":194,"z":"d5caab33.2a3558","wires":[]},{"id":"b75445a3.48abb8","type":"function","name":"JSONify","func":"msg.payload = '{\"idx\":46, \"nvalue\": 0, \"svalue\":\"' + msg.payload + ';0;0\"}';\n\nreturn msg;","outputs":1,"valid":true,"x":1163,"y":196,"z":"d5caab33.2a3558","wires":[["a4bce105.5b432"]]},{"id":"a2d8a9ac.5d2758","type":"mqtt in","name":"Domoticz out","topic":"domoticz/out","broker":"66a92c76.9956d4","x":548,"y":244,"z":"d5caab33.2a3558","wires":[["39558984.c6aa76"]]},{"id":"39558984.c6aa76","type":"function","name":"Select payloads","func":"msg.parameters = JSON.parse(msg.payload);\n//console.log(msg.parameters);\nmsg.topic = \"debug output\";\nmsg.payload = msg.parameters.svalue1;\n\nif ( msg.parameters.idx == 35) {\n  context.global.kitchentemp = msg.parameters.svalue1;\n}\n\nif ( msg.parameters.idx == 116) {\n  context.global.masterbedtemp = msg.parameters.svalue1;\n}\n\nif ( msg.parameters.idx == 117) {\n  context.global.geeklofttemp = msg.parameters.svalue1;\n}\n\nif ( msg.parameters.idx == 122) {\n  context.global.guestbedtemp = msg.parameters.svalue1;\n}\n\nif ( msg.parameters.idx == 132) {\n  context.global.livingroomtemp = msg.parameters.svalue1;\n}\n\nif ( msg.parameters.idx == 133) {\n  context.global.livingroomtemp = msg.parameters.svalue1;\n}\n\nif ( msg.parameters.idx == 134) {\n  context.global.bottomhallwaytemp = msg.parameters.svalue1;\n}\n\nreturn msg;","outputs":"2","valid":true,"x":763,"y":172,"z":"d5caab33.2a3558","wires":[["b8f4e8e2.470b18"],["d5306da.f2acf9"]]},{"id":"d5306da.f2acf9","type":"function","name":"Average humidity","func":"numdevices = 6;\n\ntotal = parseFloat(context.global.masterbedhum) + \nparseFloat(context.global.guestbedhum) + \nparseFloat(context.global.geeklofthum) + \nparseFloat(context.global.studyhum) +\nparseFloat(context.global.livingroomhum) +\nparseFloat(context.global.bottomhallwayhum);\n\nmsg.payload = (total / numdevices).toFixed(2);\n//msg.payload = msg.payload + 0.5;\n\nreturn msg;","outputs":1,"valid":true,"x":976,"y":258,"z":"d5caab33.2a3558","wires":[[]]}]
1 x HP DL380 & KVM
1 x RPI
1 x RFXtrx433 V78
11 x LWRF Switches
1 x LIFX
2 x Echo Dots
6 x Byron PIRs
2 x Nexa PIRs
2 x Kodi
2 x ESP8266 MQTT
1 x GPIO/MQTT PIR
1 x GPIO(PWM)/MQTT LEDs
1 x GPIO(SPI)/MQTT LPD6803 LEDs
Lots of Node-Red glue
User avatar
gizmocuz
Posts: 2350
Joined: Thursday 11 July 2013 18:59
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Top of the world
Contact:

Re: [90] MQTT Support

Post by gizmocuz »

dont think this is a domoticz bug
Quality outlives Quantity!
kylegordon
Posts: 26
Joined: Thursday 31 October 2013 23:58
Target OS: Linux
Domoticz version: Trunk
Contact:

Re: [90] MQTT Support

Post by kylegordon »

I was able to recreate it in a clean build of a Debian 7.8 machine, using r2572.

Create a Dummy device, and then create dummy Temperature sensors (not Temp+Hum, or Temp+Hum+Baro). Add the temperature devices so that they appear in the Temperatures tab.

Create a MQTT device, and get connected.

Send mosquitto_pub -h localhost -t domoticz/in -m '{"idx":1, "nvalue": 0, "svalue":"60"}' - it should update the temperature.

Send mosquitto_pub -h localhost -t domoticz/in -m '{"idx":1, "nvalue": 0, "svalue":"NaN"}' - refresh the temperature tab and the temperature sensors will be gone. Equally, the devices list will be empty, and any new devices will refuse to display.

It seems to be particular to the string 'NaN'. Putting 'foo' in there instead doesn't cause the issue to arise.
1 x HP DL380 & KVM
1 x RPI
1 x RFXtrx433 V78
11 x LWRF Switches
1 x LIFX
2 x Echo Dots
6 x Byron PIRs
2 x Nexa PIRs
2 x Kodi
2 x ESP8266 MQTT
1 x GPIO/MQTT PIR
1 x GPIO(PWM)/MQTT LEDs
1 x GPIO(SPI)/MQTT LPD6803 LEDs
Lots of Node-Red glue
User avatar
gizmocuz
Posts: 2350
Joined: Thursday 11 July 2013 18:59
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Top of the world
Contact:

Re: [90] MQTT Support

Post by gizmocuz »

its just showing '0' here....

but i find it the users responsibility to provide valid data, like not putting a electric counter value in a lux sensor kind of thing...
Quality outlives Quantity!
kylegordon
Posts: 26
Joined: Thursday 31 October 2013 23:58
Target OS: Linux
Domoticz version: Trunk
Contact:

Re: [90] MQTT Support

Post by kylegordon »

How odd. I too get 0 when I put 'foo' in, but it dies with 'NaN'

I agree though, the user shouldn't be sending a string into a system that's expecting a number. However, I raised it as 'NaN' is Javascripts special way of saying 'your calculation got messed up', and that's what Node-Red will send out. It also appears to corrupt the database, which is hardly ideal :-)

This is what I'm using to catch it in node-red

Code: Select all

if(isNaN(total))
{
    console.log("returning null");
    //dataStr='NAN';
    return null;
}
I can record a screencast if you wish to see the bug happening :-)
1 x HP DL380 & KVM
1 x RPI
1 x RFXtrx433 V78
11 x LWRF Switches
1 x LIFX
2 x Echo Dots
6 x Byron PIRs
2 x Nexa PIRs
2 x Kodi
2 x ESP8266 MQTT
1 x GPIO/MQTT PIR
1 x GPIO(PWM)/MQTT LEDs
1 x GPIO(SPI)/MQTT LPD6803 LEDs
Lots of Node-Red glue
parrotface
Posts: 36
Joined: Monday 02 February 2015 15:50
Target OS: Linux
Domoticz version:
Location: Cheshire England
Contact:

Re: [90] MQTT Support

Post by parrotface »

I can receive a MQTT message in a python script each time a switch/contact is made on ESP8266.
I now need the python script to display the state of this switch/contact in a dummy domoticz sensor in the dashboard.
I don't know where to start, I can use a python script to publish a message and switch a light on via the ESP8266 OK, but I now need to know the switch status in return.
Thanks
maxtrash
Posts: 106
Joined: Tuesday 06 August 2013 1:31
Target OS: -
Domoticz version:
Contact:

Re: [90] MQTT Support

Post by maxtrash »

parrotface wrote:I can receive a MQTT message in a python script each time a switch/contact is made on ESP8266.
I now need the python script to display the state of this switch/contact in a dummy domoticz sensor in the dashboard.
I don't know where to start, I can use a python script to publish a message and switch a light on via the ESP8266 OK, but I now need to know the switch status in return.
Thanks
not sure I completely get it but you have a board with an ESP8266 which understands MQTT using some python? If so you could adjust the python to accept messages in the JSON format as sent by Domoticz. And your python scripts should subscribe to the correct domoticz MQTT topic (/Domoticz/out)

another solution would be to keep the message format and topic that the python script subscribes in tact. In that case you would need something which translates (republishes) messages coming from domoticz to whatever is needed.

is this of any help?
leoncornelissen
Posts: 24
Joined: Monday 27 April 2015 20:33
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8258
Location: Netherlands
Contact:

Re: [90] MQTT Support

Post by leoncornelissen »

I have a question regarding the mqtt config..
I have installed domoticz on my synology Nas and I also installed a mosquito package. I read that there is a mqtt client in the domoticz so I searched how to get this up and running. I mainly saw an install guide on the wiki but there is a native client now so no need to follow the wiki

I found a thread about adding a new hardware device so I added the mqtt client gateway with lan interface and with the server ip and port. But now my log is full of:
* mqtt connecting to 193.168.1.33:1883
* error mqtt: connection failed, restarting

I tried it with http in front and also 127.0.0.1 but with no luck,
Can somebody help me out.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest