Get count on reset, even if its 0

Hi,

My problem is simple yet I can't seem to wrap my head around it. I'm grabbing the last saved count from 'node-red-contrib-counter' when it's reset at midnight and displaying the old total as "Yesterdays Count". The function below for "Yesterdays Count" is working if the count is 1 or more but if the count is 0 it is not returning 0. "Yesterdays Count" will remain at whatever the value was before or will update next time the count is a reset as long as the count is 1 or more. How can I adapt the function below to accept 0 and pass it to "Yesterdays Count"?

// Grab the last saved total count:
msg.ycount = flow.get("ycount")||0;
// If the count just reset, display the old total:
if (msg.reset === "True") return msg;
// otherwise just update the new saved value,
else {
flow.set("ycount", msg.count);
// and end the program:
return null;
}

Add a debug node set to show Complete Message to see what is going into that function. Also add a debug node showing what is coming out. Give the debug nodes names so that they are distinguishable in the debug pane.

Edit do you realise that if msg.reset is true then it is returning before the flow.set() line, so the flow variable is not updated in that case?

More edits Also add this line after the flow.get() line

node.warn(`ycount is ${msg.ycount}`)

That will show ycount in the debug pane each time round.

The node-red-contrib-counter needs a boolean for the reset msg.
So testing for it in your function will be if (msg.reset === true) without the quotes around true.

edit: I changed msg.reset command to boolean true rather than string "True". Both seem to work as far as resetting node-red-contrib-counter but I'll keep it as a boolean as it's a better best practice option.

I've been checking the debug pane each time round and ycount is updating properly. If the count goes to 5 for example and I click reset, Yesterdays Count will update to 5 and Todays Count resets to 0. If the Todays Count then goes to 3 and I hit reset again, Yesterdays Count changes to 3 and Todays Count goes to 0. All of this is working how I want so far. But if Todays Count = 0 and I hit reset, Yesterdays Count will stay at 3.

Show us the input and output debugs from the function node when that happens, including the warn statement. That should tell you what is going wrong. In particular check what msg.reset is set to. Remember js is case sensitive so true is not the same as True.

[{"id":"9c63906c.c1cf","type":"counter","z":"bbb5fd78.aa486","name":"","init":"0","step":"1","lower":"0","upper":"","mode":"increment","outputs":1,"x":560,"y":260,"wires":[["f1370665.1292f8","ff1ab10e.52868"]]},{"id":"f1370665.1292f8","type":"function","z":"bbb5fd78.aa486","name":"get count on reset","func":"// Grab the last saved total count:\nmsg.ycount = flow.get(\"ycount\")||0;\nnode.warn(`ycount is ${msg.ycount}`)\n// If the count just reset, display the old total:\nif (msg.reset === true) return msg;\n// otherwise just update the new saved value,\nelse {\n flow.set(\"ycount\", msg.count);\n // and end the program:\nreturn null;\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","x":730,"y":320,"wires":[["1ec88881.cf4617"]]},{"id":"ff1ab10e.52868","type":"debug","z":"bbb5fd78.aa486","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"count","targetType":"msg","statusVal":"","statusType":"auto","x":930,"y":260,"wires":[]},{"id":"1ec88881.cf4617","type":"debug","z":"bbb5fd78.aa486","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"ycount","targetType":"msg","statusVal":"","statusType":"auto","x":930,"y":320,"wires":[]},{"id":"f632022.9ba9","type":"inject","z":"bbb5fd78.aa486","name":"Increment","props":[{"p":"increment","v":"1","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":300,"y":240,"wires":[["9c63906c.c1cf"]]},{"id":"c9f19619.43e138","type":"inject","z":"bbb5fd78.aa486","name":"Reset","props":[{"p":"reset","v":"true","vt":"bool"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":290,"y":280,"wires":[["9c63906c.c1cf"]]}]

I meant what you get out of a debug node showing what is going into the function and what is coming out of the function when the error happens