How to install a package into Node-Red on Hass.io?

Hi,

I want to use my Home Connect devices through Node-Red, what is a great opportunity as Home Assistant has no integration for that yet.

There is a package for Nore-Red:

However I have no idea where to run

npm install node-red-contrib-homeconnect

on a Hass.io installation. I have SSH running but I think the addons are running in their own environments? Can somebody enlighten me?

Kind regards

3 Likes

Click on the menu button on upper right corner.

Click palette manager.

Click install.

Type in homeconnect

Install it

9 Likes

That is what I tried first - no homeconnect there :frowning:

You can add the packages inside the addon configuration.

### Option:  `system_packages`

Allows you to specify additional [Alpine packages](https://pkgs.alpinelinux.org/packages) to be installed to your Node-RED setup (e.g.,  `g++` .  `make` ,  `ffmpeg` ).

**Note** :  *Adding many packages will result in a longer start-up time for the add-on.*

### Option:  `npm_packages`

Allows you to specify additional [NPM packages](https://www.npmjs.com/) or [Node-RED nodes](https://flows.nodered.org/?type=node&num_pages=1) to be installed to your Node-RED setup (e.g.,  `node-red-dashboard` ,  `node-red-contrib-ccu` ).

**Note** :  *Adding many packages will result in a longer start-up time for the add-on.*
2 Likes

Thank you!!

Why can’t I mark the post as solution?

This fails, too:

npm ERR! code E404
npm ERR! 404 Not Found - GET https://registry.npmjs.org/- - Not found
npm ERR! 404 
npm ERR! 404  '-@latest' is not in the npm registry.
npm ERR! 404 You should bug the author to publish it (or use the name yourself!)
npm ERR! 404 
npm ERR! 404 Note that you can also install from a
npm ERR! 404 tarball, folder, http url, or git url.
npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2019-10-25T20_48_50_397Z-debug.log
[22:48:50] FATAL: Failed installing npm package -
[cont-init.d] user.sh: exited 1.
[cont-finish.d] executing container finish scripts...
[cont-finish.d] 99-message.sh: executing... 
-----------------------------------------------------------
                Oops! Something went wrong.

 We are so sorry, but something went terribly wrong when
 starting or running this add-on.
 
 Be sure to check the log above, line by line, for hints.
-----------------------------------------------------------
[cont-finish.d] 99-message.sh: exited 0.
[cont-finish.d] done.
[s6-finish] waiting for services.
[s6-finish] sending all processes the TERM signal.

My config:

{
  "credential_secret": "#####",
  "dark_mode": true,
  "http_node": {
    "username": "",
    "password": ""
  },
  "http_static": {
    "username": "",
    "password": ""
  },
  "ssl": false,
  "certfile": "fullchain.pem",
  "keyfile": "privkey.pem",
  "require_ssl": true,
  "system_packages": [],
  "npm_packages": "node-red-contrib-homeconnect",
  "init_commands": []
}

Im not sure if this will automatically install the both dependencies your package has. Just in case, you may try to add both of them manually.

1 Like

Okay, will try that later and I also want to find it what it means when saying

 You should bug the author to publish it (or use the name yourself!)

I thought I used the name :smiley:

Got some time to check on this again. Thanks to your hint regarding the dependencies I got it working now.

  "npm_packages": [
    "request",
    "swagger-client",
    "node-red-contrib-homeconnect"
  ],`

This does the trick.

Reviving this old thread:

I would like to use simple MD5 hashing in my function nodes (need speed, not security).
I managed to load the npm package adding this to my config:

npm_packages:
  - md5

And I see in the log that the package is loaded:

[09:36:08] INFO: Starting installation of custom NPM/Node-RED packages...
+ [email protected]
added 4 packages from 4 contributors and audited 1186 packages in 46.1s

But how can I access MD5 now in my function nodes?

Solved it:

Add this to the config of the HA Node RED add-on:

npm_packages:
  - md5

Then edit /config/node-red/settings.js and add to the property functionGlobalContext :

    functionGlobalContext: {
        // os:require('os'),
        // jfive:require("johnny-five"),
        // j5board:require("johnny-five").Board({repl:false})
        md5:require("md5")  // <- ADD THIS
    },

Now restart the Node RED add-on.

Then you can use MD5 in your function nodes like this:

let md5 = global.get("md5");
let myHash = md5("blah");

Details on functionGlobalContext can be found here: Writing Functions : Node-RED
These docs also contain another option (functionExternalModules) which also seems to work, if enabled through settings.js.

4 Likes

In my experience this is not working. I can’t add the packages to the list of packages and use it, it says the package does not exists.
Also, it is very inefficient because it install them on every start, which is IMO a waste

Ok, not sure why but it was not working previously. I think I have to add the package first, restart and then use it

Absolutely! Only a restart of the Node RED add-on will load the Node package into Node RED. This can’t happen on the fly.

I have updated my posting above to make that very clear.

out of curiosity: why you cannot add those packages using pallete? I always do that this way and never had problems.
Is it something unavailable through pallete?

You are talking about the palette, right?
Imagine that you need to hash some string right within a complex function node. If you install an MD5 contrib node through the palette you cannot use the MD5 function within the JS of a function node. You can only use MD5 as a dedicated node that acts on the payloads of messages.

But if you go the way described above you can use the new function right within your code.

MD5 is only an example here. It could be any function that is not available in native JS but available as a Node.js package.

1 Like

This is a neat trick, didn’t you could extend function nodes like this. Definitely bookmarking this for later :+1:

Using the functionExternalModules option - Added in 1.3.0

https://nodered.org/docs/user-guide/writing-functions

1 Like

Yes, I have activated that option too and it works.
But I much prefer the global version. And I don’t think that there is a performance penalty as this is probably only a question of scoping.

In my case I used it to install “graylog2” library so I can create a custom logger on node-red settings.js file. Now all my node-red logs are on a centralised place which is my logs-server.

  // Configure the logging output
  logging: {/* ... bla bla bla, the built it logging */},
  graylog: {
      level: 'debug',
      handler(settings){
        const graylog2 = require("graylog2");
        const logger = new graylog2.graylog({
          servers: [ { "host": "logs-server.home", port: 12201 } ],
           facility: "node-red.js",     // the facility for these log messages
           });
          return function gLogger(msg){
            logger.log(msg)
          }
      }
    }
  },
1 Like