How to build a button flow that has 3 functions 🤧

i need a flow to:
1-start counting
2-stop counting
3- reset counting
all using just a one button

thx in advance :slightly_smiling_face:

I posted a solution for the reset option in your other thread:

let counter = context.get("counter") || 0
if (msg.topic === "Reset") {
  // reset the counter
  counter = 0
} else {
  counter++
}
context.set("counter", counter)
msg.payload = counter
return msg;

To stop and start you could further extend the function, or you could use node-red-contrib-simple gate which can be used to block or allow messages to pass. Put this in front of the counter function to stop and start counting.

You said you wanted to use just one button to do this. From the point of view of the operator how do you want the button to operate?

If the button is at start state, a click could start counting, and change the button to stop state, another click could then stop counting, then change button to reset, clicking reset could then reset counter and change the button to start state again.

Is this what you were thinking ? It's possible but not sure if its very intuitive ?

What if you wanted to start and stop without reset ?

1 Like

This may be of help.

I wrote it a long time ago, and I think it does what you need.

[{"id":"1139a03d.d1ca","type":"change","z":"184dc884.7aba5f","name":"Reset","rules":[{"t":"set","p":"reset","pt":"msg","to":"foo","tot":"str"},{"t":"delete","p":"payload","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":610,"y":2270,"wires":[["fcb3cb17.693a9"]]},{"id":"900577c3.9deb4","type":"inject","z":"184dc884.7aba5f","name":"Reset","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":true,"onceDelay":"0.5","x":430,"y":2230,"wires":[["1139a03d.d1ca"]]},{"id":"820bd696.fbc028","type":"debug","z":"184dc884.7aba5f","name":"Four presses","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1180,"y":2370,"wires":[]},{"id":"525e3ec9.3bb6b8","type":"inject","z":"184dc884.7aba5f","name":"Start","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":273.33331298828125,"y":2201.111083984375,"wires":[["fcb3cb17.693a9","ba72e20d.299b8"]]},{"id":"fcb3cb17.693a9","type":"counter","z":"184dc884.7aba5f","name":"","init":"0","step":"1","lower":null,"upper":null,"mode":"increment","outputs":"1","x":620,"y":2200,"wires":[["9faf97df.4ce4a"]]},{"id":"4189a6d1.44073","type":"switch","z":"184dc884.7aba5f","name":"count presses","property":"count","propertyType":"flow","rules":[{"t":"eq","v":"1","vt":"num"},{"t":"eq","v":"2","vt":"num"},{"t":"eq","v":"3","vt":"num"},{"t":"eq","v":"4","vt":"num"}],"checkall":"true","repair":false,"outputs":4,"x":790,"y":2310,"wires":[["83d02113.4743b8"],["fbfe53e5.4fcae8"],["e22efc97.c6b018"],["d9a249bd.1f82a8"]],"info":"Just expand the flow.count to how ever many\npresses you want/need."},{"id":"ba72e20d.299b8","type":"trigger","z":"184dc884.7aba5f","op1":"","op2":"0","op1type":"nul","op2type":"str","duration":"1","extend":true,"units":"s","reset":"","bytopic":"all","name":"","x":440,"y":2310,"wires":[["62860129.ec7bd","4189a6d1.44073"]]},{"id":"9faf97df.4ce4a","type":"change","z":"184dc884.7aba5f","name":"Flow set","rules":[{"t":"set","p":"count","pt":"flow","to":"count","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":770,"y":2200,"wires":[["a018a094.13d14"]]},{"id":"ddd0ab58.0a3e58","type":"debug","z":"184dc884.7aba5f","name":"Tripple press","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1180,"y":2330,"wires":[]},{"id":"ed9739c.0628148","type":"debug","z":"184dc884.7aba5f","name":"Double press","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1180,"y":2290,"wires":[]},{"id":"8082f0.4ba02d1","type":"debug","z":"184dc884.7aba5f","name":"Single Press","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1180,"y":2250,"wires":[]},{"id":"83d02113.4743b8","type":"change","z":"184dc884.7aba5f","name":"Single Press","rules":[{"t":"set","p":"payload","pt":"msg","to":"Single Press","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1000,"y":2250,"wires":[["8082f0.4ba02d1"]]},{"id":"fbfe53e5.4fcae8","type":"change","z":"184dc884.7aba5f","name":"Double Press","rules":[{"t":"set","p":"payload","pt":"msg","to":"Double Press","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1010,"y":2290,"wires":[["ed9739c.0628148"]]},{"id":"e22efc97.c6b018","type":"change","z":"184dc884.7aba5f","name":"Tripple Press","rules":[{"t":"set","p":"payload","pt":"msg","to":"Tripple Press","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1000,"y":2330,"wires":[["ddd0ab58.0a3e58"]]},{"id":"d9a249bd.1f82a8","type":"change","z":"184dc884.7aba5f","name":"Four Press","rules":[{"t":"set","p":"payload","pt":"msg","to":"Four Press","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1000,"y":2370,"wires":[["820bd696.fbc028"]]},{"id":"a1783324.3f9f","type":"comment","z":"184dc884.7aba5f","name":"DELAY - Readme","info":"The DELAY time has to be a bit longer than how\nlong it takes you to press the button the\nmaximum number of times.\nRead information in the SWITCH node too!","x":820,"y":2120,"wires":[]},{"id":"660d35d0.b41aa4","type":"comment","z":"184dc884.7aba5f","name":"Input","info":"","x":430,"y":2160,"wires":[]},{"id":"f91932b.cacfb5","type":"comment","z":"184dc884.7aba5f","name":"Outputs","info":"Debug nodes are here now, but just connect\nthem to what ever you want.\nThe messages sent are in the nodes just before\nthe debug nodes.\n","x":1160,"y":2210,"wires":[]},{"id":"a018a094.13d14","type":"debug","z":"184dc884.7aba5f","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"count","targetType":"msg","x":1000,"y":2200,"wires":[]},{"id":"62860129.ec7bd","type":"delay","z":"184dc884.7aba5f","name":"Delay","pauseType":"delay","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":430,"y":2270,"wires":[["1139a03d.d1ca"]]}]
1 Like

If you are interested, I wrote this subflow which may suit your needs.

[{"id":"4be6db179f2af0b9","type":"subflow","name":"Press count","info":"Used to count button presses and send the number in a message.\nSet the *timeout* value in the `delay` variable.\n\nThen send the output message into a `switch` node to do wonderful things.\n\nThe output message is a `number`.","category":"","in":[{"x":60,"y":330,"wires":[{"id":"a457eafcdad07d0b"}]}],"out":[{"x":820,"y":210,"wires":[{"id":"900cc4787c0480a4","port":0}]}],"env":[{"name":"delay","type":"num","value":"","ui":{"type":"input","opts":{"types":["num"]}}}],"meta":{},"color":"#D8BFD8","icon":"node-red-dashboard/ui_button.png","status":{"x":820,"y":370,"wires":[{"id":"900476fafffa3ae3","port":0}]}},{"id":"c2137fa8aec03468","type":"inject","z":"4be6db179f2af0b9","name":"Reset","repeat":"","crontab":"","once":true,"onceDelay":"0.5","topic":"","payload":"","payloadType":"date","x":280,"y":290,"wires":[["5188ca7c92884164"]]},{"id":"de02a7b5bf7954d2","type":"trigger","z":"4be6db179f2af0b9","name":"","op1":"","op2":"0","op1type":"nul","op2type":"str","duration":"0.5","extend":true,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":300,"y":250,"wires":[["a14dd5822f1f6015","900cc4787c0480a4"]]},{"id":"5188ca7c92884164","type":"change","z":"4be6db179f2af0b9","name":"Reset","rules":[{"t":"set","p":"reset","pt":"msg","to":"foo","tot":"str"},{"t":"delete","p":"payload","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":500,"y":250,"wires":[["d5314c210f7d9a76"]]},{"id":"d5314c210f7d9a76","type":"counter","z":"4be6db179f2af0b9","name":"","init":"0","step":"1","lower":null,"upper":null,"mode":"increment","outputs":"1","x":460,"y":330,"wires":[["6a5f10e31345b897","bb0e17c295353204"]]},{"id":"6a5f10e31345b897","type":"change","z":"4be6db179f2af0b9","name":"Flow set","rules":[{"t":"set","p":"count","pt":"flow","to":"count","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":670,"y":280,"wires":[[]]},{"id":"a457eafcdad07d0b","type":"change","z":"4be6db179f2af0b9","name":"","rules":[{"t":"set","p":"delay","pt":"msg","to":"delay","tot":"env"}],"action":"","property":"","from":"","to":"","reg":false,"x":145,"y":330,"wires":[["d5314c210f7d9a76","de02a7b5bf7954d2"]],"l":false},{"id":"a14dd5822f1f6015","type":"change","z":"4be6db179f2af0b9","name":"","rules":[{"t":"delete","p":"delay","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":415,"y":250,"wires":[["5188ca7c92884164"]],"l":false},{"id":"900cc4787c0480a4","type":"function","z":"4be6db179f2af0b9","name":"","func":"msg.payload = flow.get(\"count\");\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":460,"y":210,"wires":[[]]},{"id":"bb0e17c295353204","type":"function","z":"4be6db179f2af0b9","name":"CounterValue","func":"node.status({text: \"Counting \" + msg.count});\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":650,"y":330,"wires":[[]]},{"id":"900476fafffa3ae3","type":"status","z":"4be6db179f2af0b9","name":"","scope":["bb0e17c295353204"],"x":670,"y":370,"wires":[[]]},{"id":"f4d82258ab1dacf8","type":"subflow:4be6db179f2af0b9","z":"b25fd092.bcc758","name":"","env":[{"name":"delay","value":"400","type":"num"}],"x":4170,"y":490,"wires":[["c39072fc78f48cb4"]]}]

It can do more than you want, but I hope it will meet your needs.

(actually it probably won't.)
But it could be a way to do what you want.

1 Like

Ok, this is a flow that should do what you want.

Yaeh, I've used my subflow, but .......

One button.

1 press to start.
2 presses to stop
3 presses to reset/restart.

You will have to do those parts. This does the GUI side.

[{"id":"4be6db179f2af0b9","type":"subflow","name":"Press count","info":"Used to count button presses and send the number in a message.\nSet the *timeout* value in the `delay` variable.\n\nThen send the output message into a `switch` node to do wonderful things.\n\nThe output message is a `number`.","category":"","in":[{"x":60,"y":330,"wires":[{"id":"a457eafcdad07d0b"}]}],"out":[{"x":820,"y":210,"wires":[{"id":"900cc4787c0480a4","port":0}]}],"env":[{"name":"delay","type":"num","value":"","ui":{"type":"input","opts":{"types":["num"]}}}],"meta":{},"color":"#D8BFD8","icon":"node-red-dashboard/ui_button.png","status":{"x":820,"y":370,"wires":[{"id":"900476fafffa3ae3","port":0}]}},{"id":"c2137fa8aec03468","type":"inject","z":"4be6db179f2af0b9","name":"Reset","repeat":"","crontab":"","once":true,"onceDelay":"0.5","topic":"","payload":"","payloadType":"date","x":280,"y":290,"wires":[["5188ca7c92884164"]]},{"id":"de02a7b5bf7954d2","type":"trigger","z":"4be6db179f2af0b9","name":"","op1":"","op2":"0","op1type":"nul","op2type":"str","duration":"0.5","extend":true,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":300,"y":250,"wires":[["a14dd5822f1f6015","900cc4787c0480a4"]]},{"id":"5188ca7c92884164","type":"change","z":"4be6db179f2af0b9","name":"Reset","rules":[{"t":"set","p":"reset","pt":"msg","to":"foo","tot":"str"},{"t":"delete","p":"payload","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":500,"y":250,"wires":[["d5314c210f7d9a76"]]},{"id":"d5314c210f7d9a76","type":"counter","z":"4be6db179f2af0b9","name":"","init":"0","step":"1","lower":null,"upper":null,"mode":"increment","outputs":"1","x":460,"y":330,"wires":[["6a5f10e31345b897","bb0e17c295353204"]]},{"id":"6a5f10e31345b897","type":"change","z":"4be6db179f2af0b9","name":"Flow set","rules":[{"t":"set","p":"count","pt":"flow","to":"count","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":670,"y":280,"wires":[[]]},{"id":"a457eafcdad07d0b","type":"change","z":"4be6db179f2af0b9","name":"","rules":[{"t":"set","p":"delay","pt":"msg","to":"delay","tot":"env"}],"action":"","property":"","from":"","to":"","reg":false,"x":145,"y":330,"wires":[["d5314c210f7d9a76","de02a7b5bf7954d2"]],"l":false},{"id":"a14dd5822f1f6015","type":"change","z":"4be6db179f2af0b9","name":"","rules":[{"t":"delete","p":"delay","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":415,"y":250,"wires":[["5188ca7c92884164"]],"l":false},{"id":"900cc4787c0480a4","type":"function","z":"4be6db179f2af0b9","name":"","func":"msg.payload = flow.get(\"count\");\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":460,"y":210,"wires":[[]]},{"id":"bb0e17c295353204","type":"function","z":"4be6db179f2af0b9","name":"CounterValue","func":"node.status({text: \"Counting \" + msg.count});\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":650,"y":330,"wires":[[]]},{"id":"900476fafffa3ae3","type":"status","z":"4be6db179f2af0b9","name":"","scope":["bb0e17c295353204"],"x":670,"y":370,"wires":[[]]},{"id":"4291b6e7e088ae98","type":"subflow:4be6db179f2af0b9","z":"b25fd092.bcc758","name":"","env":[{"name":"delay","value":"400","type":"num"}],"x":4396,"y":914,"wires":[["e08bf10e7b37e5f8"]]},{"id":"b3cf278e741133cc","type":"ui_button","z":"b25fd092.bcc758","name":"MULTIBUTTONEXAMPLE","group":"765e8e16619c94e2","order":2,"width":"6","height":"1","passthru":false,"label":"{{msg.txt}}","tooltip":"","color":"","bgcolor":"","icon":"","payload":"X","payloadType":"str","topic":"topic","topicType":"msg","x":4176,"y":914,"wires":[["4291b6e7e088ae98"]]},{"id":"e08bf10e7b37e5f8","type":"switch","z":"b25fd092.bcc758","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"num"},{"t":"eq","v":"1","vt":"num"},{"t":"eq","v":"2","vt":"num"},{"t":"eq","v":"3","vt":"num"}],"checkall":"true","repair":false,"outputs":4,"x":4546,"y":914,"wires":[[],["26ae77da7437eca1","a129df32ab1dffd8"],["17cf118e70dbbce5","fa75f646e91478f6"],["019457c6b33ba31d","00ec8af791520ad0"]]},{"id":"d199d0051f6f3a6e","type":"change","z":"b25fd092.bcc758","name":"","rules":[{"t":"set","p":"txt","pt":"msg","to":"Press to start","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":4220,"y":870,"wires":[["b3cf278e741133cc"]]},{"id":"26ae77da7437eca1","type":"change","z":"b25fd092.bcc758","name":"","rules":[{"t":"set","p":"txt","pt":"msg","to":"Press two times to stop counting","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":4716,"y":884,"wires":[["b3cf278e741133cc"]]},{"id":"17cf118e70dbbce5","type":"change","z":"b25fd092.bcc758","name":"","rules":[{"t":"set","p":"txt","pt":"msg","to":"Press three times to reset","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":4716,"y":924,"wires":[["b3cf278e741133cc"]]},{"id":"019457c6b33ba31d","type":"delay","z":"b25fd092.bcc758","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":4230,"y":950,"wires":[["d199d0051f6f3a6e"]]},{"id":"a129df32ab1dffd8","type":"debug","z":"b25fd092.bcc758","name":"Start","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":4700,"y":980,"wires":[]},{"id":"00ec8af791520ad0","type":"debug","z":"b25fd092.bcc758","name":"Reset","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":4700,"y":1060,"wires":[]},{"id":"fa75f646e91478f6","type":"debug","z":"b25fd092.bcc758","name":"Stop","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":4700,"y":1020,"wires":[]},{"id":"56b0101d4a5ed8be","type":"inject","z":"b25fd092.bcc758","name":"Setup","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"setup","payloadType":"str","x":4020,"y":870,"wires":[["d199d0051f6f3a6e"]]},{"id":"765e8e16619c94e2","type":"ui_group","name":"Group 3","tab":"9e1ed5e519ff655f","order":3,"disp":true,"width":6},{"id":"9e1ed5e519ff655f","type":"ui_tab","name":"TEST","icon":"dashboard","order":39,"disabled":false,"hidden":false}]
1 Like

yes that what i was thinking, the project required the button to start stop and then reset counting, no need to start from last number

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