# Counter on rising edge only

**URL:** https://discourse.nodered.org/t/counter-on-rising-edge-only/79821
**Category:** General
**Created:** [14 July 2023 14:28 UTC](https://discourse.nodered.org/t/counter-on-rising-edge-only/79821 "2023-07-14T14:28:09Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Smidaimene](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smidaimene/32/81319_2.png) [@Smidaimene](https://discourse.nodered.org/u/Smidaimene)
#### Post date: [14 July 2023 14:28 UTC](https://discourse.nodered.org/t/counter-on-rising-edge-only/79821/1 "2023-07-14T14:28:09Z")

</div>

I am using rpi to calculate number of pieces  
I need to increment each rising edge through the rpi  
Then if the switch ui node is on the counter stops and if it is off it increments from the last value it stopped  
It returns to 0 after 8hours  
any help?

---

<div class="post-metadata">

### Author: ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)
#### Post date: [14 July 2023 14:39 UTC](https://discourse.nodered.org/t/counter-on-rising-edge-only/79821/2 "2023-07-14T14:39:41Z")

</div>

Hello and welcome to the Forum.

It would help if you can share what you have tried so far and which part is causing the problem.  
e.g. Can you share your Node-RED flow.

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [14 July 2023 14:54 UTC](https://discourse.nodered.org/t/counter-on-rising-edge-only/79821/3 "2023-07-14T14:54:18Z")

</div>

By default the UI switch node sends a single boolean payload **true** when it changes from OFF to ON and **false** when it changes from ON to OFF.

So your counter (maybe a function node, using a context variable) only needs to count messages with payload **true**.  
Either test it in the function or use a switch node (not a ui-switch) to only pass messages where msg.payload is **true**.

 ![Untitled 1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/d/2dfc0c3145399ab0ee75b02d7651d6ffbf98421b.jpeg)

---

<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: [12 September 2023 14:55 UTC](https://discourse.nodered.org/t/counter-on-rising-edge-only/79821/4 "2023-09-12T14:55:08Z")

</div>

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