NodeJS Module: node-domoticz-mqtt

Client tools or tools that can connect with Domoticz. Tools for Windows, iOS, Android, Linux etc.

Moderator: leecollings

User avatar
nayr
Posts: 354
Joined: Tuesday 11 November 2014 18:42
Target OS: Linux
Domoticz version: github
Location: Denver, CO - USA
Contact:

NodeJS Module: node-domoticz-mqtt

Post 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!
Debian Jessie: CuBox-i4 (Primary) w/Static Routed IP and x509 / BeagleBone with OpenSprinkler / BeagleBone Planted Aquarium / 3x Raspbery Pi2b GPIO Slaves
Elemental Theme - node-domoticz-mqtt - Home Theatre Controller - AndroidTV Simple OSD Remote - x509 TLS Auth
User avatar
nayr
Posts: 354
Joined: Tuesday 11 November 2014 18:42
Target OS: Linux
Domoticz version: github
Location: Denver, CO - USA
Contact:

Re: NodeJS Module: node-domoticz-mqtt

Post 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.
Debian Jessie: CuBox-i4 (Primary) w/Static Routed IP and x509 / BeagleBone with OpenSprinkler / BeagleBone Planted Aquarium / 3x Raspbery Pi2b GPIO Slaves
Elemental Theme - node-domoticz-mqtt - Home Theatre Controller - AndroidTV Simple OSD Remote - x509 TLS Auth
jrich523
Posts: 63
Joined: Monday 18 January 2016 21:43
Target OS: Windows
Domoticz version: 3.5196
Location: Seattle, WA
Contact:

Re: NodeJS Module: node-domoticz-mqtt

Post 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)
[ Domoticz 3.5196 | Win10 | AeoTec Z-Stick Gen5 ]
User avatar
nayr
Posts: 354
Joined: Tuesday 11 November 2014 18:42
Target OS: Linux
Domoticz version: github
Location: Denver, CO - USA
Contact:

Re: NodeJS Module: node-domoticz-mqtt

Post by nayr »

Domoticz has native support for DHT and 1Wire sensors.. dont use this library to integrate those.
Debian Jessie: CuBox-i4 (Primary) w/Static Routed IP and x509 / BeagleBone with OpenSprinkler / BeagleBone Planted Aquarium / 3x Raspbery Pi2b GPIO Slaves
Elemental Theme - node-domoticz-mqtt - Home Theatre Controller - AndroidTV Simple OSD Remote - x509 TLS Auth
User avatar
gizmocuz
Posts: 2486
Joined: Thursday 11 July 2013 18:59
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Top of the world
Contact:

Re: NodeJS Module: node-domoticz-mqtt

Post by gizmocuz »

Hi Nayr,

Just found this thread....

Domoticz has native mqtt support.... would it not be better to extend/enrich domoticz's implementation ?
Quality outlives Quantity!
User avatar
nayr
Posts: 354
Joined: Tuesday 11 November 2014 18:42
Target OS: Linux
Domoticz version: github
Location: Denver, CO - USA
Contact:

Re: NodeJS Module: node-domoticz-mqtt

Post 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.
Debian Jessie: CuBox-i4 (Primary) w/Static Routed IP and x509 / BeagleBone with OpenSprinkler / BeagleBone Planted Aquarium / 3x Raspbery Pi2b GPIO Slaves
Elemental Theme - node-domoticz-mqtt - Home Theatre Controller - AndroidTV Simple OSD Remote - x509 TLS Auth
jrich523
Posts: 63
Joined: Monday 18 January 2016 21:43
Target OS: Windows
Domoticz version: 3.5196
Location: Seattle, WA
Contact:

Re: NodeJS Module: node-domoticz-mqtt

Post 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
[ Domoticz 3.5196 | Win10 | AeoTec Z-Stick Gen5 ]
User avatar
nayr
Posts: 354
Joined: Tuesday 11 November 2014 18:42
Target OS: Linux
Domoticz version: github
Location: Denver, CO - USA
Contact:

Re: NodeJS Module: node-domoticz-mqtt

Post 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
Debian Jessie: CuBox-i4 (Primary) w/Static Routed IP and x509 / BeagleBone with OpenSprinkler / BeagleBone Planted Aquarium / 3x Raspbery Pi2b GPIO Slaves
Elemental Theme - node-domoticz-mqtt - Home Theatre Controller - AndroidTV Simple OSD Remote - x509 TLS Auth
woody4165
Posts: 476
Joined: Monday 14 March 2016 13:55
Target OS: Linux
Domoticz version: beta
Location: Rome, Italy
Contact:

Re: NodeJS Module: node-domoticz-mqtt

Post 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
Cubietruck - Linux cubietruck 4.13.16 (Debian GNU/Linux 8 (jessie)) + Domoticz + RFLink, Xiaomi Gateway, Owl USB, Yeelight Color and B/W, ESP8266, Broadlink RM2, Netatmo Thermostat
jrich523
Posts: 63
Joined: Monday 18 January 2016 21:43
Target OS: Windows
Domoticz version: 3.5196
Location: Seattle, WA
Contact:

Re: NodeJS Module: node-domoticz-mqtt

Post 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.
[ Domoticz 3.5196 | Win10 | AeoTec Z-Stick Gen5 ]
sj3fk3
Posts: 23
Joined: Sunday 22 March 2015 6:41
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: NodeJS Module: node-domoticz-mqtt

Post by sj3fk3 »

Wouldn't it be nice to make it node-red node?
http://nodered.org/
rlust
Posts: 4
Joined: Saturday 24 December 2016 18:03
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: NodeJS Module: node-domoticz-mqtt

Post 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?
rlust
Posts: 4
Joined: Saturday 24 December 2016 18:03
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: NodeJS Module: node-domoticz-mqtt

Post 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?
seblang
Posts: 10
Joined: Sunday 27 December 2015 21:24
Target OS: Windows
Domoticz version:
Contact:

Re: NodeJS Module: node-domoticz-mqtt

Post 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?
salvacalatayud
Posts: 112
Joined: Monday 26 June 2017 21:16
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Spain
Contact:

Re: NodeJS Module: node-domoticz-mqtt

Post 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
Charley
Posts: 24
Joined: Tuesday 06 March 2018 12:20
Target OS: Raspberry Pi / ODroid
Domoticz version: V3.9030
Location: Netherlands
Contact:

Re: NodeJS Module: node-domoticz-mqtt

Post 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
User avatar
emme
Posts: 909
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi / ODroid
Domoticz version: latest
Location: Milano, Italy
Contact:

Re: NodeJS Module: node-domoticz-mqtt

Post 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
The most dangerous phrase in any language is:
"We always done this way"
Charley
Posts: 24
Joined: Tuesday 06 March 2018 12:20
Target OS: Raspberry Pi / ODroid
Domoticz version: V3.9030
Location: Netherlands
Contact:

Re: NodeJS Module: node-domoticz-mqtt

Post 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?
User avatar
emme
Posts: 909
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi / ODroid
Domoticz version: latest
Location: Milano, Italy
Contact:

Re: NodeJS Module: node-domoticz-mqtt

Post 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?
The most dangerous phrase in any language is:
"We always done this way"
Charley
Posts: 24
Joined: Tuesday 06 March 2018 12:20
Target OS: Raspberry Pi / ODroid
Domoticz version: V3.9030
Location: Netherlands
Contact:

Re: NodeJS Module: node-domoticz-mqtt

Post 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
Attachments
This is what my Node-RED takes from Domoticz/out
This is what my Node-RED takes from Domoticz/out
Dashboard2.png (112.25 KiB) Viewed 11481 times
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest