Function counter

I have looked for this thinking it is a basic function script but I cant find anything.

I am looking for a function script that counts the number of times a button is pressed in 60second and only outputs the number of times it was pressed after that 60seconds. I was able to make a simple counter but it output every time a press the button 1, 2, 3, 1, 2, and so on.

Really id like it to count the number of button press then based on that set the var to true or false or have the function node have different output and each have its own number association.

any help would be great

Here's something to play around with .. needs some modifications
its based on a timer and Context

[{"id":"ccad4c8e4284effb","type":"inject","z":"54efb553244c241f","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":360,"y":1180,"wires":[["26999229f59011bd"]]},{"id":"26999229f59011bd","type":"function","z":"54efb553244c241f","name":"","func":"let counter = flow.get(\"counter\") || 0;\n\n// if the timer is running\nif (flow.get(\"timer\")) {\n    node.status({fill:\"green\",shape:\"ring\",text:\"Counting..\"});\n    counter++\n    flow.set(\"counter\", counter)\n    node.send({ payload: counter })\n}\nelse {\n    counter++\n    flow.set(\"counter\", counter)\n    node.send({ payload: counter })\n    // start timer\n    flow.set(\"timer\", setTimeout(() => {\n        flow.set(\"counter\", undefined)\n        flow.set(\"timer\", undefined)\n        node.status({});\n    }, 10000))\n    node.status({fill:\"green\",shape:\"ring\",text:\"Counting..\"});\n}\n\n\n\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":540,"y":1180,"wires":[["d780b62681bc4a94"]]},{"id":"d780b62681bc4a94","type":"debug","z":"54efb553244c241f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":720,"y":1180,"wires":[]}]

And Here is a low code way to achieve similar.

[{"id":"3632a93e.af1496","type":"inject","z":"c791cbc0.84f648","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"str","x":50,"y":700,"wires":[["8b5cd1d9.c24818"]]},{"id":"8b5cd1d9.c24818","type":"join","z":"c791cbc0.84f648","name":"60 sec","mode":"custom","build":"string","property":"payload","propertyType":"msg","key":"topic","joiner":"","joinerType":"str","accumulate":false,"timeout":"60","count":"","reduceRight":false,"reduceExp":"$A & $$.payload","reduceInit":"","reduceInitType":"str","reduceFixup":"$length($A)","x":180,"y":700,"wires":[["d585a8df.4eefc"]]},{"id":"d585a8df.4eefc","type":"change","z":"c791cbc0.84f648","name":"convert to number or if wish T/F","rules":[{"t":"set","p":"payload","pt":"msg","to":"$length($$.payload)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":420,"y":700,"wires":[["e759132a.fac86"]]},{"id":"e759132a.fac86","type":"switch","z":"c791cbc0.84f648","name":"output based on number or T/F","property":"payload","propertyType":"msg","rules":[{"t":"lt","v":"5","vt":"num"},{"t":"gte","v":"5","vt":"num"}],"checkall":"false","repair":false,"outputs":2,"x":760,"y":700,"wires":[["02f1fafdc71e278d"],["35ce92ac.60e8ae"]]},{"id":"02f1fafdc71e278d","type":"debug","z":"c791cbc0.84f648","name":"less than","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":630,"y":640,"wires":[]},{"id":"35ce92ac.60e8ae","type":"debug","z":"c791cbc0.84f648","name":"greater than","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":640,"y":760,"wires":[]}]

You could package it in a Subflow with as many outs as you wish.

1 Like

Thank you for replying. I tried that and it out puts 1,2,3,4xxx did see the timer working. Im going to try and work with it and see if I can make it work.

thank you. this worked and if I mess around with it some more. This may work just have to see.

Still trying to make it work in a function node. It seems like others would have wanted to do this too?

What have you tried ?

With some re-arranging of the commands i shared in my example you can make it work ..
then with a Switch node like E1cid showed in his example you can make a decision for true or false.

This is not using a function node, but to just check my understanding of your requirement is this what expected ?

per_minute

[{"id":"3a8364f7d496b306","type":"inject","z":"2590bb8249a56fde","name":"","props":[{"p":"payload"}],"repeat":"1","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":170,"y":520,"wires":[["1a8f97b04d99b040"]]},{"id":"1a8f97b04d99b040","type":"Message Counter","z":"2590bb8249a56fde","name":"","units":"minutes","interval":"1","alignToClock":true,"generator":"internal","x":350,"y":520,"wires":[["7599d93080973614"],["b119589dbc06a85a"]]},{"id":"7599d93080973614","type":"debug","z":"2590bb8249a56fde","name":"Per Minute","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":710,"y":460,"wires":[]},{"id":"b119589dbc06a85a","type":"counter","z":"2590bb8249a56fde","inc":1,"name":"","x":540,"y":540,"wires":[["235db53def516772"]]},{"id":"235db53def516772","type":"debug","z":"2590bb8249a56fde","name":"Total","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":690,"y":540,"wires":[]}]

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