Page 1 of 2

NodeJS Module: node-domoticz-mqtt

Posted: Monday 25 January 2016 0:27
by nayr
I created a NodeJS Module for interfacing with Domoticz directly via MQTT.. It has some sanity checks to keep Domoticz happy and all the basic functions you need to start interfacing your own hardware with Domoticz quickly and easily.

Here is the npm project: https://www.npmjs.com/package/node-domoticz-mqtt
Here is the code: https://github.com/nayrnet/node-domoticz-mqtt

What is the MQTT Interface? Its identical to the JSON HTTP GET interface with these important differences:
  • Persistent Connection - Quick Response without setting up full blown HTTP connections for every update. Great if your changing lots of devices.
  • Bi-Directional - Get instant status updates without polling for changes, no need to call your app via HTTP/Script
  • MQTT Broker provides simple security for devices, with crypto support. great if your locking down the Web interface for security reasons.
  • Your NodeJS app will have nearly all the benefits of native hardware
Why NodeJS? Well tons of hardware is already supported by NodeJS, for example: This solution does not require node-red or any of that, this lets you develop NodeJS applications interacting directly with Domoticz. All you need to do is install mosquito locally and add and configure the MQTT Hardware within Domoticz. Your now ready to start using this module.

Requires:
  • MQTT Broker (apt-get install mosquitto)
  • NodeJS and NPM (apt-get install nodejs npm)
  • Domoticz w/MQTT Hardware Interface
To install:

Code: Select all

npm install node-domoticz-mqtt
Support, Feedback, Contributions, etc should all be done in this thread or my github repo.. thanks and enjoy!

Ive got several NodeJS Applications that will be using this module, and more planned.. I should be able to finally add BeagleBone Black support to Domoticz soon!

Re: NodeJS Module: node-domoticz-mqtt

Posted: Monday 25 January 2016 10:08
by nayr
small version update released, 1.1.0

Changes:
  • Small Bugfixes for Switches, beware Virtual Switches do not support 100 steps.. only 0-15, but I am working getting this fixed upstream.
  • Callback for connect and error, now you can ensure you are connected before publishing data.
  • All the IDX provided in Options are now automatically requested upon connect, dont have to do it your self. You'll see a data callback with the info you need shortly after connecting.

Re: NodeJS Module: node-domoticz-mqtt

Posted: Sunday 15 May 2016 0:14
by jrich523
Looks great, but, i have to say im a little lost. If you helped walk me through it id be happy to write up the tutorial.
One of the things throwing me is what belongs where. I have Domoticz running on a seperate PC so for your setup you dont really indicate whats done on the server vs for the "device". As you know from the other forum im just trying to start with something super basic (temp, DHT or 1wire would be fine)

Re: NodeJS Module: node-domoticz-mqtt

Posted: Sunday 15 May 2016 17:45
by nayr
Domoticz has native support for DHT and 1Wire sensors.. dont use this library to integrate those.

Re: NodeJS Module: node-domoticz-mqtt

Posted: Monday 16 May 2016 10:34
by gizmocuz
Hi Nayr,

Just found this thread....

Domoticz has native mqtt support.... would it not be better to extend/enrich domoticz's implementation ?

Re: NodeJS Module: node-domoticz-mqtt

Posted: Monday 16 May 2016 16:43
by nayr
Hey Gizmocuz, of course.. this module is simply a bunch of pre-defined functions that are ran against Domoticz's MQTT Implementation (domoticz/in) with a bit of error checking to prevent you from crashing domoticz's MQTT module with invalid data (NaN)..

its just a library to load and talk to domoticz/in without all the MQTT/JSON formatting to worry about, simple functions make the calls for you.. basically allows NodeJS to be used like Lua/Python thanks to MQTT.. nothing is stoping you from talking to domoticz/in directly.

Re: NodeJS Module: node-domoticz-mqtt

Posted: Tuesday 17 May 2016 1:20
by jrich523
I guess I was mostly trying the temp because it was one of the most basic sensors, not so much that this was the end goal. Also I run my Dom on a laptop along with some other things, so id still need to run the temp on either a beagle/pi or something more basic like an arduino, using mysensors (which i havent had good luck with so far)

really the bottom line was an end to end example of how to get data from a beaglebone to dom

Re: NodeJS Module: node-domoticz-mqtt

Posted: Tuesday 17 May 2016 1:28
by nayr
for temp through this library you'd create a dummy sensor with idx of 101, then use: domoticz.device(101,0,"20") to set it to 20C

// Publish uDevice: nvalue = int, svalue = string
domoticz.device(idx,nvalue,svalue);

same as you'd find through the JSON API here: https://www.domoticz.com/wiki/Domoticz_ ... .2Fsensors

Re: NodeJS Module: node-domoticz-mqtt

Posted: Monday 30 May 2016 13:03
by woody4165
Hi

I'm trying to undertand if MQTT is better than other solution with an ESP8266 with ESPEasy fw on it.
What I'm trying to reach is, a part setting On and Off GPIOs on ESP, having ESP to send "I'm alive" messages to Domoticz and with the help of Rules on ESPEasy return GPIOs status to Domoticz.

Hope this will work better than what I'm doing right now with HTTP.

What do you mean when you say that NodeRed is not anymore needed with node-domoticz-mqtt? Can I uninstall it from Domoticz?

How can I implement security (crypto and other) ?

Thanks

