# Function counter

**URL:** <https://discourse.nodered.org/t/function-counter/56616>\
**Category:** General\
**Created:** [13 January 2022 19:45 UTC](https://discourse.nodered.org/t/function-counter/56616 "2022-01-13T19:45:23Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![kwonfire](https://avatars.discourse-cdn.com/v4/letter/k/c68b51/32.png) [@kwonfire](https://discourse.nodered.org/u/kwonfire)\
**Post date:** [13 January 2022 19:45 UTC](https://discourse.nodered.org/t/function-counter/56616/1 "2022-01-13T19:45:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [13 January 2022 20:30 UTC](https://discourse.nodered.org/t/function-counter/56616/2 "2022-01-13T20:30:54Z")

</div>

> [@kwonfire](#):
>
> I am looking for a function script that counts the number of times a button is pressed in 60second

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

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

```

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [13 January 2022 20:34 UTC](https://discourse.nodered.org/t/function-counter/56616/3 "2022-01-13T20:34:43Z")

</div>

And Here is a low code way to achieve similar.

```auto
[{"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.

---

<div class="post-metadata">

**Author:** ![kwonfire](https://avatars.discourse-cdn.com/v4/letter/k/c68b51/32.png) [@kwonfire](https://discourse.nodered.org/u/kwonfire)\
**Post date:** [14 January 2022 02:09 UTC](https://discourse.nodered.org/t/function-counter/56616/4 "2022-01-14T02:09:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kwonfire](https://avatars.discourse-cdn.com/v4/letter/k/c68b51/32.png) [@kwonfire](https://discourse.nodered.org/u/kwonfire)\
**Post date:** [14 January 2022 02:10 UTC](https://discourse.nodered.org/t/function-counter/56616/5 "2022-01-14T02:10:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kwonfire](https://avatars.discourse-cdn.com/v4/letter/k/c68b51/32.png) [@kwonfire](https://discourse.nodered.org/u/kwonfire)\
**Post date:** [14 January 2022 02:35 UTC](https://discourse.nodered.org/t/function-counter/56616/6 "2022-01-14T02:35:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [14 January 2022 02:53 UTC](https://discourse.nodered.org/t/function-counter/56616/7 "2022-01-14T02:53:45Z")

</div>

> [@kwonfire](#):
>
> Still trying to make it work in a function node

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.

---

<div class="post-metadata">

**Author:** ![smanjunath211](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smanjunath211/32/95742_2.png) [@smanjunath211](https://discourse.nodered.org/u/smanjunath211)\
**Post date:** [14 January 2022 04:11 UTC](https://discourse.nodered.org/t/function-counter/56616/8 "2022-01-14T04:11:43Z")

</div>

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

![per_minute](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/3/230acfde09536f7866c25ffb603a0d6a2da5342a.gif)

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

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [15 March 2022 04:12 UTC](https://discourse.nodered.org/t/function-counter/56616/9 "2022-03-15T04:12:28Z")

</div>

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