Custom Node code



I am working on a custom node and have been able to get this to work correctly in a function node on a flow. I have been able to make multiple copies of the function and change variables etc and all works fine as you would expect.
I have now worked on the html, js and package files for the node and when I test this there is unexpected/unpredictable behaviour. In most cases the first node on the flow works fine but when I add a second node of the same type this does not work and sometimes node red stops. I know that function nodes are a “special” case in a flow but I am trying to work out if the problem is in the code for the node or what the difference is between a working function node and creating a node for the pallette


Controlling gopro camera

Well you give us no clues as to what you have actually done so going to be tricky to guess…
But yes they are different… One way to start would be to look at an existing node and see what that does, and read the docs at


Thanks, apologies for lack of detail. The function node that works is

//var msg = {};
const bacnet1 = global.get('bacnet');
// Initialize BACStack
var client = bacnet1({adpuTimeout: 6000});

client.readProperty('', {type: 4, instance: 0}, 85, (err, value) => {
  msg.payload = {value: value} ;

The node js file is

module.exports = function(RED) {
      bacnet = require ('bacstack');
  function BacnetReadNode(config) {
      this.oidtype = config.oidtype;
      this.oidinst = config.oidinst;
      this.propid = config.propid;
      this.device = config.device;
      var client = new bacnet({adpuTimeout:2000});
var node = this;
this.on('input', function(msg) {
  client.readProperty(this.device, {type: this.oidtype, instance: this.oidinst}, this.propid, (err, value) => {
    msg.payload = {value: value} ;


Apart from the fun of it… any reason not to play with the existing BACnet node ?


I have been working with the node red developer for some time and also with the bacstack developer to resolve some issues. The write node has never worked in my use case and I have been using my write and read functions (which work fine) that I have created as a workaround. I wanted to see if I could create some working nodes to progress this to share with the existing bacnet node developer so that we can try to resolve the problems.


I don’t know enough about BACnet (or anything really :-)… but will the library allow you create multiple client ? (as you are making one per node at present).

Also you have no node.on("close",... function to release the client so on next deploy there will be yet more…


Oh, I am also doing this because I need the practice and experience :wink:

I did notice the node.on (close and thought this may be an issue. I have added this in a test in the js code but it did not make any difference, if fact I think it created new problems. If this was the problem logic suggests that this would also be a problem in the functions. There must be something fundamentally different about how a function executes this code as opposed to a “proper” node.

Once I got the functions to work I thought it would be simple to create the node and move forward

Mmm not so much


When I try to create a node with this functionality node red crashes. The logic is that a function node in a flow must execute this differently. Is there anywhere I could read to understand what these differences may be to fix the problem. Thanks


The Function runs its code using the standard node.js vm sandbox module. That’s the only real difference.

If you share the details of the error you are hitting then we might be able to help.


I have tried to recreate the error but now I am able to install the module with some hard coded variables and it works, but only on the last node placed on the flow. When a second node is added the first node msg.payload is empty, so on a flow with multiple of the same node only the last one works. If I test the similar normal function node with similar code it is possible to have many of these all working. I suspect it may have to do with client.close() and possibly the function node is automatically doing something after it has passed the message. The code examples are earlier in this post. Thanks for the help


A key difference between your Function code and your custom Node code is that the Function code creates a new instance of the backstack client object for every message. Your Node code creates a single backstack client object for each node and reuses it for each message a node receives.

I cannot comment on why that does not behave as you expect as I do not know the bacstack module.

You may want to look at the inner workings of one of the existing bacnet modules - they may not do exactly what you want, but they will provide a proven starting point.


Thanks for the help, this makes sense so I will carry on in this direction:grinning:


whether can support MS/TP ?


@Chris you'll need to explain what MS/TP is... And how that relates to this topic.


@Chris no, the node red bacnet module only supports bacnet IP. The Node Red bacnet node is based on Bacstack which does not support Bacnet MS/TP


Hi, based on existing bacstack whether can add BACnet MS/TP(Master Slave/Token Passing) function which is very popular in BAS. I hope the node-red-contrib-bacnet which seems like node-red-contrib-modbus both support TCP AND SERIAL(RTU) interface. Maybe how to implement the 5ms timers for token is the key issue in bacstack with JS.