Re: NodeJS Module: node-domoticz-mqtt

Posted: Monday 30 May 2016 22:31
by jrich523
So I was able to setup the virtual device just fine (idx 52) and then used PowerShell to test sending data to it

Code: Select all

Invoke-WebRequest -Uri "http://192.168.11.98:8080/json.htm?type=command&param=udevice&idx=52&nvalue=0&svalue=21;33;1"
and it returned a status of OK and title of "Update Device" and I was able to see the data reflected in domoticz

I then went over to the beaglebone and created a simple script, I wont bother showing much of it, since its almost exactly youre example code, but i will show the output,

domoticz/in: {"command":"udevice","idx":52,"nvalue":0,"svalue":"21;33;1"}

Since this is from the On Data, i assume this is the info that dom received jsut fine. I also tossed in an On Error but nothing was spewed, however when I look in domoticz it never updates the sensor.

Re: NodeJS Module: node-domoticz-mqtt

Posted: Thursday 02 June 2016 14:46
by sj3fk3
Wouldn't it be nice to make it node-red node?
http://nodered.org/

Re: NodeJS Module: node-domoticz-mqtt

Posted: Monday 09 January 2017 3:56
by rlust
Trying to understand how to use this.
I am reading temperature and humidity on a ESP8266. The ESP code allows me to directly post a switch state, but cannot send temp & humidity data to domoticz . Does this allow me to use MQTT non json topics to domoticz?

Re: NodeJS Module: node-domoticz-mqtt

Posted: Thursday 12 January 2017 4:23
by rlust
I have used node-red to create a flow that posts the following to:
domoticz/in{"idx":59,"nvalue":0,"svalue":"70.0"}

I am getting this data from an ESP8266 and posting via MQTT
I get a reading in domoticz but the reading is being treated as Celsius.
I have tried 1 for the nvalue but still the same result.

Any ideas on how to fix the conversion?

Re: NodeJS Module: node-domoticz-mqtt

Posted: Wednesday 18 January 2017 20:24
by seblang
Hi

I am still trying to use custom page with mqtt.
I am on windows and I have try mosquitto but mosquitto do not accept websocket!
I have try Hivemq!
Somebody use it? who can help me?

Re: NodeJS Module: node-domoticz-mqtt

Posted: Thursday 30 November 2017 15:54
by salvacalatayud
sj3fk3 wrote: Thursday 02 June 2016 14:46 Wouldn't it be nice to make it node-red node?
http://nodered.org/
+1

I'm not good at all in programing, so a graphic interface would help a lot

Re: NodeJS Module: node-domoticz-mqtt

Posted: Thursday 12 July 2018 13:33
by Charley
I made some flows in Node-RED to recieve sensor data (temperature and so) from Domoticz.
In node red i only recieve object from domoticz/out

There would be a big improvement if Domoticz publishes in the MQTT format like:

domoticz/out/switches
domoticz/out/sensors
domoticz/out/uservars

So far I have not been able to publish anything to Domoticz through code-red

Re: NodeJS Module: node-domoticz-mqtt

Posted: Thursday 12 July 2018 13:47
by emme
I had a similar request
http://domoticz.com/forum/viewtopic.php ... tt#p185829
implement a .topic option in the mqtt telegram to better address messages :P

Re: NodeJS Module: node-domoticz-mqtt

Posted: Thursday 12 July 2018 14:45
by Charley
I am able to switch on a light using node-RED that works perfect however an RGBW strip sometimes switches on and immediately off again. But most of the times is just stays off.

The flow I made is working (there is a JSON error you can dismiss "Unexpected token c in JSON at position 0") just change the idx in the functions

Code: Select all

[{"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":"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 newMsg = { command: \"setcolbrightnessvalue\", idx: 59, hex: msg.payload, brightness: 90, iswhite: false };\nreturn {payload: 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 newMsg = { command: \"switchlight\", idx: 59, switchcmd: msg.payload };\n} else if (msg.topic==\"light\"){\n    var newMsg={ command: \"switchlight\", idx: 28, switchcmd: msg.payload }\n}\nreturn {payload: newMsg}\n","outputs":1,"noerr":0,"x":410,"y":260,"wires":[["3c7b98c0.c99a28","b170a429.7792e8"]]},{"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":"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":""}]
The debug node shows the command-object I am publishing to Domoticz/in.
Does anyone have any idea how to keep the RGBW lights on after injecting the command?

Re: NodeJS Module: node-domoticz-mqtt

Posted: Thursday 12 July 2018 15:00
by emme
I have the same issue for switchcmd on/off... I think it's a hardware connector issue that should be addressed to github (I'm gathering more info before pulling the request)
as a workaround (which does NOT solve the issue) i dormally do:

var newMsg = {};
newMsg.payload = {};
newMsg.payload = <mqtt string>;

return msg;

about the setcolor.... could you try to send the ON command and then the setcolor command?

btw... are you using mosqitto on the same server where domoticz is installed?

Re: NodeJS Module: node-domoticz-mqtt

Posted: Thursday 12 July 2018 15:24
by Charley
Hello Emme,

I made the flow so that I can set them in any order.
If the RGB was on a setColor from the Domoticz UI then I am able to switch ON or change the color through Node-RED.
The moment I switch off with Node-RED I am not able to switch on with node-RED or set the new color, must go back to Domoticz UI first.

My MQTT, Mosquitto and Node-red are all on the same raspberry PI