# Node-red-contrib-counter

**URL:** <https://discourse.nodered.org/t/node-red-contrib-counter/28360>\
**Category:** General\
**Created:** [15 June 2020 14:41 UTC](https://discourse.nodered.org/t/node-red-contrib-counter/28360 "2020-06-15T14:41:26Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![kxn25](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kxn25/32/10205_2.png) [@kxn25](https://discourse.nodered.org/u/kxn25)\
**Post date:** [15 June 2020 14:41 UTC](https://discourse.nodered.org/t/node-red-contrib-counter/28360/1 "2020-06-15T14:41:26Z")

</div>

Hello,  
i need help with the Counter.  
I want to count a GPIO input and the data get send to iobroker.  
So far so good.

 ![Zwischenablage01](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/f/bff4d210be0a883ceeb46c4acfb35db9b5fff5cc.jpeg)  
Everytime i resart the node the counter starts at 0 again, but i want to continue with the previos amount.

Can anyone help me?

---

<div class="post-metadata">

**Author:** ![Nxito](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nxito/32/88293_2.png) [@Nxito](https://discourse.nodered.org/u/Nxito)\
**Post date:** [15 June 2020 15:48 UTC](https://discourse.nodered.org/t/node-red-contrib-counter/28360/2 "2020-06-15T15:48:40Z")

</div>

take a look from this

```auto
[{"id":"4098285c.f730d8","type":"function","z":"87d4405e.717fa","name":"","func":"var count=context.get('counter') || 0;\nif(msg.topic==\"up\"){\ncount +=1; } \nif(msg.topic==\"down\"){\ncount -=1; } \nmsg.payload= msg.payload+\" \"+count;\ncontext.set('counter',count); \nif(msg.topic==\"reset\"){\ncontext.set('counter',0);\nmsg.payload=\"Counter reseted\";}\nreturn msg;","outputs":1,"noerr":0,"x":910,"y":280,"wires":[["18378891.ba84d7"]]},{"id":"10c6d6d5.e9ca39","type":"inject","z":"87d4405e.717fa","name":"counter up","topic":"up","payload":"","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":700,"y":240,"wires":[["4098285c.f730d8"]],"outputLabels":["msg.min"]},{"id":"18378891.ba84d7","type":"debug","z":"87d4405e.717fa","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1070,"y":280,"wires":[]},{"id":"93e37336.906ea","type":"inject","z":"87d4405e.717fa","name":"reset","topic":"reset","payload":"","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":690,"y":320,"wires":[["4098285c.f730d8"]],"outputLabels":["msg.min"]},{"id":"194a7d26.243c13","type":"inject","z":"87d4405e.717fa","name":"counter down","topic":"down","payload":"","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":710,"y":280,"wires":[["4098285c.f730d8"]],"outputLabels":["msg.min"]}]

```

---

<div class="post-metadata">

**Author:** ![michaelblight](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/michaelblight/32/445_2.png) [@michaelblight](https://discourse.nodered.org/u/michaelblight)\
**Post date:** [16 June 2020 05:49 UTC](https://discourse.nodered.org/t/node-red-contrib-counter/28360/3 "2020-06-16T05:49:11Z")

</div>

I think you're out of luck with that node. Persistent context was introduced into Node Red in version 0.19 on the 14th August 2018. This node was last changed on the 8th May 2018, so it would not be making use of this capability. You would have to do this yourself, so you may as well replace it with a `function` as @Nxito suggested.

---

<div class="post-metadata">

**Author:** ![kxn25](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kxn25/32/10205_2.png) [@kxn25](https://discourse.nodered.org/u/kxn25)\
**Post date:** [16 June 2020 12:04 UTC](https://discourse.nodered.org/t/node-red-contrib-counter/28360/4 "2020-06-16T12:04:01Z")

</div>

Is there another way?  
Iam not comming right with that script.  
As input i have a simple 0 witch need to be counted.  
is there a way to store that?  
Or take the send number to iobroker and count on that?

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [16 June 2020 19:35 UTC](https://discourse.nodered.org/t/node-red-contrib-counter/28360/5 "2020-06-16T19:35:24Z")

</div>

> [@kxn25](#):
>
> As input i have a simple 0 witch need to be counted

I assume that `0` is number zero and not text `"O"`, if so try this flow;

```auto
[{"id":"ddcf7841.dbf458","type":"function","z":"c9c4eeb8.bad8e","name":"","func":"let count=context.get('counter') || 0;\n\nif(msg.payload===0){\n count +=1;\n node.send({\"payload\": count});\n context.set('counter',count);\n} \n \nif(msg.topic==\"reset\"){\n context.set('counter',0);\n node.send({\"payload\": \"counter reset\"});\n}\n","outputs":1,"noerr":0,"x":860,"y":220,"wires":[["4a7b3c74.c32264"]]},{"id":"adb2b2f3.0d6ed","type":"inject","z":"c9c4eeb8.bad8e","name":"input","topic":"","payload":"0","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":670,"y":200,"wires":[["ddcf7841.dbf458"]],"outputLabels":["msg.min"]},{"id":"4a7b3c74.c32264","type":"debug","z":"c9c4eeb8.bad8e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1020,"y":220,"wires":[]},{"id":"8ba718d9.522438","type":"inject","z":"c9c4eeb8.bad8e","name":"reset","topic":"reset","payload":"","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":670,"y":240,"wires":[["ddcf7841.dbf458"]],"outputLabels":["msg.min"]}]

```

I've just changed @Nxito's flow a little to accept `0` as it's input value.

---

<div class="post-metadata">

**Author:** ![kxn25](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kxn25/32/10205_2.png) [@kxn25](https://discourse.nodered.org/u/kxn25)\
**Post date:** [16 June 2020 22:20 UTC](https://discourse.nodered.org/t/node-red-contrib-counter/28360/6 "2020-06-16T22:20:11Z")

</div>

Yes its a zero.  
As soon as i save the script again it´s starts with 1 again.

![Zwischenablage01](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/b/8b911376e6229951174dd5e9c9f188d594095c4b.jpeg)

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [16 June 2020 22:30 UTC](https://discourse.nodered.org/t/node-red-contrib-counter/28360/7 "2020-06-16T22:30:18Z")

</div>

Have you setup a [file-system context data store](https://nodered.org/docs/user-guide/context)?  
You are perhaps using a data store using memory, which will be lost after a reboot/restart.

The file-system context saves your data to disk, which can survive a reboot/restart.

---

<div class="post-metadata">

**Author:** ![kxn25](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kxn25/32/10205_2.png) [@kxn25](https://discourse.nodered.org/u/kxn25)\
**Post date:** [17 June 2020 05:27 UTC](https://discourse.nodered.org/t/node-red-contrib-counter/28360/8 "2020-06-17T05:27:10Z")

</div>

No, with restart i mean "Deploy"

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [17 June 2020 08:14 UTC](https://discourse.nodered.org/t/node-red-contrib-counter/28360/9 "2020-06-17T08:14:03Z")

</div>

please export your flow and attach it to a reply

---

<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:** [17 June 2020 08:33 UTC](https://discourse.nodered.org/t/node-red-contrib-counter/28360/10 "2020-06-17T08:33:36Z")

</div>

Which option have you selected when you press on the little arrow in the deploy button to open the dropdown?  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/9/6943ea06902d68384e09abef20e0651796cd10a5.jpeg)  
If you have selected Full it will be reset every time you deploy as all nodes will be redeployed every time with this option independent of if they have changed or not.  
The second option will do this if anything in the same flow/tab has changed.  
To prevent this select the third option and the counter shouldn’t be reset everytime you hit deploy as long as you don’t edit the counter node itself.

Johannes

---

<div class="post-metadata">

**Author:** ![kxn25](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kxn25/32/10205_2.png) [@kxn25](https://discourse.nodered.org/u/kxn25)\
**Post date:** [19 June 2020 06:14 UTC](https://discourse.nodered.org/t/node-red-contrib-counter/28360/11 "2020-06-19T06:14:06Z")

</div>

@JGKK Thank you that is part of a Solution. I did not know that.  
Thank you

But is it posibble to store that number in a file?

---

<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:** [19 June 2020 06:59 UTC](https://discourse.nodered.org/t/node-red-contrib-counter/28360/12 "2020-06-19T06:59:51Z")

</div>

Yes you could do something like this:

 ![Screenshot 2020-06-19 at 08.54.00](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/d/cd5911e0a31b9d65575bb37e663a31d1abdeb9b3.png)

```auto
[{"id":"81f8fc1d.e30aa8","type":"inject","z":"7df8e457.799894","name":"increment","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":600,"y":1100,"wires":[["708f5a99.58ff24"]]},{"id":"708f5a99.58ff24","type":"counter","z":"7df8e457.799894","name":"","init":"0","step":"1","lower":"","upper":"","mode":"increment","outputs":"1","x":760,"y":1160,"wires":[["9ed2ba7.3946a48","51a52e8c.2571e8"]]},{"id":"216dc2ec.cdb6e6","type":"inject","z":"7df8e457.799894","name":"get previous count","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":true,"onceDelay":0.1,"x":130,"y":1220,"wires":[["68b83e9e.f49c98"]]},{"id":"7ab67010.30bbc","type":"file","z":"7df8e457.799894","name":"","filename":"/tmp/count.txt","appendNewline":false,"createDir":false,"overwriteFile":"true","encoding":"none","x":1280,"y":1220,"wires":[[]]},{"id":"68b83e9e.f49c98","type":"file in","z":"7df8e457.799894","name":"","filename":"/tmp/count.txt","format":"utf8","chunk":false,"sendError":false,"encoding":"none","x":340,"y":1220,"wires":[["e00b7267.7202d8"]]},{"id":"6d29777.8689708","type":"change","z":"7df8e457.799894","name":"payload=count","rules":[{"t":"set","p":"payload","pt":"msg","to":"count","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1080,"y":1220,"wires":[["7ab67010.30bbc"]]},{"id":"e00b7267.7202d8","type":"change","z":"7df8e457.799894","name":"increment by previous count","rules":[{"t":"set","p":"increment","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":600,"y":1220,"wires":[["708f5a99.58ff24"]]},{"id":"38cace80.08c012","type":"inject","z":"7df8e457.799894","name":"reset","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":410,"y":1160,"wires":[["641692de.c9d414"]]},{"id":"641692de.c9d414","type":"change","z":"7df8e457.799894","name":"","rules":[{"t":"set","p":"reset","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":580,"y":1160,"wires":[["708f5a99.58ff24"]]},{"id":"9ed2ba7.3946a48","type":"debug","z":"7df8e457.799894","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"count","targetType":"msg","x":930,"y":1100,"wires":[]},{"id":"51a52e8c.2571e8","type":"switch","z":"7df8e457.799894","name":"!=0","property":"count","propertyType":"msg","rules":[{"t":"neq","v":"0","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":910,"y":1220,"wires":[["6d29777.8689708"]]}]

```

Of course you would want to save it somewhere else than tmp and you would probably not need to check for 0 before the file out.

The more elegant approach would be to set up persistent [storage that saves context to the file system](https://nodered.org/docs/user-guide/context#saving-context-data-to-the-file-system) and save the count to a flow variable instead of writing it to a file and injecting that flow variable on startup instead of reading the file.

Johannes

---

<div class="post-metadata">

**Author:** ![kxn25](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kxn25/32/10205_2.png) [@kxn25](https://discourse.nodered.org/u/kxn25)\
**Post date:** [19 June 2020 13:52 UTC](https://discourse.nodered.org/t/node-red-contrib-counter/28360/13 "2020-06-19T13:52:23Z")

</div>

@JGKK Thank you that is working just fine for me.  
I changed the file path and it´s working.

> [@JGKK](#):
>
> The more elegant approach would be to set up persistent [storage that saves context to the file system](https://nodered.org/docs/user-guide/context#saving-context-data-to-the-file-system) and save the count to a flow variable instead of writing it to a file and injecting that flow variable on startup instead of reading the file.

I quite did´nt under stand that part, but it works.  
I made a file name and gave it all permission and it reads and writes to it.

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [19 June 2020 14:37 UTC](https://discourse.nodered.org/t/node-red-contrib-counter/28360/14 "2020-06-19T14:37:30Z")

</div>

> [@kxn25](#):
>
> I quite did´nt under stand that part, but it works.

As I said 3 days ago..

> [@Paul-Reed](#):
>
> Have you setup a [file-system context data store](https://nodered.org/docs/user-guide/context)?

---

<div class="post-metadata">

**Author:** ![kxn25](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kxn25/32/10205_2.png) [@kxn25](https://discourse.nodered.org/u/kxn25)\
**Post date:** [20 June 2020 06:29 UTC](https://discourse.nodered.org/t/node-red-contrib-counter/28360/15 "2020-06-20T06:29:18Z")

</div>

> [@Paul-Reed](#):
>
> Have you setup a [file-system context data store](https://nodered.org/docs/user-guide/context)?

No i havn´t.  
I am reading it now how to do so.  
I understand that i need to do so, if i want to store other counters, correct?

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [20 June 2020 08:03 UTC](https://discourse.nodered.org/t/node-red-contrib-counter/28360/16 "2020-06-20T08:03:17Z")

</div>

> [@kxn25](#):
>
> I understand that i need to do so, if i want to store other counters, correct?

It is the correct way to save & retrieve counters and other data to/from disk, instead of memory.

---

<div class="post-metadata">

**Author:** ![kxn25](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kxn25/32/10205_2.png) [@kxn25](https://discourse.nodered.org/u/kxn25)\
**Post date:** [20 June 2020 15:14 UTC](https://discourse.nodered.org/t/node-red-contrib-counter/28360/17 "2020-06-20T15:14:25Z")

</div>

Is that correct so?

 ![Zwischenablage01](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/0/8040416f6559cc6fb8324da82b131885f3f98b6d.jpeg)

It saves the Count in that file and read´s it.

---

<div class="post-metadata">

**Author:** ![kxn25](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kxn25/32/10205_2.png) [@kxn25](https://discourse.nodered.org/u/kxn25)\
**Post date:** [22 June 2020 05:50 UTC](https://discourse.nodered.org/t/node-red-contrib-counter/28360/18 "2020-06-22T05:50:16Z")

</div>

Ok i now have a context system.  
I changed the setting.js and enabled Context.  
Now where or how do i set it to store it in my /home/pi/count.txt?  
Or how do i go about creating a file for the count to store?

And how do i work with the counter when i have to count a 1/1000 of it.  
When i set step to 0.001 i get odd numbers.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [22 June 2020 07:11 UTC](https://discourse.nodered.org/t/node-red-contrib-counter/28360/19 "2020-06-22T07:11:08Z")

</div>

> [@kxn25](#):
>
> Now where or how do i set it to store it in my /home/pi/count.txt

You don't need to tell it where to save the context, the system will save it for you. Go back and read again the link that was posted earlier. [Working with context : Node-RED](https://nodered.org/docs/user-guide/context)

---

<div class="post-metadata">

**Author:** ![kxn25](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kxn25/32/10205_2.png) [@kxn25](https://discourse.nodered.org/u/kxn25)\
**Post date:** [22 June 2020 09:10 UTC](https://discourse.nodered.org/t/node-red-contrib-counter/28360/20 "2020-06-22T09:10:24Z")

</div>

So i dont need the file node?

 ![Zwischenablage01](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/1/81ff9508c1f01f2c9e29be4105a31bbfc4724099.jpeg)

[flows.json](https://discourse.nodered.org/uploads/short-url/eUPOeOjqHwAaGWbl23P3Tl9qI2h.json) (2.5 KB)

[Next page](https://discourse.nodered.org/t/node-red-contrib-counter/28360.md?page=2)
