How to stop a inject node from working

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.

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

You can even show the count in the function nodes 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:

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:

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


Hope this helps,
Johannes

1 Like

@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.

1 Like

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.

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

1 Like

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?

1 Like

@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.

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.

2 Likes

Indeed, always nice to find little gems like that. :gem: Thanks! :+1:

1 Like

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