# Counter node loses count on upgrade

**URL:** <https://discourse.nodered.org/t/counter-node-loses-count-on-upgrade/43440>\
**Category:** General\
**Created:** [31 March 2021 07:21 UTC](https://discourse.nodered.org/t/counter-node-loses-count-on-upgrade/43440 "2021-03-31T07:21:13Z")\
**Posts on this page:** 1\
**Showing post:** 22

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [31 March 2021 16:53 UTC](https://discourse.nodered.org/t/counter-node-loses-count-on-upgrade/43440/22 "2021-03-31T16:53:24Z")

</div>

When you setup persistent context, then flow and globals (that you set either by a function or change node) are persisted to a JSON file in your .node-red directory.

> [@mikefila](#):
>
> i'm still having trouble understanding how the counter node stores and uses context. Does it store the count value just because context is enabled? Or does it also need to be connected to a change node that sets flow.counter?

Unless we look at the source or it explicitly states so, difficult to say.

If you want to be certain, make your own counter.  
 ![2BtEU3dNM9](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/5/55fc537fd0f196a1ae9e160393efed548e0cddcd.gif)

```auto
[{"id":"d1e7267c.8a21e8","type":"function","z":"89aa4a33.4c3098","name":"counter","func":"var count = context.get(\"count\") || 0;\n\nif(msg.topic == \"reset\" || msg.reset == true) {\n count = 0;\n} else if (msg.topic == \"set\") {\n count = msg.payload;\n} else if (msg.topic == \"decrement\" || msg.topic == \"dec\") {\n count--;\n} else {\n //assume increment\n count++;\n}\ncontext.set(\"count\", count); //store it\nnode.status({ text: count });\n\nmsg._count = count; //pass current count in msg._count\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":725,"y":380,"wires":[[]],"icon":"node-red-dashboard/ui_numeric.png","l":false},{"id":"ce9c22d4.90a37","type":"inject","z":"89aa4a33.4c3098","name":"reset","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"reset","payload":"","payloadType":"str","x":570,"y":380,"wires":[["d1e7267c.8a21e8"]]},{"id":"e2bda53.5e40558","type":"inject","z":"89aa4a33.4c3098","name":"inc","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"str","x":570,"y":420,"wires":[["d1e7267c.8a21e8"]]},{"id":"6cf4ceb6.efbac","type":"inject","z":"89aa4a33.4c3098","name":"dec","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"dec","payload":"","payloadType":"str","x":570,"y":460,"wires":[["d1e7267c.8a21e8"]]},{"id":"a129a75c.b0e598","type":"inject","z":"89aa4a33.4c3098","name":"set 100","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"set","payload":"100","payloadType":"num","x":570,"y":340,"wires":[["d1e7267c.8a21e8"]]}]

```

---

_[View the full topic](https://discourse.nodered.org/t/counter-node-loses-count-on-upgrade/43440)._
