# Counter node with gpio

**URL:** <https://discourse.nodered.org/t/counter-node-with-gpio/2456>\
**Category:** General\
**Created:** [21 August 2018 23:02 UTC](https://discourse.nodered.org/t/counter-node-with-gpio/2456 "2018-08-21T23:02:11Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ghbisson](https://avatars.discourse-cdn.com/v4/letter/g/db5fbb/32.png) [@Ghbisson](https://discourse.nodered.org/u/Ghbisson)\
**Post date:** [21 August 2018 23:02 UTC](https://discourse.nodered.org/t/counter-node-with-gpio/2456/1 "2018-08-21T23:02:11Z")

</div>

I use a counter node to measure rain fall at my home. It sends a pulse every 0.2 mm but the node reads the value twice. Is there anyone who knows how this block counts i.e. on a rising edge falling edge both ?

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [22 August 2018 07:47 UTC](https://discourse.nodered.org/t/counter-node-with-gpio/2456/2 "2018-08-22T07:47:41Z")

</div>

I would guess the counter counts any input it sees - and the gpio will be producing a 1 and 0 - so as you said both edges... - You can either divide the count by 2 ... or use a switch node to only let (say) the 1 value through to the counter.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [22 August 2018 08:25 UTC](https://discourse.nodered.org/t/counter-node-with-gpio/2456/3 "2018-08-22T08:25:41Z")

</div>

Which counter node, I see from [flows.nodered.org](http://flows.nodered.org) that there are several. It may be node-red-contrib-counter for example, or it may be one of the others.

---

<div class="post-metadata">

**Author:** ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)\
**Post date:** [22 August 2018 08:28 UTC](https://discourse.nodered.org/t/counter-node-with-gpio/2456/4 "2018-08-22T08:28:34Z")

</div>

Try a RBE node?

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [22 August 2018 09:45 UTC](https://discourse.nodered.org/t/counter-node-with-gpio/2456/5 "2018-08-22T09:45:56Z")

</div>

RBE won't help... the gpio will send 1 then 0 - so will go through RBE... - the counter will count any input so will count the 1 and then the 0 ... - so you just want to send the 1 (or the 0) .

---

<div class="post-metadata">

**Author:** ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)\
**Post date:** [22 August 2018 09:48 UTC](https://discourse.nodered.org/t/counter-node-with-gpio/2456/6 "2018-08-22T09:48:27Z")

</div>

Oops, didn't think that one through at all Dave!

---

<div class="post-metadata">

**Author:** ![davidcgu](https://avatars.discourse-cdn.com/v4/letter/d/e8c25b/32.png) [@davidcgu](https://discourse.nodered.org/u/davidcgu)\
**Post date:** [22 August 2018 11:05 UTC](https://discourse.nodered.org/t/counter-node-with-gpio/2456/7 "2018-08-22T11:05:52Z")

</div>

Just filter with a switch if msg.payload = 1 so you eliminate the zeros..

---

<div class="post-metadata">

**Author:** ![Ghbisson](https://avatars.discourse-cdn.com/v4/letter/g/db5fbb/32.png) [@Ghbisson](https://discourse.nodered.org/u/Ghbisson)\
**Post date:** [23 August 2018 06:22 UTC](https://discourse.nodered.org/t/counter-node-with-gpio/2456/8 "2018-08-23T06:22:04Z")

</div>

The node I use is  
[https://www.npmjs.com/package/node-red-contrib-counter](https://www.npmjs.com/package/node-red-contrib-counter)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [23 August 2018 07:40 UTC](https://discourse.nodered.org/t/counter-node-with-gpio/2456/9 "2018-08-23T07:40:21Z")

</div>

> [@Ghbisson](#):
>
> Is there anyone who knows how this block counts i.e. on a rising edge falling edge both

The first line of the Usage section in the readme of nod-red-contrib-counter reads "By default the counter will be incremented for every inbound message ". It takes no notice of the contents. If you only want it to count when the value is now 1, for example, then you can use a Switch node to only pass on those messages to the counter, as has already been suggested I think.

---

<div class="post-metadata">

**Author:** ![Ghbisson](https://avatars.discourse-cdn.com/v4/letter/g/db5fbb/32.png) [@Ghbisson](https://discourse.nodered.org/u/Ghbisson)\
**Post date:** [23 August 2018 22:50 UTC](https://discourse.nodered.org/t/counter-node-with-gpio/2456/10 "2018-08-23T22:50:17Z")

</div>

Finally with a switch node everything is fine. It counts exactly as expected. I also learn how to work with the switch node! Thanks for the help!

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [24 August 2018 00:06 UTC](https://discourse.nodered.org/t/counter-node-with-gpio/2456/11 "2018-08-24T00:06:30Z")

</div>

A bit late to the party, I know -- but for future reference, you could use `https://flows.nodered.org/node/node-red-contrib-statistics` to calculate a "sum" of the payloads received. Since half of your `msg.payload` values are 0, I believe it would give you the same answer without having to use a `switch` node to filter out half of the msgs...

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [24 August 2018 08:31 UTC](https://discourse.nodered.org/t/counter-node-with-gpio/2456/12 "2018-08-24T08:31:36Z")

</div>

> [@shrickus](#):
>
> but for future reference, you could use `https://flows.nodered.org/node/node-red-contrib-statistics` to calculate a "sum" of the payloads received

I am not certain, but I think that will retain all previous values received in order to perform the statistical calculations. If that is correct then it is not appropriate for a situation like this as the size of the array will grow without bounds. Also the time taken to calculate the average may well get longer and longer.
