# How to Reset the Counter

**URL:** <https://discourse.nodered.org/t/how-to-reset-the-counter/13199>\
**Category:** General\
**Created:** [12 July 2019 04:27 UTC](https://discourse.nodered.org/t/how-to-reset-the-counter/13199 "2019-07-12T04:27:53Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![azrul](https://avatars.discourse-cdn.com/v4/letter/a/5fc32e/32.png) [@azrul](https://discourse.nodered.org/u/azrul)\
**Post date:** [12 July 2019 04:27 UTC](https://discourse.nodered.org/t/how-to-reset-the-counter/13199/1 "2019-07-12T04:27:53Z")

</div>

I have created flows to start a counter using function node when the sensor is sensing an object and I need to restart the counter when the sensor is sensing an object again. I'm new to NODE-RED. Any tips ??

[{"id":"70589088.60c16","type":"source\_in","z":"58e02bdc.9c0da4","device":"New Device - MODBUSRTU.1","sensor":"IR Sensor - 03\_MODBUS\_04","x":141,"y":656,"wires":[["d869c0a6.8b4e9"]]},{"id":"31e02fd2.6acb9","type":"debug","z":"58e02bdc.9c0da4","name":"","active":true,"console":"false","complete":"false","x":300,"y":847,"wires":},{"id":"cdfba21b.03b0a","type":"trigger","z":"58e02bdc.9c0da4","op1":"","op2":"1","op1type":"nul","op2type":"num","duration":"5","extend":false,"units":"s","reset":"","name":"","x":289,"y":737,"wires":[["f81110ec.91fd6"]]},{"id":"d869c0a6.8b4e9","type":"function","z":"58e02bdc.9c0da4","name":"Trigger Trial","func":"var HtoD = parseInt(msg.payload, 16);\n\n\n\n msg.payload=HtoD; \n return msg;\n","outputs":1,"noerr":0,"x":121,"y":714,"wires":[["4245d83.4c69f28"]]},{"id":"4245d83.4c69f28","type":"switch","z":"58e02bdc.9c0da4","name":"Switch","property":"payload","propertyType":"msg","rules":[{"t":"lt","v":"5000","vt":"num"},{"t":"else"}],"checkall":"true","outputs":2,"x":278,"y":700,"wires":[["cdfba21b.03b0a"],]},{"id":"f81110ec.91fd6","type":"function","z":"58e02bdc.9c0da4","name":"Counter","func":"for (var i=0; i\<400; i++)\n{\n node.send({payload:i});\n}\n","outputs":1,"noerr":0,"x":289,"y":773,"wires":[["74870591.3229bc"]]},{"id":"74870591.3229bc","type":"delay","z":"58e02bdc.9c0da4","name":"","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":299,"y":809,"wires":[["31e02fd2.6acb9"]]}]

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [12 July 2019 04:48 UTC](https://discourse.nodered.org/t/how-to-reset-the-counter/13199/2 "2019-07-12T04:48:22Z")

</div>

Please export your flow again and use:

three backticks `

```auto
your code

```

three backticks `

---

<div class="post-metadata">

**Author:** ![azrul](https://avatars.discourse-cdn.com/v4/letter/a/5fc32e/32.png) [@azrul](https://discourse.nodered.org/u/azrul)\
**Post date:** [12 July 2019 08:37 UTC](https://discourse.nodered.org/t/how-to-reset-the-counter/13199/6 "2019-07-12T08:37:02Z")

</div>

```auto
[{"id":"70589088.60c16","type":"source_in","z":"58e02bdc.9c0da4","device":"New Device - MODBUSRTU.1","sensor":"IR Sensor - 03_MODBUS_04","x":141,"y":656,"wires":[["d869c0a6.8b4e9"]]},{"id":"31e02fd2.6acb9","type":"debug","z":"58e02bdc.9c0da4","name":"","active":true,"console":"false","complete":"false","x":300,"y":847,"wires":[]},{"id":"cdfba21b.03b0a","type":"trigger","z":"58e02bdc.9c0da4","op1":"","op2":"1","op1type":"nul","op2type":"num","duration":"5","extend":false,"units":"s","reset":"","name":"","x":289,"y":737,"wires":[["f81110ec.91fd6"]]},{"id":"d869c0a6.8b4e9","type":"function","z":"58e02bdc.9c0da4","name":"Trigger Trial","func":"var HtoD = parseInt(msg.payload, 16);\n\n\n\n msg.payload=HtoD; \n return msg;\n","outputs":1,"noerr":0,"x":121,"y":714,"wires":[["4245d83.4c69f28"]]},{"id":"4245d83.4c69f28","type":"switch","z":"58e02bdc.9c0da4","name":"Switch","property":"payload","propertyType":"msg","rules":[{"t":"lt","v":"5000","vt":"num"},{"t":"else"}],"checkall":"true","outputs":2,"x":278,"y":700,"wires":[["cdfba21b.03b0a"],[]]},{"id":"f81110ec.91fd6","type":"function","z":"58e02bdc.9c0da4","name":"Counter","func":"for (var i=0; i<400; i++)\n{\n node.send({payload:i});\n}\n","outputs":1,"noerr":0,"x":289,"y":773,"wires":[["74870591.3229bc"]]},{"id":"74870591.3229bc","type":"delay","z":"58e02bdc.9c0da4","name":"","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":299,"y":809,"wires":[["31e02fd2.6acb9"]]}] 

```

Thank you

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [12 July 2019 09:12 UTC](https://discourse.nodered.org/t/how-to-reset-the-counter/13199/7 "2019-07-12T09:12:00Z")

</div>

I'll byte.

Ok, looking at the flow.

You are sending a counter counting up every second by 1. Fair enough.

This is started by an incoming message.

If another message comes in you want the counter to reset and start at 1 again?

So what is the 5 second delay at the start?

I'll have a go at getting something for you.

Back soon!  
I hope.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [12 July 2019 09:44 UTC](https://discourse.nodered.org/t/how-to-reset-the-counter/13199/8 "2019-07-12T09:44:26Z")

</div>

This is the flow I bashed out.

I haven't included all your original flow. I have from the `switch` node on.

There is a `DEMO` button to get things going. Pressing it again the counter restarts.  
As it should if there is a new signal received from the switch.

I had to add a new node to set the payload value to 0 because that is needed to reset the counter.

I also have to include another `function` node to send a `reset` command to the two other nodes.  
Hope this works for you.

```auto
[{"id":"c17e0ea2.05018","type":"switch","z":"f64521da.ed6a","name":"Switch","property":"payload","propertyType":"msg","rules":[{"t":"lt","v":"5000","vt":"num"},{"t":"else"}],"checkall":"true","outputs":2,"x":2050,"y":1060,"wires":[["185e706f.d0add","63b1f57a.37a55c"],[]]},{"id":"11b7eaee.2131e5","type":"delay","z":"f64521da.ed6a","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":2210,"y":1220,"wires":[["ecc63a1a.70a63"]]},{"id":"ecc63a1a.70a63","type":"function","z":"f64521da.ed6a","name":"increment value","func":"var value;\nvalue = parseInt(msg.payload);\nvalue = value + 1;\nmsg.payload = value;\nreturn msg;","outputs":1,"noerr":0,"x":2450,"y":1220,"wires":[["3ea56ada.3bc97e","e04a0f69.134b98"]]},{"id":"646c0f21.97c978","type":"inject","z":"f64521da.ed6a","name":"Demo","topic":"","payload":"0","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":2050,"y":1150,"wires":[["185e706f.d0add","c5c02271.b7b298"]]},{"id":"3ea56ada.3bc97e","type":"delay","z":"f64521da.ed6a","name":"","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":2220,"y":1310,"wires":[["11b7eaee.2131e5"]]},{"id":"e04a0f69.134b98","type":"debug","z":"f64521da.ed6a","name":"","active":true,"console":"false","complete":"false","x":2460,"y":1310,"wires":[]},{"id":"185e706f.d0add","type":"function","z":"f64521da.ed6a","name":"reset other nodes","func":"msg.reset = true;\nreturn msg;","outputs":1,"noerr":0,"x":2010,"y":1220,"wires":[["11b7eaee.2131e5","3ea56ada.3bc97e"]]},{"id":"c5c02271.b7b298","type":"delay","z":"f64521da.ed6a","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":2210,"y":1150,"wires":[["ecc63a1a.70a63"]]},{"id":"63b1f57a.37a55c","type":"function","z":"f64521da.ed6a","name":"set payload","func":"msg.payload = 0;\nreturn msg;","outputs":1,"noerr":0,"x":2030,"y":1110,"wires":[["c5c02271.b7b298"]]}]

```

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [12 July 2019 10:47 UTC](https://discourse.nodered.org/t/how-to-reset-the-counter/13199/9 "2019-07-12T10:47:40Z")

</div>

Please explain what you are trying to accomplish. Why do you want/need a counter ? When does it need to count ? When the object is lower than 5000 ?

---

<div class="post-metadata">

**Author:** ![azrul](https://avatars.discourse-cdn.com/v4/letter/a/5fc32e/32.png) [@azrul](https://discourse.nodered.org/u/azrul)\
**Post date:** [15 July 2019 03:50 UTC](https://discourse.nodered.org/t/how-to-reset-the-counter/13199/10 "2019-07-15T03:50:11Z")

</div>

So, here is my application.

Say, if I were to implement this system to the production line where the products are moving on the conveyor.

Once the sensor is detecting a unit, it will allow only 5 seconds delay (it can be 20s, 30s) before it starts the counter if there is any additional delay. The counter will sum up additional time delayed until the next unit arrives and store that into the database. Only then, the counter will reset and cycle continues.

Thank you in advance !

---

<div class="post-metadata">

**Author:** ![azrul](https://avatars.discourse-cdn.com/v4/letter/a/5fc32e/32.png) [@azrul](https://discourse.nodered.org/u/azrul)\
**Post date:** [15 July 2019 03:52 UTC](https://discourse.nodered.org/t/how-to-reset-the-counter/13199/11 "2019-07-15T03:52:01Z")

</div>

So, here is my application.

Say, if I were to implement this system to the production line where the products are moving on the conveyor.

Once the sensor is detecting a unit, it will allow only 5 seconds delay (it can be 20s, 30s) before it starts the counter if there is any additional delay. The counter will sum up additional time delayed until the next unit arrives and store that into the database. Only then, the counter will reset and cycle continues.

5000 is the output value that the sensor send when it detects an object.

Hope this clarifies.

Thank you !

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [15 July 2019 06:06 UTC](https://discourse.nodered.org/t/how-to-reset-the-counter/13199/12 "2019-07-15T06:06:27Z")

</div>

Did the flow I uploaded work?

---

<div class="post-metadata">

**Author:** ![azrul](https://avatars.discourse-cdn.com/v4/letter/a/5fc32e/32.png) [@azrul](https://discourse.nodered.org/u/azrul)\
**Post date:** [15 July 2019 08:10 UTC](https://discourse.nodered.org/t/how-to-reset-the-counter/13199/13 "2019-07-15T08:10:27Z")

</div>

The counter restarts when it is triggered by the sensor but the previous counter also continues. Plus, some random numbers which i couldn't figured out where they are coming from. I would prefer to use my old flow to restarts the counter if possible.

Thank you.

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [15 July 2019 08:16 UTC](https://discourse.nodered.org/t/how-to-reset-the-counter/13199/14 "2019-07-15T08:16:33Z")

</div>

I am still a little confused.

A counter counts in numbers,  
a delay is time.

Do you want to count the total delay in time, or do you want a (arbitrary) number ?

---

<div class="post-metadata">

**Author:** ![azrul](https://avatars.discourse-cdn.com/v4/letter/a/5fc32e/32.png) [@azrul](https://discourse.nodered.org/u/azrul)\
**Post date:** [15 July 2019 08:28 UTC](https://discourse.nodered.org/t/how-to-reset-the-counter/13199/15 "2019-07-15T08:28:33Z")

</div>

Initially, a counter counts in numbers only for data recording purposes  
and I didn't know that it could be count in time. If to count a counter in time is simpler, then please correct me.

I just want the message output to indicate the total loss time. Doesn't matter in time or in number.  
Whichever is better for message output and data recording purposes.

[A delay is a gap (in this case = 5 seconds) between the product units. If more than that, it is considered as a loss time which is the data i want to record.]

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [15 July 2019 09:25 UTC](https://discourse.nodered.org/t/how-to-reset-the-counter/13199/16 "2019-07-15T09:25:07Z")

</div>

Check this flow:

```auto
[{"id":"737ec844.a9a218","type":"trigger","z":"7feb1d47.422abc","op1":"","op2":"1","op1type":"nul","op2type":"num","duration":"5","extend":false,"units":"s","reset":"","bytopic":"all","name":"","x":385,"y":350,"wires":[["6ec839c8.f7578"]]},{"id":"a093533.65d163","type":"debug","z":"7feb1d47.422abc","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":695,"y":350,"wires":[]},{"id":"653bee85.b01298","type":"inject","z":"7feb1d47.422abc","name":"New Unit","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":350,"wires":[["368cd45a.3160a4"]]},{"id":"6ec839c8.f7578","type":"function","z":"7feb1d47.422abc","name":"","func":"c = msg.payload\nf = flow.get(\"counter\")\no = c+f\n\nflow.set(\"counter\",o)\nnode.status({fill:\"green\",shape:\"dot\",text:\"count:\"+o});\nreturn {payload:{counter:o}};","outputs":1,"noerr":0,"x":525,"y":350,"wires":[["a093533.65d163","737ec844.a9a218"]]},{"id":"368cd45a.3160a4","type":"function","z":"7feb1d47.422abc","name":"","func":"flow.set(\"counter\",0)\nreturn {payload:{unit:true}};\n\n\n\n","outputs":1,"noerr":0,"x":250,"y":350,"wires":[["737ec844.a9a218"]]}]

```

New unit sets a flow variable "counter", gets triggered, waits 5 seconds, count is increased. New unit arrives (click the button), counter is reset.

Is this what you mean ?

---

<div class="post-metadata">

**Author:** ![azrul](https://avatars.discourse-cdn.com/v4/letter/a/5fc32e/32.png) [@azrul](https://discourse.nodered.org/u/azrul)\
**Post date:** [15 July 2019 09:46 UTC](https://discourse.nodered.org/t/how-to-reset-the-counter/13199/18 "2019-07-15T09:46:19Z")

</div>

Counter in this case means the amount of time (seconds) taken after the '5 seconds delay' until the next unit arrives not the amount of product unit.

(1) Unit arrives  
(2) 5 seconds delay (allowed)  
(3) counter (count by time-seconds) - If there's any (if the unit arrives after 5 seconds, so there's no need for counter)  
(4) Unit arrives \>Counter reset  
(5) The cycle continues

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [15 July 2019 10:11 UTC](https://discourse.nodered.org/t/how-to-reset-the-counter/13199/19 "2019-07-15T10:11:02Z")

</div>

That is what my example does. You can calculate time by `counter * 5 seconds` no ?

Perhaps you should draw what you are trying to achieve. What should be the result ? The delay time between a unit and the next unit ?

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [15 July 2019 10:37 UTC](https://discourse.nodered.org/t/how-to-reset-the-counter/13199/20 "2019-07-15T10:37:52Z")

</div>

@bakman2 I think the idea is to only report the time when there isn't a unit every 5 seconds.  
(1) a unit arrives and a 5 second timer is started  
(2) a new unit arrives and the 5 second timer is restarted  
(3) a new unit arrives and the 5 second timer is restarted  
(4) an elapsed timer starts because it has been six seconds  
(5) after 12 seconds a new item arrives and two things happen

- 1 - the 5 second timer is restarted
- 2 - the elapsed timer is stopped and that value is reported.

@azrul - what happens in an edge case where the last item of the day arrives and nothing else raves. You need a way to indicate the line has stopped.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [15 July 2019 10:57 UTC](https://discourse.nodered.org/t/how-to-reset-the-counter/13199/21 "2019-07-15T10:57:32Z")

</div>

> [@azrul](#):
>
> Once the sensor is detecting a unit, it will allow only 5 seconds delay (it can be 20s, 30s) before it starts the counter if there is any additional delay. The counter will sum up additional time delayed until the next unit arrives and store that into the database. Only then, the counter will reset and cycle continues.

You are really changing how things work.

The first part can be resolved with the flow I posted. Just adjust the time in the first `DELAY` node that is starting the new cycle.

Adding the additional time is going to need more flow work.  
But seriously? 20 second delay saved in a data base to report a delay?

The original problem of counting up by one if there is more than a 5 second gap between inputs and resetting it if it is ok has been done in the flow.  
Adding a "new batch delay" (of ..... 20 to 30 seconds) is also covered by the `delay` node.

To detect any time the "production line" is stopped because of there being a \>5 second gap between objects on the line....

Take the output from the node which detects the \>5 second gap and send it to a counter node which adds up the times it sees an input and sends that value as an output.

That value then can be on sent to the database and things done there that need doing.

If you want to get even more tricky and the "first of the new batch" arrives before the flow has restarted, then you need to get the detector to then `flush` that `delay` node's contents and start the time from then and not after the pre-set time.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [15 July 2019 11:08 UTC](https://discourse.nodered.org/t/how-to-reset-the-counter/13199/22 "2019-07-15T11:08:41Z")

</div>

> [@azrul](#):
>
> The counter restarts when it is triggered by the sensor but the previous counter also continues. Plus, some random numbers which i couldn't figured out where they are coming from. I would prefer to use my old flow to restarts the counter if possible.

I have tested my flow again and I can't see any "random numbers".

Can you do a screen shot of the that part of the flow with what is before the `switch` node?

 ![Screenshot%20from%202019-07-15%2021-05-19](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/c/c2fc91801dd493b406c3039040a94c5840ddb565.png)

---

<div class="post-metadata">

**Author:** ![azrul](https://avatars.discourse-cdn.com/v4/letter/a/5fc32e/32.png) [@azrul](https://discourse.nodered.org/u/azrul)\
**Post date:** [16 July 2019 03:52 UTC](https://discourse.nodered.org/t/how-to-reset-the-counter/13199/23 "2019-07-16T03:52:47Z")

</div>

The message output keep counting the previous counter at the same time count the new one. Some of the numbers are repeating.

 ![Untitled1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/c/c4e91f4d36901a60badc8776ef462c86356e1489.jpeg)

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [16 July 2019 06:33 UTC](https://discourse.nodered.org/t/how-to-reset-the-counter/13199/24 "2019-07-16T06:33:54Z")

</div>

> [@azrul](#):
>
> The message output keep counting the previous counter at the same time count the new one. Some of the numbers are repeating.

Ah!

I believe the reason is what and where you are injecting.

The ONLY THING you should inject is a value (which is less than 500000) (or what ever you set the switch value)

Why you connected the `trigger 5seconds` node output to `reset other counters` is W R O N G

I though that was explained in what I posted.

What ever it is you are sending goes into the `switch` node only.

That is from your original flow. I left it there as a reference point.

Give it another go as my screen shot explained/showed. There was black text saying "Input as marked".

[Next page](https://discourse.nodered.org/t/how-to-reset-the-counter/13199.md?page=2)
