Friday, 11 November 2016

Importing a lib for use in Node-Red in a Docker

In one of my Node-Red flows, I wanted to use the fuzzyset.js library. This has an npm installer but it didn't work inside the docker. The recommended approach is to install to the /data directory (appdata/nodered) from the host system but as unRAID doesn't support npm, I couldn't do that either.

So, I had a .js library that I needed to manually install and use. Here's how I did it;

First, from unRaid console, I created a 'fuzzyset' folder inside /mnt/user/docker/appdata/nodered/lib/. Then, I copied the fuzzyset.js file to this folder.

Next, I edited this part of the/mnt/user/docker/appdata/nodered/settings.js file to include the library;


 functionGlobalContext: {

      fuzzyset:require('/data/lib/fuzzyset/fuzzyset.js')

    },


Then, I could call and use the fuzzyset functions inside a node-red function like this



  
   var FuzzySet = context.global.get('fuzzyset');


   var a = FuzzySet(someArray);
   var result = a.get("Some String");




      
       

2 comments:

Michael said...

Hi,

I'm also using Node-Red (on Unraid) to stitch all of these things together and I'm muddling my way through some it with the help of your blog. My latest challenge is to get node-red controlling my TV. There's several node.js solutions (https://github.com/alanreid/bravia for one) but I don't really know where to start getting them into Node-Red.

Would this js approach work for this, or is there a better way - do you know?

MediaServer8 said...

Hi Michael

That should wok, though you'll still need to learn the specific API and write the javascript manually inside Node-Red functions - no nice visual UI, unfortunately.

Other things I'd look at would be if there's alternative platforms that could control your TV and could integrate with Node-Red. MQTT and IFTTT spring to mind.

Finally, do you have any other HA software on your network that you could use. For example, I have OpenHab and there might be a plug-in for that?

Good luck, and let me know how you get on.