How to Reset the Counter

@azrul This was an interesting problem and I think I have a solution. You will need to install the node node-red-contrib-stoptimer to run my example. Here is what it looks like:

Initialization: This just sets the item delayed flag to false
initialization

Main processing: A sensor reading comes in and a switch checks to see if the delay flag is true. Weither it is true or false, it starts a 5 second timer. If that five second timer expires, the delay flag is set to true and the start_delay time is set.
If the the delay flag was on, the code sets the end delay time and computes the amount of delay time and reports it.

The last part is a simulation of items on the conveyor belt to test the code.

Here is the full flow:

[{"id":"8c83488b.98e1f","type":"tab","label":"Flow 6","disabled":false,"info":""},{"id":"50b4afb6.32df8","type":"switch","z":"8c83488b.98e1f","name":"Switch","property":"item_delayed","propertyType":"flow","rules":[{"t":"false"},{"t":"true"}],"checkall":"true","repair":false,"outputs":2,"x":310,"y":160,"wires":[["dbae1bcd.f439e8"],["dbae1bcd.f439e8","c05c4be0.80cbc"]]},{"id":"10a76c1c.534634","type":"debug","z":"8c83488b.98e1f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":790,"y":280,"wires":[]},{"id":"dbae1bcd.f439e8","type":"stoptimer","z":"8c83488b.98e1f","duration":"5","units":"Second","payloadtype":"num","payloadval":"0","name":"","x":520,"y":160,"wires":[["67a88726.b320d"],[]]},{"id":"67a88726.b320d","type":"change","z":"8c83488b.98e1f","name":"set delay start and flag delay","rules":[{"t":"set","p":"delay_start_time","pt":"flow","to":"","tot":"date"},{"t":"set","p":"item_delayed","pt":"flow","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":780,"y":140,"wires":[[]]},{"id":"579c2758.69d8f","type":"inject","z":"8c83488b.98e1f","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":true,"onceDelay":0.1,"x":130,"y":60,"wires":[["46046399.1c3004"]]},{"id":"46046399.1c3004","type":"change","z":"8c83488b.98e1f","name":"set item_delayed to false","rules":[{"t":"set","p":"item_delayed","pt":"flow","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":370,"y":60,"wires":[[]]},{"id":"114dcbda.255494","type":"function","z":"8c83488b.98e1f","name":"Report end_delay - start_delay / 1000 (milliseconds)","func":"var start_time = flow.get('delay_start_time')||0;\nvar end_time = flow.get('delay_end_time')||0;\nvar time_diff = (end_time - start_time)/1000;\nvar delay = \"item delayed by \"+time_diff+\" seconds\";\nmsg.payload = delay;\nreturn msg;","outputs":1,"noerr":0,"x":460,"y":280,"wires":[["10a76c1c.534634"]]},{"id":"c05c4be0.80cbc","type":"change","z":"8c83488b.98e1f","name":"set delay_end and turn delay flag off","rules":[{"t":"set","p":"delay_end_time","pt":"flow","to":"","tot":"date"},{"t":"set","p":"item_delayed","pt":"flow","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":410,"y":220,"wires":[["114dcbda.255494"]]},{"id":"54f13cd.dcb6cc4","type":"comment","z":"8c83488b.98e1f","name":"============ Initilization ============","info":"","x":230,"y":20,"wires":[]},{"id":"69f6e6b5.87d95","type":"link in","z":"8c83488b.98e1f","name":"sensor input","links":["4ed514b7.739284"],"x":130,"y":160,"wires":[["50b4afb6.32df8","6c498070.7dce48"]],"l":true},{"id":"9d26fdf3.5fa6a","type":"comment","z":"8c83488b.98e1f","name":"============ Main flow ============","info":"","x":220,"y":120,"wires":[]},{"id":"5524c152.ec6ea","type":"comment","z":"8c83488b.98e1f","name":"============ Test cases ============","info":"","x":190,"y":340,"wires":[]},{"id":"277d5e81.33a7ca","type":"inject","z":"8c83488b.98e1f","name":"run at start up","topic":"start","payload":"","payloadType":"date","repeat":"","crontab":"","once":true,"onceDelay":0.1,"x":400,"y":380,"wires":[["4ed514b7.739284"]]},{"id":"c4e185f9.b8b51","type":"inject","z":"8c83488b.98e1f","name":"run after 5 seconds","topic":"run after 5 seconds","payload":"","payloadType":"date","repeat":"","crontab":"","once":true,"onceDelay":"5","x":410,"y":420,"wires":[["4ed514b7.739284"]]},{"id":"97d6c554.65a6d8","type":"inject","z":"8c83488b.98e1f","name":"run after 10 seconds","topic":"run after 10 seconds","payload":"","payloadType":"date","repeat":"","crontab":"","once":true,"onceDelay":"10","x":420,"y":460,"wires":[["4ed514b7.739284"]]},{"id":"28ed0444.1fe544","type":"inject","z":"8c83488b.98e1f","name":"run after 20 seconds","topic":"run after 20 seconds","payload":"","payloadType":"date","repeat":"","crontab":"","once":true,"onceDelay":"20","x":420,"y":500,"wires":[["4ed514b7.739284"]]},{"id":"a34fcb3f.16c79","type":"inject","z":"8c83488b.98e1f","name":"run after 25 seconds","topic":"run after 25 seconds","payload":"","payloadType":"date","repeat":"","crontab":"","once":true,"onceDelay":"25","x":420,"y":540,"wires":[["4ed514b7.739284"]]},{"id":"4ed514b7.739284","type":"link out","z":"8c83488b.98e1f","name":"to sensor input","links":["69f6e6b5.87d95"],"x":760,"y":500,"wires":[],"l":true},{"id":"6c498070.7dce48","type":"debug","z":"8c83488b.98e1f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"topic","targetType":"msg","x":120,"y":220,"wires":[]},{"id":"ef2a7030.15e8a","type":"inject","z":"8c83488b.98e1f","name":"run after 39 seconds","topic":"run after 39 seconds","payload":"","payloadType":"date","repeat":"","crontab":"","once":true,"onceDelay":"39","x":420,"y":580,"wires":[["4ed514b7.739284"]]},{"id":"2c5aa75e.8b029","type":"inject","z":"8c83488b.98e1f","name":"run after 50 seconds","topic":"run after 50 seconds","payload":"","payloadType":"date","repeat":"","crontab":"","once":true,"onceDelay":"50","x":420,"y":620,"wires":[["4ed514b7.739284"]]},{"id":"9769d6d4.102b4","type":"comment","z":"8c83488b.98e1f","name":"should report 5 second delay","info":"","x":140,"y":500,"wires":[]},{"id":"43a12dff.058074","type":"comment","z":"8c83488b.98e1f","name":"should report 9 second delay","info":"","x":140,"y":580,"wires":[]},{"id":"2365320d.e1a2f6","type":"comment","z":"8c83488b.98e1f","name":"should report 6 second delay","info":"","x":140,"y":620,"wires":[]}]

Thank you for your help and response. Will give a feedback soon.

Thank you. I will give it another go.

I believe all you need to do is add a function node with the following code. The function should be connected to the input of the counter node.

msg.reset = true;
return msg;