Counter node loses count on upgrade

I'm sorry, 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?

In other words if I didn't set flow.counter, the counter nodes value would not be stored in context?

The data that is stored in flow.counter file, how is that used? Does node red automatically check the file on restart/deploy to set the value of that counter?

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.

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

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

So it took me a bit to understand how to make this fit my scenario. I want the initial value to be 55000000. Each step is 694. However if context.count is not found it will reset to 55100000, I believe that's how I set it?

I'd like to be able to differentiate between a reset command vs the file being deleted.

var count = context.get("count") || 55100000;

if(msg.topic == "reset" || msg.reset == true) {
    count = 55000000;
} else if (msg.topic == "set") {
    count = msg.payload;
} else if (msg.topic == "increment" || msg.topic == "inc") {
    count += 694;
} else {
    //assume decrement
    count -= 694;
}
context.set("count", count); //store it
node.status({ text: count });

msg.count = count; //pass current count in msg.count
return msg;

Change the first line to...

var count = context.get("count");

Then if count is null or undefined, you know this is a "fresh start".

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.