# Help with a node I made.

**URL:** <https://discourse.nodered.org/t/help-with-a-node-i-made/356>\
**Category:** General\
**Created:** [24 May 2018 13:26 UTC](https://discourse.nodered.org/t/help-with-a-node-i-made/356 "2018-05-24T13:26:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [24 May 2018 13:26 UTC](https://discourse.nodered.org/t/help-with-a-node-i-made/356/1 "2018-05-24T13:26:02Z")

</div>

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:

Idea:  
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.

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [24 May 2018 14:02 UTC](https://discourse.nodered.org/t/help-with-a-node-i-made/356/2 "2018-05-24T14:02:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [24 May 2018 14:04 UTC](https://discourse.nodered.org/t/help-with-a-node-i-made/356/3 "2018-05-24T14:04:42Z")

</div>

For an explanation of @shrickus first point see  
[https://nodered.org/docs/writing-functions#storing-data](https://nodered.org/docs/writing-functions#storing-data)

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [25 May 2018 06:26 UTC](https://discourse.nodered.org/t/help-with-a-node-i-made/356/4 "2018-05-25T06:26:54Z")

</div>

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)

```auto
   [{"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"]]}]

```
