# How to stop a inject node from working

**URL:** <https://discourse.nodered.org/t/how-to-stop-a-inject-node-from-working/39062>\
**Category:** General\
**Created:** [13 January 2021 14:50 UTC](https://discourse.nodered.org/t/how-to-stop-a-inject-node-from-working/39062 "2021-01-13T14:50:43Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![oussama55](https://avatars.discourse-cdn.com/v4/letter/o/dfb087/32.png) [@oussama55](https://discourse.nodered.org/u/oussama55)\
**Post date:** [13 January 2021 14:50 UTC](https://discourse.nodered.org/t/how-to-stop-a-inject-node-from-working/39062/1 "2021-01-13T14:50:44Z")

</div>

Hi guys,  
i have a issue that i couldn't resolve it.  
Well i want to stop an inject node from sending its peyload to a database node when an incrementation process arrives to 135, i had in my mind to implement a function( which contains the condition "if" when the number arrives to 135) linked to the inject node but i think it's impossible!!  
please can i have some help or just some suggestions about how the solution will be(it's urgent please).  
Thank you.

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [13 January 2021 15:21 UTC](https://discourse.nodered.org/t/how-to-stop-a-inject-node-from-working/39062/2 "2021-01-13T15:21:42Z")

</div>

Hello,  
It’s easily done in a function node.  
You will need to save the count to context as each time a message is send to a function node this creates a new instance of the function node and context storage is the only way to share variables across several instances of the same function:

[https://nodered.org/docs/user-guide/writing-functions#storing-data](https://nodered.org/docs/user-guide/writing-functions#storing-data)

You can even show the count in the function nodes status:

[https://nodered.org/docs/user-guide/writing-functions#adding-status](https://nodered.org/docs/user-guide/writing-functions#adding-status)

If you apply those concepts you can write a simple function like this for example:

```auto
let count = context.get("count") || 0;
if (msg.payload !== "reset") {
    if (count < 125) {
        count += 1;
        node.status({text:`count: ${count}`});
        context.set("count", count);
        return msg;
    } else {
        node.status({text:"limit reached"});
        return null;
    }
} else {
    context.set("count", 0);
    node.status({text:"reset"});
    return null;
}

```

Which blocks message after it passed 125 but can be reset with a `msg.payload` of **reset** and also reflects this in the status.  
Here is a example flow:

```auto
[{"id":"e007b528.9f4fc","type":"function","z":"d2a4c65d.74fe8","name":"Pass to 125","func":"let count = context.get(\"count\") || 0;\nif (msg.payload !== \"reset\") {\n if (count < 125) {\n count += 1;\n node.status({text:`count: ${count}`});\n context.set(\"count\", count);\n return msg;\n } else {\n node.status({text:\"limit reached\"});\n return null;\n }\n} else {\n context.set(\"count\", 0);\n node.status({text:\"reset\"});\n return null;\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":430,"y":240,"wires":[["533ba1b6.851f58"]]},{"id":"54fcb3ec.12b7c4","type":"inject","z":"d2a4c65d.74fe8","name":"Every Second","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"1","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":240,"y":240,"wires":[["e007b528.9f4fc"]]},{"id":"55aca3bf.f5054c","type":"inject","z":"d2a4c65d.74fe8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"reset","payloadType":"str","x":270,"y":300,"wires":[["e007b528.9f4fc"]]},{"id":"533ba1b6.851f58","type":"debug","z":"d2a4c65d.74fe8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":620,"y":240,"wires":[]}]

```

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/f/3f3e3754ce6ad07a3741db71ae3e9ebf4e63c38c.jpeg)  
Hope this helps,  
Johannes

---

<div class="post-metadata">

**Author:** ![oussama55](https://avatars.discourse-cdn.com/v4/letter/o/dfb087/32.png) [@oussama55](https://discourse.nodered.org/u/oussama55)\
**Post date:** [13 January 2021 15:40 UTC](https://discourse.nodered.org/t/how-to-stop-a-inject-node-from-working/39062/3 "2021-01-13T15:40:10Z")

</div>

@JGKK I tried your code and i understand it very good, and it worked just fine.  
Thank you very much, your assistance has helped me very much and i appreciate it.

---

<div class="post-metadata">

**Author:** ![Urs-Eppenberger](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/urs-eppenberger/32/28_2.png) [@Urs-Eppenberger](https://discourse.nodered.org/u/Urs-Eppenberger)\
**Post date:** [13 January 2021 15:40 UTC](https://discourse.nodered.org/t/how-to-stop-a-inject-node-from-working/39062/4 "2021-01-13T15:40:37Z")

</div>

There are usually many ways to do it.  
We currently work under the assumption, that your inject node is running repeatedly, and that you want to stop that repetition.  
I just lately implemented something similar with the trigger node. Maybe you can copy that concept. It is very similar to the solution of Johannes.

```auto
[{"id":"54fcb3ec.12b7c4","type":"inject","z":"b46b7fa0.7b04d","name":"start","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":270,"y":120,"wires":[["3b1d0ace.904dae"]]},{"id":"55aca3bf.f5054c","type":"inject","z":"b46b7fa0.7b04d","name":"reset","props":[{"p":"reset","v":"true","vt":"bool"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":270,"y":180,"wires":[["3b1d0ace.904dae"]]},{"id":"533ba1b6.851f58","type":"debug","z":"b46b7fa0.7b04d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":720,"y":120,"wires":[]},{"id":"3b1d0ace.904dae","type":"trigger","z":"b46b7fa0.7b04d","name":"","op1":"1","op2":"0","op1type":"str","op2type":"str","duration":"-1","extend":false,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":490,"y":120,"wires":[["533ba1b6.851f58"]]}]

```

 ![Screenshot 2021-01-13 at 16.39.04](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/3/e38facf2f9bb5df8c66d51b3d428f35c47698b34.jpeg)

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [13 January 2021 15:44 UTC](https://discourse.nodered.org/t/how-to-stop-a-inject-node-from-working/39062/5 "2021-01-13T15:44:12Z")

</div>

I don’t see how you automatically stop after a certain number of repetitions with that? As that was the requirement. You would still have to add a function afterwards for this?

---

<div class="post-metadata">

**Author:** ![oussama55](https://avatars.discourse-cdn.com/v4/letter/o/dfb087/32.png) [@oussama55](https://discourse.nodered.org/u/oussama55)\
**Post date:** [13 January 2021 15:52 UTC](https://discourse.nodered.org/t/how-to-stop-a-inject-node-from-working/39062/6 "2021-01-13T15:52:40Z")

</div>

@Urs-Eppenberger  
Thank your Mr.Eppenberger for your reply.  
i've thought before of this solution and tried it lately and it also worked just fine but it hasn't fixed the problem of stopping the incrementation when it reaches 135.  
That's why it had to be modified with a simple touch by adding a function.

---

<div class="post-metadata">

**Author:** ![Urs-Eppenberger](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/urs-eppenberger/32/28_2.png) [@Urs-Eppenberger](https://discourse.nodered.org/u/Urs-Eppenberger)\
**Post date:** [13 January 2021 16:00 UTC](https://discourse.nodered.org/t/how-to-stop-a-inject-node-from-working/39062/7 "2021-01-13T16:00:24Z")

</div>

I know. I just thought, the count is done somewhere else.  
I especially like the solution of Johannes with the count in the status of the node. Very cool.  
Urs.

---

<div class="post-metadata">

**Author:** ![duaw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/duaw/32/20970_2.png) [@duaw](https://discourse.nodered.org/u/duaw)\
**Post date:** [13 January 2021 16:30 UTC](https://discourse.nodered.org/t/how-to-stop-a-inject-node-from-working/39062/8 "2021-01-13T16:30:23Z")

</div>

> [@Urs-Eppenberger](#):
>
> I especially like the solution of Johannes with the count in the status of the node. Very cool.

Indeed, always nice to find little gems like that. 💎 Thanks! 👍

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [27 January 2021 16:30 UTC](https://discourse.nodered.org/t/how-to-stop-a-inject-node-from-working/39062/9 "2021-01-27T16:30:27Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
