Count current widgets (msg) and keep yesterdays count - example flow

I needed to count objects (really msg.payload) and keep a running live count, but then reset that count at midnight and save yesterdays total count.
@torchard wrote some function node code for me and here is a flow that should get you started.

Note, before you import the flow, be sure and install the counter node via the usual ‘manage pallet’ menu. (Note there are lots of counter nodes, so be sure and get the right one!).

Here is what the flow is going to look like;

You can see the reset inject. Of course you can reset at any time by clicking the inject tab.
The top inject is just to test things out and to show you where you inject your msg.payload or what ever you need to count.
The yellow change node just resets the counter by setting the msg to ‘reset’.
The counter node is set to increment by 1.
The function node has the code to move the current count into a holding message. Then when the function node sees the reset, it moves the count from the holding message to msg.yesterdaysCount.
You can display it or further handle it as needed.

Here then is the flow to import.

[{"id":"9ea8f292.ec453","type":"inject","z":"a84ec2b4.76b138","name":"12am reset","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"00 00 * * *","once":false,"onceDelay":0.1,"x":910,"y":220,"wires":[["7cc39011.103618"]]},{"id":"7cc39011.103618","type":"change","z":"a84ec2b4.76b138","name":"reset","rules":[{"t":"set","p":"reset","pt":"msg","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":1090,"y":220,"wires":[["2998f466.fbd634"]]},{"id":"2998f466.fbd634","type":"counter","z":"a84ec2b4.76b138","inc":1,"name":"","x":1240,"y":220,"wires":[["b65b0f3d.8680e8","208336ac.fd5262"]]},{"id":"b65b0f3d.8680e8","type":"function","z":"a84ec2b4.76b138","name":"get yesterdays count on reset","func":"if (msg.reset === 0) {      // If this is a reset;\n    prereset = context.get(\"old\")||0;   // grab the rolling total saved before this reset\n    flow.set(\"yesterdaysCount\", prereset);       // save this total from yesterday to display later\n    msg.yesterdaysCount = prereset;              // msg.ycount = `yesterday`, msg.payload = `live count`\n    return msg;                         // update live count and yesterday's total\n}\nelse {                      // Otherwise;\n    context.set(\"old\", msg.payload);    // update the rolling total\n    msg.yesterdaysCount = flow.get(\"yesterdaysCount\");    // get the old total from yesterday\n    return msg;                         // update live count and yesterday's total\n}","outputs":1,"noerr":0,"x":1490,"y":220,"wires":[["53d04062.27e8d8"]]},{"id":"b22bc2c3.004ea8","type":"inject","z":"a84ec2b4.76b138","name":"thing you want to count","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":1020,"y":160,"wires":[["2998f466.fbd634"]]},{"id":"208336ac.fd5262","type":"debug","z":"a84ec2b4.76b138","name":"Current thing count","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1460,"y":160,"wires":[]},{"id":"53d04062.27e8d8","type":"debug","z":"a84ec2b4.76b138","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"yesterdaysCount","targetType":"msg","x":1760,"y":220,"wires":[]}]
3 Likes