Adding a counter


I am trying to add a counter to my node-red process. I would like it to add one to the counter every time that the whole process runs once.
This is because I am accumulating a math value until a certain counter value, which then leads to another path.
The mod 10 function is because after 9 counts, the process will move to another path.

Here is the code regarding the counter value I have been trying, the problem is that the counter is not updating and increasing by 1 each process:

packetcount = global.get("packetcount")||0;

packetcount = (packetcount+1) % 10;

node.warn("COUNT: "+ packetcount);

return msg;

Hi @pinkdolphins

I have already pointed you at how to use Context a couple times.

In your code you are reading packetcount from global context, but you never set it back to the updated value.

IN other words, you need to add

global.set("packetcount", packetcount);

before the return statement.