# Up down counter

**URL:** <https://discourse.nodered.org/t/up-down-counter/53785>\
**Category:** Dashboard\
**Created:** [15 November 2021 19:23 UTC](https://discourse.nodered.org/t/up-down-counter/53785 "2021-11-15T19:23:20Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![evanic14](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/evanic14/32/23480_2.png) [@evanic14](https://discourse.nodered.org/u/evanic14)\
**Post date:** [15 November 2021 19:23 UTC](https://discourse.nodered.org/t/up-down-counter/53785/1 "2021-11-15T19:23:21Z")

</div>

Hi, I would like to create a simple up and down counter. Tap the + button once, the value goes up. Tap the - button once the value goes down. There should aslo be a reset button. I have found two libraries one of them only counts up. The other one counts up and down but I cant use that data. Can you help me?

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/c/1c112419bfb9362d31378d2ccd5a5e0d28d4e6ec.png)

translation:  
tijdklok = timeclock  
uur = hour

---

<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:** [15 November 2021 20:07 UTC](https://discourse.nodered.org/t/up-down-counter/53785/2 "2021-11-15T20:07:47Z")

</div>

A suggestion:

 ![Untitled 6](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/2/62c1261a96461ae79931c5ce40b28d6529c9c7fe.jpeg)

---

<div class="post-metadata">

**Author:** ![evanic14](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/evanic14/32/23480_2.png) [@evanic14](https://discourse.nodered.org/u/evanic14)\
**Post date:** [15 November 2021 20:29 UTC](https://discourse.nodered.org/t/up-down-counter/53785/3 "2021-11-15T20:29:23Z")

</div>

hi. thanks for the reply. Your solution didn't work for me though. Probably because im doing something wrong again. This is my code in the function node:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/2/524d93d3b3adfca4cae34e7a202283106b0d30f4.png)

this is the output i was getting when i pressed the reset button:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/0/d0ee7e2d1f6f9e73c9c230b8d8f908720183ea2f.png)

---

<div class="post-metadata">

**Author:** ![evanic14](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/evanic14/32/23480_2.png) [@evanic14](https://discourse.nodered.org/u/evanic14)\
**Post date:** [15 November 2021 20:30 UTC](https://discourse.nodered.org/t/up-down-counter/53785/4 "2021-11-15T20:30:26Z")

</div>

and this is the flow i've setup:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/8/08775406efa4c87d1768163a0273791f323a9f57.png)

---

<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:** [15 November 2021 20:36 UTC](https://discourse.nodered.org/t/up-down-counter/53785/5 "2021-11-15T20:36:31Z")

</div>

Export your code and post it here using the preformatted text button.

---

<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:** [15 November 2021 20:55 UTC](https://discourse.nodered.org/t/up-down-counter/53785/6 "2021-11-15T20:55:16Z")

</div>

You need to initialise the global variable first time, and you must always return a message object, not just a value. I find it easier to keep the code in multiple lines so it is easier to understand. So something like (untested)

```auto
let count = flow.get('count') || 0 // initialise to 0 if undefined
msg.payload += count
flow.set('count', msg.payload)
return msg

```

Have a look at the docs page Working with Functions which will help, and also shows how you can easily debug functions using `node.warn()` in the function. Then you should be able to analyse what is happening and fix it for yourself if it is still not working.

---

<div class="post-metadata">

**Author:** ![evanic14](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/evanic14/32/23480_2.png) [@evanic14](https://discourse.nodered.org/u/evanic14)\
**Post date:** [15 November 2021 20:57 UTC](https://discourse.nodered.org/t/up-down-counter/53785/7 "2021-11-15T20:57:55Z")

</div>

```auto
[{"id":"92124d52.6defa","type":"ui_button","z":"455ed50a.5deadc","name":"+","group":"3f0457bf.87fa48","order":1,"width":4,"height":2,"passthru":true,"label":"","tooltip":"","color":"","bgcolor":"","className":"","icon":"expand_less","payload":"1","payloadType":"num","topic":"Topic","topicType":"msg","x":1010,"y":680,"wires":[["10304b9b.a0dda4"]]},{"id":"841e1d5f.13b9f","type":"ui_button","z":"455ed50a.5deadc","name":"-","group":"3f0457bf.87fa48","order":3,"width":4,"height":2,"passthru":true,"label":"","tooltip":"","color":"","bgcolor":"","className":"","icon":"expand_more","payload":"-1","payloadType":"num","topic":"Topic","topicType":"msg","x":1010,"y":720,"wires":[["10304b9b.a0dda4"]]},{"id":"5976be9f.9c54a","type":"ui_button","z":"455ed50a.5deadc","name":"reset","group":"3f0457bf.87fa48","order":3,"width":4,"height":"1","passthru":true,"label":"reset","tooltip":"","color":"","bgcolor":"red","className":"","icon":"","payload":"true","payloadType":"bool","topic":"Topic","topicType":"msg","x":1010,"y":760,"wires":[["b91031b3.bfdc8"]]},{"id":"10304b9b.a0dda4","type":"function","z":"455ed50a.5deadc","name":"","func":"msg = flow.set('count', flow.get('count') + msg.payload)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1210,"y":700,"wires":[["6bd4c04f.b2352"]]},{"id":"6bd4c04f.b2352","type":"debug","z":"455ed50a.5deadc","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1290,"y":760,"wires":[]},{"id":"b91031b3.bfdc8","type":"change","z":"455ed50a.5deadc","name":"to 0","rules":[{"t":"set","p":"count","pt":"flow","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":1130,"y":760,"wires":[[]]},{"id":"3f0457bf.87fa48","type":"ui_group","name":"Tijdklok","tab":"4ac8d4a5.3b285c","order":3,"disp":true,"width":4,"collapse":false,"className":""},{"id":"4ac8d4a5.3b285c","type":"ui_tab","name":"Controle","icon":"report","order":1,"disabled":false,"hidden":false}]

```

---

<div class="post-metadata">

**Author:** ![evanic14](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/evanic14/32/23480_2.png) [@evanic14](https://discourse.nodered.org/u/evanic14)\
**Post date:** [15 November 2021 21:03 UTC](https://discourse.nodered.org/t/up-down-counter/53785/8 "2021-11-15T21:03:41Z")

</div>

right, I managed to get it working because of the code you send me. thanks a lot.

this is the final flow:

```auto
[{"id":"92124d52.6defa","type":"ui_button","z":"455ed50a.5deadc","name":"+","group":"3f0457bf.87fa48","order":1,"width":4,"height":2,"passthru":true,"label":"","tooltip":"","color":"","bgcolor":"","className":"","icon":"expand_less","payload":"1","payloadType":"num","topic":"Topic","topicType":"msg","x":870,"y":740,"wires":[["10304b9b.a0dda4"]]},{"id":"841e1d5f.13b9f","type":"ui_button","z":"455ed50a.5deadc","name":"-","group":"3f0457bf.87fa48","order":3,"width":4,"height":2,"passthru":true,"label":"","tooltip":"","color":"","bgcolor":"","className":"","icon":"expand_more","payload":"-1","payloadType":"num","topic":"Topic","topicType":"msg","x":870,"y":780,"wires":[["10304b9b.a0dda4"]]},{"id":"5976be9f.9c54a","type":"ui_button","z":"455ed50a.5deadc","name":"reset","group":"3f0457bf.87fa48","order":3,"width":4,"height":"1","passthru":true,"label":"reset","tooltip":"","color":"","bgcolor":"red","className":"","icon":"","payload":"true","payloadType":"bool","topic":"Topic","topicType":"msg","x":870,"y":820,"wires":[["b91031b3.bfdc8"]]},{"id":"10304b9b.a0dda4","type":"function","z":"455ed50a.5deadc","name":"","func":"let count = flow.get('count') || 0 // initialise to 0 if undefined\nmsg.payload += count\nflow.set('count', msg.payload)\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1070,"y":760,"wires":[["6bd4c04f.b2352","cc79f07f.6231d"]]},{"id":"6bd4c04f.b2352","type":"debug","z":"455ed50a.5deadc","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1250,"y":800,"wires":[]},{"id":"b91031b3.bfdc8","type":"change","z":"455ed50a.5deadc","name":"to 0","rules":[{"t":"set","p":"count","pt":"flow","to":"-1","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":990,"y":820,"wires":[["10304b9b.a0dda4"]]},{"id":"cc79f07f.6231d","type":"ui_text","z":"455ed50a.5deadc","group":"3f0457bf.87fa48","order":2,"width":0,"height":0,"name":"text","label":"uur:","format":"{{msg.payload}}","layout":"row-center","className":"","x":1230,"y":760,"wires":[]},{"id":"3f0457bf.87fa48","type":"ui_group","name":"Tijdklok","tab":"4ac8d4a5.3b285c","order":3,"disp":true,"width":4,"collapse":false,"className":""},{"id":"4ac8d4a5.3b285c","type":"ui_tab","name":"Controle","icon":"report","order":1,"disabled":false,"hidden":false}]

```

---

<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:** [15 November 2021 21:08 UTC](https://discourse.nodered.org/t/up-down-counter/53785/9 "2021-11-15T21:08:35Z")

</div>

Glad you got it working.

I agree with Colin about not cramming too many operations into one line of code. I actually wrote my function exactly like his but compacted it as the label in my picture.

---

<div class="post-metadata">

**Author:** ![evanic14](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/evanic14/32/23480_2.png) [@evanic14](https://discourse.nodered.org/u/evanic14)\
**Post date:** [15 November 2021 21:09 UTC](https://discourse.nodered.org/t/up-down-counter/53785/10 "2021-11-15T21:09:38Z")

</div>

i've got a question though. I implemented the `node.warn()` function like this:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/c/bc9185c2e6bf8e4eb00e61bcbcd2a943f1fc1ec2.png)

and it gave this in response: ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/a/3a1019a78063765e43e6ec605a8e980511eb14b7.png)  
is this going to be a problem?

---

<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:** [15 November 2021 21:10 UTC](https://discourse.nodered.org/t/up-down-counter/53785/11 "2021-11-15T21:10:51Z")

</div>

```auto
node.warn("Count =", msg.payload)

```

Edit - sorry, + not comma. I'm thinking in python 😀

---

<div class="post-metadata">

**Author:** ![evanic14](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/evanic14/32/23480_2.png) [@evanic14](https://discourse.nodered.org/u/evanic14)\
**Post date:** [15 November 2021 21:11 UTC](https://discourse.nodered.org/t/up-down-counter/53785/12 "2021-11-15T21:11:00Z")

</div>

yeah i usualy do that aswell but when someone is helping me with my code i always copy that person so that its less like ill make a mistake.

---

<div class="post-metadata">

**Author:** ![evanic14](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/evanic14/32/23480_2.png) [@evanic14](https://discourse.nodered.org/u/evanic14)\
**Post date:** [15 November 2021 21:16 UTC](https://discourse.nodered.org/t/up-down-counter/53785/13 "2021-11-15T21:16:13Z")

</div>

ah, i was wondering why it wasnt working. now that i think about it it makes a lot of sense

edit: it is working now. thanks a lot

---

<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:** [29 November 2021 21:16 UTC](https://discourse.nodered.org/t/up-down-counter/53785/14 "2021-11-29T21:16:54Z")

</div>

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