[Solution-Flow] Reset and retain a counter value at midnight

Spent way too long figuring out how to do this so thought I should share it.

I count widgets (messages actually) and display the current count on the dashboard.
Wanted to reset the count at midnight, but carry over the count so I could see what yesterdays total got up to.

Note. I am not a programmer, so there is probably better ways to do this, but a 3 hour search of these forums did not reveal the secret sauce.

Here is what the flow you are about to import looks like.

I have tried to make this as harmless to import as possible.

Here is the counter node I used.

The data you are counting is / should be msg.payload for the counter node to count it.
You set msg.reset to 0 at midnight. msg.reset goes through the counter node untouched, so the function node looks for the reset message and grabs the current count when it equals zero.
It then moves the current count to 'ycount' (yesterdays count).
You can display both or either today's current count (msg.payload) or yesterdays total count (msg.ycount).

Here is the flow;
[{"id":"113b7e8c.305051","type":"counter","z":"8ec2c530.bfa978","inc":1,"name":"","x":640,"y":1480,"wires":[["5f5784e5.11792c","2debc86d.0b0db8"]]},{"id":"e1320843.e658e8","type":"inject","z":"8ec2c530.bfa978","name":"midnight reset","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"00 23 * * *","once":false,"onceDelay":0.1,"x":320,"y":1480,"wires":[["cc5df278.dd9b2"]]},{"id":"cc5df278.dd9b2","type":"change","z":"8ec2c530.bfa978","name":"reset","rules":[{"t":"set","p":"reset","pt":"msg","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":490,"y":1480,"wires":[["113b7e8c.305051"]]},{"id":"5f5784e5.11792c","type":"function","z":"8ec2c530.bfa978","name":"get count on reset","func":"// Grab the last saved total count:\nmsg.ycount = flow.get(\"ycount\")||0;\n// If the count just reset, display the old total:\nif (msg.reset === 0) return msg;\n// otherwise just update the new saved value,\nelse {\n flow.set(\"ycount\", msg.payload);\n // and end the program:\n return null;\n}","outputs":1,"noerr":0,"x":850,"y":1520,"wires":[["ab87afc4.8688b"]]},{"id":"9bd38d5f.857e2","type":"inject","z":"8ec2c530.bfa978","name":"data your counting","topic":"","payload":"","payloadType":"date","repeat":"1","crontab":"","once":false,"onceDelay":0.1,"x":450,"y":1400,"wires":[["113b7e8c.305051"]]},{"id":"2debc86d.0b0db8","type":"debug","z":"8ec2c530.bfa978","name":"todays count","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1070,"y":1460,"wires":[]},{"id":"ab87afc4.8688b","type":"debug","z":"8ec2c530.bfa978","name":"yesterdays total count","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"ycount","targetType":"msg","x":1100,"y":1520,"wires":[]},{"id":"62a37fb5.3d671","type":"comment","z":"8ec2c530.bfa978","name":"Probably connected to dashboard nodes","info":"","x":1100,"y":1400,"wires":[]}]

Hope that helps someone out.


Since I'm quite new at this I'll play with it some, thanks. Question: What is the "counter" node? Since I don't have it I assume you got it from a contributor.

Thanks for taking a look.
I have edited my original post with the counter node I used.... Very remiss of me to omit it.

No problem. That's why this is a forum and not a blog :wink:

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