Help with a node I made.


This node is similar to one I got help with earlier.

That node is still needed and used.

But working on my flows, it became obvious I needed another node of similar functions, but not exactly the same.

I’m not sure if I am re-inventing the wheel, but here is my effort:

A node with 1 input and 1 output.
One set of signals is TOPIC set to ENABLE.
It can be ENABLE or DISABLE.
(I guess I can change that to boolean, but…)

The other input/message is only passed if ENABLE is active.

[{“id”:“a3d66060.812868”,“type”:“function”,“z”:“2d44537.f26082c”,“name”:“EN”,“func”:“context.message;\ncontext.mtopic = msg.topic;\nvar enable_topic = context.get(‘enable_topic’);\nvar mpayload = msg.payload;\nvar enable_topic = ‘ENABLE’;\n\nif (enable_topic === 1)\n{\n node.status({fill:“green”,shape:“dot”,text:“On-line”});\n return msg;\n}\n\nif (context.mtopic == enable_topic)\n{\n //\n if (msg.payload === ‘ENABLE’)\n {\n node.status({fill:“yellow”,shape:“dot”,text:“ENABLED”});\n context.enable = 1;\n context.set(‘enable’,enable_topic);\n }\n if (msg.payload === ‘DISABLE’)\n {\n context.enable = 0;\n context.set(‘enable’,enable_topic);\n node.status({fill:“red”,shape:“dot”,text:“DISABLED”});\n }\n}\n”,“outputs”:1,“noerr”:0,“x”:720,“y”:1870,“wires”:[[“a1219f49.a1eac8”]]}]

I seem to be missing something about how the message is passed through.

I (maybe) should learn how to have multiple inputs to a node.
But for now I am using a function node and it has only 1 input.



There is in fact a node to do this in the flows library, called node-red-contrib-traffic

But if you just want to learn how to code it yourself, here are couple tips:

  • You should set and get the node’s context variables using context.set("name", value) and context.get("name"), instead of the dotted notation you are currently using
  • You are redefining the var enable_topic, which is why your function never returns the incoming msg object (enable_topic will never === 1)
  • the first line of your function does nothing


For an explanation of @shrickus first point see



Thanks both.

The reason the first line of code in the original was not used was because it was used in the “original” node configuration.

I hadn’t gone through and tidied it up as it was still work in progress.

The reason I was using “dotted” commands (dated?) is because that was how I was shown.
I have now adapted the code to the newer format/structure.

This is the node I finally got working.

(Sorry it is from the text editor and not from NR.)

I admit I get confused with JavaSCRIPT maths functions compared to Arduino ones, and switching between them doesn’t help.

My fault. I admit. But I am now glad I got it working.

I hope someone else can find it helpful.

I haven’t worked out (yet) how to have the input as Boolean with ‘true’ and ‘false’ being sent.
That’s beyond what I know at this stage.

(Edited and pasted from NR)
(edited by @knolleary to put the flow in a code block)

   [{"id":"a3d66060.812868","type":"function","z":"2d44537.f26082c","name":"EN","func":"if (msg.topic === \"ENABLE\")\n{\n    //\n    if (msg.payload === 'true')\n    {\n        node.status({fill:\"yellow\",shape:\"dot\",text:\"ENABLED\"});\n        context.set('enable',1);\n    }\n    //if (msg.payload === 'false')\n    else\n    {\n        context.set('enable',0);\n//        context.enable = 0;\n//        context.set('enable',enable_topic);\n        node.status({fill:\"red\",shape:\"dot\",text:\"DISABLED\"});\n    }\n}\nif (msg.topic != \"ENABLE\")\n{\n    if (context.get('enable') == 1)\n    {\n        //\n        node.status({fill:\"green\",shape:\"dot\",text:\"On-line\"});\n        return msg;\n    }\n}\n","outputs":1,"noerr":0,"x":570,"y":1870,"wires":[["a1219f49.a1eac8"]]}]