Up down counter

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?

translation:
tijdklok = timeclock
uur = hour

A suggestion:

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:

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

and this is the flow i've setup:

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

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)

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.

1 Like
[{"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}]

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

this is the final flow:

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

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.

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

and it gave this in response: image
is this going to be a problem?

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

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

1 Like

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.

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

1 Like