Node-red-contrib-counter

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.


Everytime i resart the node the counter starts at 0 again, but i want to continue with the previos amount.

Can anyone help me?

take a look from this

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

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.

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?

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

[{"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.

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

Zwischenablage01

Have you setup a file-system context data store?
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.

No, with restart i mean "Deploy"

please export your flow and attach it to a reply

Which option have you selected when you press on the little arrow in the deploy button to open the dropdown?
image
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

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

Yes you could do something like this:

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

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

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.

As I said 3 days ago..

1 Like

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?

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

Is that correct so?

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

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.

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. https://nodered.org/docs/user-guide/context

1 Like

So i dont need the file node?

flows.json (2.5 KB)