Node-red Switch node counter timestamp. How can I start and stop output

Create timestamp repeatedly on the Inject node
When Switch node is on The counter starts at 1 to 20, If the counter value exceeds 20, counter from 1 again.
debug node outputs timestamp Output up to 20, Until the Switch node is turned off.
When Switch node is off stop debug node.
Switch node is on debug node work again.

Sorry for my english

Hi, Welcome.

See if this works for you.

[{"id":"edfb3b9a4c6ae411","type":"function","z":"22fb22405eb6a6b6","name":"Counter","func":"var count = context.get('count')||0;\n\nif (msg.topic == \"reset\") {\n    count = 0;\n    context.set('count',0);\n}\nelse {\n    count += 1;\n    context.set('count',count);\n}\n\n// make it part of the outgoing msg object\nmsg.count = count;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":700,"y":440,"wires":[["5a49a79602fe3e66","d884732dd0743541"]]},{"id":"733e28e05cdfdceb","type":"change","z":"22fb22405eb6a6b6","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"reset","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":710,"y":500,"wires":[["edfb3b9a4c6ae411"]]},{"id":"7d1150525de998d2","type":"switch","z":"22fb22405eb6a6b6","name":"","property":"payload","propertyType":"msg","rules":[{"t":"true"},{"t":"false"}],"checkall":"true","repair":false,"outputs":2,"x":510,"y":440,"wires":[["edfb3b9a4c6ae411"],[]]},{"id":"5a49a79602fe3e66","type":"debug","z":"22fb22405eb6a6b6","name":"debug 125","active":false,"tosidebar":true,"console":false,"tostatus":true,"complete":"count","targetType":"msg","statusVal":"count","statusType":"auto","x":950,"y":380,"wires":[]},{"id":"d884732dd0743541","type":"switch","z":"22fb22405eb6a6b6","name":"","property":"count","propertyType":"msg","rules":[{"t":"lte","v":"20","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":890,"y":440,"wires":[["3df07ef81eb42593"],["733e28e05cdfdceb"]]},{"id":"c2f29dd420ed2150","type":"ui_switch","z":"22fb22405eb6a6b6","name":"","label":"Control ","tooltip":"","group":"a664f24e9358412a","order":1,"width":"5","height":"2","passthru":true,"decouple":"false","topic":"topic","topicType":"msg","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","animate":false,"className":"","x":380,"y":440,"wires":[["7d1150525de998d2"]]},{"id":"3df07ef81eb42593","type":"ui_text","z":"22fb22405eb6a6b6","group":"a664f24e9358412a","order":2,"width":"7","height":"2","name":"","label":"Count","format":"{{msg.count}}","layout":"row-spread","className":"","x":1030,"y":440,"wires":[]},{"id":"02d392fd948e9dde","type":"inject","z":"22fb22405eb6a6b6","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"1","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":190,"y":440,"wires":[["c2f29dd420ed2150"]]},{"id":"a664f24e9358412a","type":"ui_group","name":"Demo","tab":"7d501f2d3ff3af24","order":1,"disp":false,"width":"24","collapse":false,"className":""},{"id":"7d501f2d3ff3af24","type":"ui_tab","name":"Demo","icon":"dashboard","order":16,"disabled":false,"hidden":false}]
1 Like

It's good!

But output only shows the count number.

I also need outputs Timestamps too.

Thank you very much for your answer.

its difficult to understand your requirement, so if you stop the counter, you want to reset also, or you want different controls ?
check this if it is ok for you.

