Page 10 of 19

Re: [90] MQTT Support

Posted: Sunday 14 June 2015 11:57
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...

Re: [90] MQTT Support

Posted: Sunday 14 June 2015 12:07
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.

Re: [90] MQTT Support

Posted: Sunday 14 June 2015 12:19
by gizmocuz
Okey new command (#2562)

Request Device Info
{"command": "getdeviceinfo", "idx": 2450 }

Re: [90] MQTT Support

Posted: Sunday 14 June 2015 12:25
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

Re: [90] MQTT Support

Posted: Sunday 14 June 2015 12:29
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 ;)

Re: [90] MQTT Support

Posted: Sunday 14 June 2015 12:32
by maxtrash
especially for light switches or anything it could prove useful to know the current state

Re: [90] MQTT Support

Posted: Sunday 14 June 2015 12:33
by gizmocuz
but you will only receive the last message (of one device)

Re: [90] MQTT Support

Posted: Sunday 14 June 2015 12:40
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...)

Re: [90] MQTT Support

Posted: Sunday 14 June 2015 14:15
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

Re: [90] MQTT Support

Posted: Sunday 14 June 2015 14:32
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

Re: [90] MQTT Support

Posted: Sunday 14 June 2015 17:49
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 :)

Re: [90] MQTT Support

Posted: Sunday 14 June 2015 18:38
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":[]}]

Re: [90] MQTT Support

Posted: Tuesday 16 June 2015 23:38
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":[[]]}]

Re: [90] MQTT Support

Posted: Wednesday 17 June 2015 7:55
by gizmocuz
dont think this is a domoticz bug

Re: [90] MQTT Support

Posted: Wednesday 17 June 2015 16:35
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.

Re: [90] MQTT Support

Posted: Wednesday 17 June 2015 17:19
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...

Re: [90] MQTT Support

Posted: Wednesday 17 June 2015 17:31
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 :-)

Re: [90] MQTT Support

Posted: Tuesday 23 June 2015 0:52
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

Re: [90] MQTT Support

Posted: Tuesday 23 June 2015 22:11
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?

Re: [90] MQTT Support

Posted: Saturday 11 July 2015 15:08
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.