# Adding a counter

**URL:** <https://discourse.nodered.org/t/adding-a-counter/13892>\
**Category:** General\
**Created:** [1 August 2019 08:18 UTC](https://discourse.nodered.org/t/adding-a-counter/13892 "2019-08-01T08:18:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![pinkdolphins](https://avatars.discourse-cdn.com/v4/letter/p/ecb155/32.png) [@pinkdolphins](https://discourse.nodered.org/u/pinkdolphins)\
**Post date:** [1 August 2019 08:18 UTC](https://discourse.nodered.org/t/adding-a-counter/13892/1 "2019-08-01T08:18:09Z")

</div>

Hello!

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:

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

packetcount = (packetcount+1) % 10;
packetcount+=1;

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

return msg;

```

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [1 August 2019 08:21 UTC](https://discourse.nodered.org/t/adding-a-counter/13892/2 "2019-08-01T08:21:58Z")

</div>

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

```auto
global.set("packetcount", packetcount);

```

before the return statement.