[{"id":"edfb3b9a4c6ae411","type":"function","z":"22fb22405eb6a6b6","name":"Counter","func":"var count = context.get('count')||0;\n\nif (msg.topic == \"reset\") {\n    count = 0;\n    context.set('count',0);\n}\nelse {\n    count += 1;\n    context.set('count',count);\n}\n\n// make it part of the outgoing msg object\nmsg.count = count;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":780,"y":460,"wires":[["d884732dd0743541"]]},{"id":"733e28e05cdfdceb","type":"change","z":"22fb22405eb6a6b6","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"reset","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":890,"y":520,"wires":[["edfb3b9a4c6ae411"]]},{"id":"19ab36768e9283b9","type":"gate","z":"22fb22405eb6a6b6","name":"","controlTopic":"control","defaultState":"closed","openCmd":"open","closeCmd":"close","toggleCmd":"toggle","defaultCmd":"default","statusCmd":"status","persist":false,"storeName":"memoryOnly","x":610,"y":460,"wires":[["edfb3b9a4c6ae411"]]},{"id":"d884732dd0743541","type":"switch","z":"22fb22405eb6a6b6","name":"","property":"count","propertyType":"msg","rules":[{"t":"lt","v":"20","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":910,"y":460,"wires":[["cded36d639543e15"],["733e28e05cdfdceb"]]},{"id":"ce424b25aeb8c9e3","type":"ui_button","z":"22fb22405eb6a6b6","name":"","group":"a664f24e9358412a","order":2,"width":10,"height":2,"passthru":false,"label":"Reset","tooltip":"","color":"black","bgcolor":"red","className":"","icon":"","payload":"true","payloadType":"bool","topic":"topic","topicType":"msg","x":710,"y":520,"wires":[["733e28e05cdfdceb"]]},{"id":"84c66340f2d4cd5a","type":"change","z":"22fb22405eb6a6b6","name":"Open","rules":[{"t":"set","p":"payload","pt":"msg","to":"open","tot":"str"},{"t":"set","p":"topic","pt":"msg","to":"control","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":510,"y":540,"wires":[["19ab36768e9283b9"]]},{"id":"4069f8f8e6877074","type":"change","z":"22fb22405eb6a6b6","name":"Close","rules":[{"t":"set","p":"payload","pt":"msg","to":"close","tot":"str"},{"t":"set","p":"topic","pt":"msg","to":"control","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":510,"y":580,"wires":[["19ab36768e9283b9"]]},{"id":"02d392fd948e9dde","type":"inject","z":"22fb22405eb6a6b6","name":"","props":[{"p":"payload"}],"repeat":"1","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":430,"y":460,"wires":[["19ab36768e9283b9"]]},{"id":"cded36d639543e15","type":"change","z":"22fb22405eb6a6b6","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"","tot":"date"},{"t":"set","p":"payload1","pt":"msg","to":"count","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1080,"y":460,"wires":[["3df07ef81eb42593","5a49a79602fe3e66"]]},{"id":"7d1150525de998d2","type":"switch","z":"22fb22405eb6a6b6","name":"","property":"payload","propertyType":"msg","rules":[{"t":"true"},{"t":"false"}],"checkall":"true","repair":false,"outputs":2,"x":370,"y":560,"wires":[["84c66340f2d4cd5a"],["4069f8f8e6877074"]]},{"id":"3df07ef81eb42593","type":"ui_text","z":"22fb22405eb6a6b6","group":"a664f24e9358412a","order":3,"width":10,"height":2,"name":"Display","label":"{{payload1}}","format":"{{payload | date:'dd-MMM-yyyy HH:mm:ss'}}","layout":"col-center","className":"","x":1260,"y":460,"wires":[]},{"id":"5a49a79602fe3e66","type":"debug","z":"22fb22405eb6a6b6","name":"debug 125","active":false,"tosidebar":true,"console":false,"tostatus":true,"complete":"true","targetType":"full","statusVal":"count","statusType":"msg","x":1270,"y":380,"wires":[]},{"id":"c2f29dd420ed2150","type":"ui_switch","z":"22fb22405eb6a6b6","name":"","label":"Control ","tooltip":"","group":"a664f24e9358412a","order":1,"width":10,"height":2,"passthru":true,"decouple":"false","topic":"topic","topicType":"msg","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","animate":false,"className":"","x":240,"y":560,"wires":[["7d1150525de998d2"]]},{"id":"a664f24e9358412a","type":"ui_group","name":"Demo","tab":"7d501f2d3ff3af24","order":1,"disp":false,"width":"10","collapse":false,"className":""},{"id":"7d501f2d3ff3af24","type":"ui_tab","name":"Demo","icon":"dashboard","order":16,"disabled":false,"hidden":false}]

Edit:

you would need to install a contrib node

1 Like

Your are amazing.

Thank you.

I'll going to connect this to the redis.

good night!

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