How to Reset the Counter

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"]]}]

1 Like

Please export your flow again and use:

three backticks `

your code

three backticks `

[{"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

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.

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.

[{"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"]]}]

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 ?

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 !

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 !

Did the flow I uploaded work?

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.

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 ?

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.]

Check this flow:

[{"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 ?

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

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 ?

@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.

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.

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?

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".