dzVents new feature: add MQTT feature

Easy to use, 100% Lua-based event scripting framework.
Post Reply
User avatar
emme
Posts: 936
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi
Domoticz version: latest
Location: Milano, Italy
Contact:

dzVents new feature: add MQTT feature

Post by emme » Tuesday 31 July 2018 11:19

Ciao,

I'm playing with MQTT and I see dZ publish only devices and sensors to MQTT, no variables, no other status message

I have few NodeRed flows that could be interested in receiving messages from MQTT to... do stuff...
a kind of these messages should came from Domoticz as a reasult of a scritp...

Instead of adding a os.execute with the mosquitto_pub (which doesn't work for me since the repository seems to be broken), it would be nice if dzVents could send MQTT telegram as function
something like
sendMqtt(topic, payload)

could it be an idea?
ciao
M
The most dangerous phrase in any language is:
"We always done this way"

Charley
Posts: 29
Joined: Tuesday 06 March 2018 13:20
Target OS: Raspberry Pi
Domoticz version: V3.9030
Location: Netherlands
Contact:

Re: dzVents new feature: add MQTT feature

Post by Charley » Sunday 05 August 2018 9:01

With nodered you can use the published events from MQTT by setting the topic to domoticz/out.
Then you need a function node to filter what event you like to control. I use extensive Nodered flows to control several devices.

Here is a flow to start how you could collect data from MQTT with domoticz/out

Code: Select all


[{"id":"cd222b8e.945fb8","type":"mqtt in","z":"150731a6.9b7e5e","name":"MQTT","topic":"domoticz/out","qos":"1","broker":"fc68fc78.a14ae","x":150,"y":260,"wires":[["cbef9f4a.ea47d"]]},{"id":"cbef9f4a.ea47d","type":"json","z":"150731a6.9b7e5e","name":"","property":"payload","action":"","pretty":false,"x":290,"y":260,"wires":[["70c33c55.72fb64","b259e07.59b2a2","b6de791b.57af08","eebb88a4.b5a368"]]},{"id":"b259e07.59b2a2","type":"switch","z":"150731a6.9b7e5e","name":"stype","property":"payload.stype","propertyType":"msg","rules":[{"t":"eq","v":"kWh","vt":"str"},{"t":"eq","v":"LaCrosse TX3","vt":"str"},{"t":"eq","v":"Sound Level","vt":"str"},{"t":"eq","v":"Weather Station","vt":"str"},{"t":"eq","v":"Electric","vt":"str"}],"checkall":"true","repair":false,"outputs":5,"x":450,"y":260,"wires":[["a7edc925.0e8878"],["69f16424.05c39c"],["862d4453.3427f8"],["e8720982.29a618","69f16424.05c39c"],["a7edc925.0e8878"]]},{"id":"fc68fc78.a14ae","type":"mqtt-broker","z":"","name":"","broker":"192.168.178.188","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthRetain":"false","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]


Here is a flow to send data to MQTT

Code: Select all


[{"id":"de09d7bd.29a8e8","type":"inject","z":"75ec7863.1959e8","name":"","topic":"light","payload":"Off","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":280,"wires":[["2f822c74.4153d4"]]},{"id":"21317f95.a289c","type":"inject","z":"75ec7863.1959e8","name":"","topic":"light","payload":"On","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":240,"wires":[["2f822c74.4153d4"]]},{"id":"fa3a4b82.b07b88","type":"function","z":"75ec7863.1959e8","name":"","func":"var mqtt = { command: \"setcolbrightnessvalue\", idx: 59, hex: msg.payload, brightness: 90, iswhite: false };\n\nvar newMsg = {};\nnewMsg.payload = {};\nnewMsg.payload = mqtt;\nreturn newMsg\n","outputs":1,"noerr":0,"x":410,"y":120,"wires":[["3c7b98c0.c99a28","b170a429.7792e8"]]},{"id":"3c7b98c0.c99a28","type":"mqtt out","z":"75ec7863.1959e8","name":"","topic":"domoticz/in","qos":"1","retain":"true","broker":"fc68fc78.a14ae","x":710,"y":120,"wires":[]},{"id":"b170a429.7792e8","type":"debug","z":"75ec7863.1959e8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":690,"y":260,"wires":[]},{"id":"2f822c74.4153d4","type":"function","z":"75ec7863.1959e8","name":"","func":"// 59 is a RGB strip, 28 is a normal ligth\nif (msg.topic==\"rgb\"){\n    var mqtt = { command: \"switchlight\", idx: 59, switchcmd: msg.payload };\n} else if (msg.topic==\"light\"){\n    var mqtt={ command: \"switchlight\", idx: 28, switchcmd: msg.payload }\n}\n\nvar newMsg = {};\nnewMsg.payload = {};\nnewMsg.payload = mqtt;\nreturn newMsg\n","outputs":1,"noerr":0,"x":410,"y":260,"wires":[["3c7b98c0.c99a28","b170a429.7792e8"]]},{"id":"38d76626.40560a","type":"inject","z":"75ec7863.1959e8","name":"set color","topic":"","payload":"FF0B85","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":140,"y":60,"wires":[["fa3a4b82.b07b88"]]},{"id":"5e1d95e8.8198ac","type":"inject","z":"75ec7863.1959e8","name":"","topic":"rgb","payload":"Off","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":140,"wires":[["2f822c74.4153d4"]]},{"id":"dbea6765.a9ad18","type":"inject","z":"75ec7863.1959e8","name":"","topic":"rgb","payload":"On","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":100,"wires":[["2f822c74.4153d4"]]},{"id":"fc68fc78.a14ae","type":"mqtt-broker","z":"","name":"","broker":"192.168.178.188","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthRetain":"false","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]
If you want to collect your uservariables this flow could be a start. I use it to set timers for lights, wakeup alarm and rollershutters.

Code: Select all

[{"id":"43534cff.c597d4","type":"inject","z":"65fdffa8.dd87d","name":"","topic":"button","payload":"17","payloadType":"num","repeat":"60","crontab":"","once":true,"onceDelay":"1","x":110,"y":240,"wires":[["595da7f.2494358"]]},{"id":"595da7f.2494358","type":"http request","z":"65fdffa8.dd87d","name":"","method":"GET","ret":"obj","url":"http://192.168.178.188:8088/json.htm?param=getuservariables&type=command","tls":"","x":270,"y":240,"wires":[["c8ac953a.354328"]]},{"id":"c8ac953a.354328","type":"function","z":"65fdffa8.dd87d","name":"Select the vars","func":"//loop the payload we don't need all\nfor (var i=0;i<msg.payload.result.length;i++){\n   //set the received minutes to 24h time format\n   var newMins = parseInt(msg.payload.result[i].Value);\n   var cMins=newMins;\n   var newHrs=Math.floor(newMins/60);\n   // the number of hours is too big set it to 7 hours\n   if (newHrs>23){newHrs=7}\n   newMins = newMins - (60 * newHrs)\n   // 12:34 \n   newTime=(newHrs<10?\"0\":'') + newHrs + \":\" +(newMins<10?\"0\":'') + newMins\n   //extract the name\n   var tmpName = msg.payload.result[i].Name\n   if (tmpName==\"LichtWoonkamer\" ||\n       tmpName==\"LichtSlaapkamer\" ||\n       tmpName==\"Wekker\" ||\n       tmpName==\"RolluikOpenSlaapkamer\" ||\n       tmpName==\"LichtKeuken\" ||\n       tmpName==\"LichtBuiten\" ||\n       tmpName==\"RolluikOpenLogeer\" ||\n       tmpName==\"RolluikKantoor\") {\n      \n      // now send them one by one \n      newMsg={payload: newTime, topic:tmpName, cMin:cMins}\n      node.send(newMsg)\n   \n           \n    }\n}","outputs":1,"noerr":0,"x":440,"y":240,"wires":[["49a6afb7.ff702"]]},{"id":"49a6afb7.ff702","type":"switch","z":"65fdffa8.dd87d","name":"","property":"topic","propertyType":"msg","rules":[{"t":"eq","v":"LichtWoonkamer","vt":"str"},{"t":"eq","v":"LichtSlaapkamer","vt":"str"},{"t":"eq","v":"Wekker","vt":"str"},{"t":"eq","v":"RolluikOpenSlaapkamer","vt":"str"},{"t":"eq","v":"LichtKeuken","vt":"str"},{"t":"eq","v":"LichtBuiten","vt":"str"},{"t":"eq","v":"RolluikOpenLogeer","vt":"str"},{"t":"eq","v":"RolluikKantoor","vt":"str"}],"checkall":"true","repair":false,"outputs":8,"x":590,"y":240,"wires":[["5f26157.dd391ec","2efab279.5e1a9e"],["50904e59.f0861"],["6b809640.6800a8"],["d377196b.ce7c48"],["9d3a9264.a1b11"],["c10b99b3.986f18"],["2dbe37eb.a28cb8"],["c117573d.c1b348"]]}]

My Domoticz, MQTT and Nodered are all on the same Raspberry PI 3+ (Nodered and MQTT are password protected)

User avatar
emme
Posts: 936
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi
Domoticz version: latest
Location: Milano, Italy
Contact:

Re: dzVents new feature: add MQTT feature

Post by emme » Thursday 16 August 2018 17:03

that doesn't work for variables... :(
The most dangerous phrase in any language is:
"We always done this way"

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests