Simple pulse generator

Hello,
I'm learning how to control loops in NR, but can't seem to debug this simple loop.

[{"id":"43361163d49b32dc","type":"inject","z":"82bb81a69c76160e","name":"Interval 5s","props":[{"p":"payload.time","v":"","vt":"date"},{"p":"delay","v":"5000","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":320,"y":2760,"wires":[["c7204267634c3125","bc2f710c2bd7dbf8"]]},{"id":"04d691146cc01a24","type":"delay","z":"82bb81a69c76160e","name":"","pauseType":"rate","timeout":"2","timeoutUnits":"seconds","rate":"1","nbRateUnits":"10","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":true,"allowrate":true,"outputs":1,"x":600,"y":2860,"wires":[["bc2f710c2bd7dbf8","c7204267634c3125"]]},{"id":"c7204267634c3125","type":"function","z":"82bb81a69c76160e","name":"","func":"\n\nif( !msg.payload.hasOwnProperty('counter'))\n{\n  msg.payload.counter = 0;\n}\n\nmsg.payload.counter = msg.payload.counter+1;\n\n//msg.delay = 2000;\n// Sets the delay, in milliseconds, to be applied to the message. \n// This option only applies if the node is configured to allow the \n// message to override the configured default delay interval.\n\n//msg.rate = 2000;\n// Sets the rate value in milliseconds between messages. This node\n// overwrites the existing rate value defined in the node \n// configuration when it receives the message which contains msg.rate \n// value in milliSeconds. This option only applies if the node is \n// configured to allow the message to override the configured default \n// rate interval.\n\n//msg.reset = true;\n// If the received message has this property set to any value, all \n// outstanding messages held by the node are cleared without being sent.\n\nmsg.flush = 1;\n// If the received message has this property set to a numeric value \n// then that many messages will be released immediately. If set to any \n// other type (e.g. boolean), then all outstanding messages held by the \n// node are sent immediately.\n\nmsg.toFront = true;\n// When in rate limit mode, if the received message has this property \n// set to boolean true, then the message is pushed to the front of the \n// queue and will be released next. This can be used in combination with \n// msg.flush=1 to resend immediately. \n\n// Details\n\n// When configured to delay messages, the delay interval can be a fixed \n// value, a random value within a range or dynamically set for each message. \n// Each message is delayed independently of any other message, based on the \n// time of its arrival.\n\n// When configured to rate limit messages, their delivery is spread across \n// the configured time period. The status shows the number of messages \n// currently in the queue. It can optionally discard intermediate messages as \n// they arrive.\n\n// If set to allow override of the rate, the new rate will be applied \n// immediately, and will remain in effect until changed again, the node \n// is reset, or the flow is restarted.\n\n// The rate limiting can be applied to all messages, or group them according \n// to their msg.topic value. When grouping, intermediate messages are \n// automatically dropped. At each time interval, the node can either release \n// the most recent message for all topics, or release the most recent message \n// for the next topic.\n\n// Note: In rate limit mode the maximum queue depth can be set by a property \n// in your settings.js file. For example nodeMessageBufferMaxLength: 1000,\n\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":580,"y":2760,"wires":[["04d691146cc01a24","bc2f710c2bd7dbf8"]]},{"id":"bc2f710c2bd7dbf8","type":"debug","z":"82bb81a69c76160e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":810,"y":2660,"wires":[]},{"id":"e33eb763ec3dadbd","type":"inject","z":"82bb81a69c76160e","name":"Interval 60s","props":[{"p":"payload.time","v":"","vt":"date"},{"p":"delay","v":"60000","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":330,"y":2820,"wires":[["c7204267634c3125","bc2f710c2bd7dbf8"]]},{"id":"0c8981a1178e6d6d","type":"inject","z":"82bb81a69c76160e","name":"Interval 1s","props":[{"p":"payload.time","v":"","vt":"date"},{"p":"delay","v":"1000","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":320,"y":2700,"wires":[["c7204267634c3125","bc2f710c2bd7dbf8"]]},{"id":"c391c8ac785d77af","type":"comment","z":"82bb81a69c76160e","name":"Trigger every n seconds","info":"","x":360,"y":2640,"wires":[]},{"id":"52f90350a9123877","type":"inject","z":"82bb81a69c76160e","name":"STOP","props":[{"p":"payload.time","v":"","vt":"date"},{"p":"reset","v":"1","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":310,"y":2880,"wires":[["c7204267634c3125","bc2f710c2bd7dbf8"]]}]

What I intend is to have a single message going round the loop. It will wait 1, 5 or 60 seconds before being released. The function node should incease the loop counter - msg.payload.counter by 1. If the inject nodes are triggered, they wait time should be updated and the total messages in the system should remain at 1.

I want to use code like this to help schedule other flows so the system is not overloaded.

p.s. I can't get my head round rate limited vs delayed.

You should really avoid loops (easy to crash or lock up your node-red)

Why not use a scheduler like Node-red-contrib-cron-plus?

Add multiple schedules either manually or at runtime.

Here are some examples of loops that count, the differences are that the trigger node can be set to topics and stopped per topic. Or with no topics will only allow one time trigger,.

Have a play and se the differences

[{"id":"0c8981a1178e6d6d","type":"inject","z":"c791cbc0.84f648","name":"Interval 1s","props":[{"p":"payload.time","v":"","vt":"date"},{"p":"delay","v":"1000","vt":"num"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"1","x":270,"y":4040,"wires":[["c7204267634c3125"]]},{"id":"c7204267634c3125","type":"function","z":"c791cbc0.84f648","name":"","func":"if(!msg.payload['counter' + msg.topic]){\n    msg.payload[\"counter\" +  msg.topic] = 1;\n}else{\n    msg.payload[\"counter\" + msg.topic] ++;\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":530,"y":4040,"wires":[["60715618.17325"]]},{"id":"e33eb763ec3dadbd","type":"inject","z":"c791cbc0.84f648","name":"Interval 60s","props":[{"p":"payload.time","v":"","vt":"date"},{"p":"delay","v":"60000","vt":"num"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"3","x":280,"y":4120,"wires":[["c7204267634c3125"]]},{"id":"43361163d49b32dc","type":"inject","z":"c791cbc0.84f648","name":"Interval 5s","props":[{"p":"payload.time","v":"","vt":"date"},{"p":"delay","v":"5000","vt":"num"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"2","x":270,"y":4080,"wires":[["c7204267634c3125"]]},{"id":"60715618.17325","type":"trigger","z":"c791cbc0.84f648","name":"","op1":"","op2":"","op1type":"nul","op2type":"pay","duration":"250","extend":false,"overrideDelay":true,"units":"ms","reset":"","bytopic":"topic","topic":"topic","outputs":1,"x":530,"y":4100,"wires":[["c7204267634c3125","bc2f710c2bd7dbf8"]]},{"id":"52f90350a9123877","type":"inject","z":"c791cbc0.84f648","name":"STOP 1","props":[{"p":"payload.time","v":"","vt":"date"},{"p":"reset","v":"1","vt":"num"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"1","payloadType":"str","x":260,"y":4160,"wires":[["60715618.17325"]]},{"id":"33f688c0.5c1a48","type":"inject","z":"c791cbc0.84f648","name":"STOP 3","props":[{"p":"payload.time","v":"","vt":"date"},{"p":"reset","v":"1","vt":"num"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"3","x":260,"y":4240,"wires":[["60715618.17325"]]},{"id":"cd93fb56.8bfdf","type":"inject","z":"c791cbc0.84f648","name":"STOP 2","props":[{"p":"payload.time","v":"","vt":"date"},{"p":"reset","v":"1","vt":"num"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"2","x":260,"y":4200,"wires":[["60715618.17325"]]},{"id":"bc2f710c2bd7dbf8","type":"debug","z":"c791cbc0.84f648","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":750,"y":4100,"wires":[]},{"id":"667143df.1cae24","type":"inject","z":"c791cbc0.84f648","name":"Interval 1s","props":[{"p":"payload.time","v":"","vt":"date"},{"p":"delay","v":"1000","vt":"num"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"1","x":510,"y":4200,"wires":[["557e8f80.ba8a48"]]},{"id":"e1e47a73.915e","type":"inject","z":"c791cbc0.84f648","name":"Interval 5s","props":[{"p":"payload.time","v":"","vt":"date"},{"p":"delay","v":"5000","vt":"num"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"2","x":510,"y":4240,"wires":[["557e8f80.ba8a48"]]},{"id":"8b9dd20a.7ba748","type":"inject","z":"c791cbc0.84f648","name":"Interval 60s","props":[{"p":"payload.time","v":"","vt":"date"},{"p":"delay","v":"60000","vt":"num"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"3","x":520,"y":4280,"wires":[["557e8f80.ba8a48"]]},{"id":"c69ecdc4.99fca","type":"inject","z":"c791cbc0.84f648","name":"STOP","props":[{"p":"payload.time","v":"","vt":"date"},{"p":"reset","v":"1","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":490,"y":4320,"wires":[["a84d1fd8.a02b5"]]},{"id":"557e8f80.ba8a48","type":"function","z":"c791cbc0.84f648","name":"","func":"if(!msg.payload['counter' + msg.topic]){\n    msg.payload[\"counter\" +  msg.topic] = 1;\n}else{\n    msg.payload[\"counter\" + msg.topic] ++;\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":810,"y":4220,"wires":[["a84d1fd8.a02b5"]]},{"id":"a84d1fd8.a02b5","type":"delay","z":"c791cbc0.84f648","name":"","pauseType":"delayv","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":800,"y":4260,"wires":[["557e8f80.ba8a48","315e9116.eeb4d6"]]},{"id":"315e9116.eeb4d6","type":"debug","z":"c791cbc0.84f648","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":990,"y":4220,"wires":[]},{"id":"d6d0c319.1a0568","type":"inject","z":"c791cbc0.84f648","name":"Interval 1s","props":[{"p":"payload.time","v":"","vt":"date"},{"p":"delay","v":"1000","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":210,"y":4380,"wires":[["7600c847.5beba8"]]},{"id":"1457581c.2181d8","type":"inject","z":"c791cbc0.84f648","name":"Interval 5s","props":[{"p":"payload.time","v":"","vt":"date"},{"p":"delay","v":"5000","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":210,"y":4420,"wires":[["7600c847.5beba8"]]},{"id":"b12c8091.d5c8c","type":"inject","z":"c791cbc0.84f648","name":"Interval 60s","props":[{"p":"payload.time","v":"","vt":"date"},{"p":"delay","v":"60000","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":220,"y":4460,"wires":[["7600c847.5beba8"]]},{"id":"c26030ab.03993","type":"inject","z":"c791cbc0.84f648","name":"STOP","props":[{"p":"payload.time","v":"","vt":"date"},{"p":"reset","v":"1","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":190,"y":4500,"wires":[["5a91a7de.a1d6"]]},{"id":"5a91a7de.a1d6","type":"trigger","z":"c791cbc0.84f648","name":"","op1":"","op2":"","op1type":"nul","op2type":"pay","duration":"250","extend":false,"overrideDelay":true,"units":"ms","reset":"","bytopic":"topic","topic":"topic","outputs":1,"x":510,"y":4420,"wires":[["5b2a87af.f5a798","7600c847.5beba8"]]},{"id":"7600c847.5beba8","type":"function","z":"c791cbc0.84f648","name":"","func":"if(!msg.payload['counter' + msg.topic]){\n    msg.payload[\"counter\" +  msg.topic] = 1;\n}else{\n    msg.payload[\"counter\" + msg.topic] ++;\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":510,"y":4380,"wires":[["5a91a7de.a1d6"]]},{"id":"5b2a87af.f5a798","type":"debug","z":"c791cbc0.84f648","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":690,"y":4380,"wires":[]}]

Hi @Steve-Mcl - I agree! My test loops have already run away several times! I am trying to monitor a process and change the frequency I am checking it based on how active the process is. i.e. If I know nothing will happen for two minutes, I want to pause checking, but if it is active I should check every second. I can see uses for Cron, but I suspect in this case it would not be responsive enough.

Thanks' @E1cid .
I tried all of these, but didn't quite work.

I want
1 message in the system
1 topic channel
repeat the message according to the last injected delay value. i.e. the message frequency will be 1s, 5s or 60s only.

For me, each time I inject it adds a message to the loop. If I press Delay 1s three times, I end up with three messages per second.

What I am trying to achieve is to monitor a service that has a minimal time left to run and to wait until the service is nearly done, then I want to monitor it more often to capture the last moments of that service.

regards
Steve

Then you would need to reset the trigger each time you changed the delay. Also if you want the counter to continue you would have to store it also.

[{"id":"ac15e95b.a44818","type":"inject","z":"bf9e1e33.030598","name":"STOP","props":[{"p":"payload.time","v":"","vt":"date"},{"p":"reset","v":"1","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":230,"y":360,"wires":[["eaaa48f4.12543"]]},{"id":"eaaa48f4.12543","type":"change","z":"bf9e1e33.030598","name":"","rules":[{"t":"delete","p":"counter","pt":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":430,"y":360,"wires":[["a6c182e.28d31"]]},{"id":"a6c182e.28d31","type":"trigger","z":"bf9e1e33.030598","name":"","op1":"","op2":"","op1type":"nul","op2type":"pay","duration":"250","extend":false,"overrideDelay":true,"units":"ms","reset":"reset","bytopic":"topic","topic":"topic","outputs":1,"x":710,"y":360,"wires":[["86d9a9b1.058c08","8c01612c.0811f8"]]},{"id":"86d9a9b1.058c08","type":"debug","z":"bf9e1e33.030598","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":890,"y":400,"wires":[]},{"id":"8c01612c.0811f8","type":"function","z":"bf9e1e33.030598","name":"","func":"let counter = flow.get(\"counter\") || 0;\nif(!msg.payload.counter){\n    msg.payload.counter = counter > 0 ? counter : 1;\n}else{\n    msg.payload.counter ++;\n}\nflow.set(\"counter\", msg.payload.counter)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":710,"y":420,"wires":[["a6c182e.28d31"]]},{"id":"f39e6573.d0383","type":"trigger","z":"bf9e1e33.030598","name":"","op1":"reset","op2":"","op1type":"str","op2type":"pay","duration":"50","extend":false,"overrideDelay":false,"units":"ms","reset":"","bytopic":"all","topic":"topic","outputs":2,"x":460,"y":420,"wires":[["a6c182e.28d31"],["8c01612c.0811f8"]]},{"id":"9ac8faf4.5264a8","type":"inject","z":"bf9e1e33.030598","name":"Interval 1s","props":[{"p":"payload.time","v":"","vt":"date"},{"p":"delay","v":"1000","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"str","x":230,"y":400,"wires":[["f39e6573.d0383"]]},{"id":"dd1e2d56.18667","type":"inject","z":"bf9e1e33.030598","name":"Interval 5s","props":[{"p":"payload.time","v":"","vt":"date"},{"p":"delay","v":"5000","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":230,"y":440,"wires":[["f39e6573.d0383"]]},{"id":"bd60adbb.40f4d","type":"inject","z":"bf9e1e33.030598","name":"Interval 60s","props":[{"p":"payload.time","v":"","vt":"date"},{"p":"delay","v":"60000","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":240,"y":480,"wires":[["f39e6573.d0383"]]}]

Thanks' @E1cid !!

Just what I need.

thank you!
Steve

Not to seem like I'm trying to one up @E1cid this is one I have.

[{"id":"e0a14b00.511f48","type":"inject","z":"0918ee609bf69fc7","name":"Default","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"30","payloadType":"num","x":713,"y":2280,"wires":[["b81a570d.b8e1c8"]]},{"id":"b81a570d.b8e1c8","type":"function","z":"0918ee609bf69fc7","name":"Convert to milliseconds","func":"var x = msg.payload;\nvar y = x * 1000;\nmsg = {delay: y, payload: x};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":808,"y":2280,"wires":[["58acd068.09e108"]],"l":false},{"id":"58acd068.09e108","type":"delay","z":"0918ee609bf69fc7","name":"Variable delay","pauseType":"delayv","timeout":"10","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"outputs":1,"x":743,"y":2320,"wires":[["856f574e.d68b58","283b720383513aee"]]},{"id":"856f574e.d68b58","type":"function","z":"0918ee609bf69fc7","name":"Variable repeat time","func":"if (msg.topic === \"interval\") {\n    context.set(\"interval\", msg.payload);\n    msg = null;\n} else {\n    var interval = context.get(\"interval\");\n    if (interval) msg.delay = interval;\n}\nreturn msg;","outputs":1,"noerr":0,"x":723,"y":2360,"wires":[["58acd068.09e108"]]},{"id":"f39c5358.61c5c","type":"function","z":"0918ee609bf69fc7","name":"Minimum","func":"x = msg.payload;\nif (x >4999)\n{\n    //  All good\n    node.status({fill:\"green\",shape:\"dot\",text:\"Good\"});\n} else\n{\n    //  Too small.\n    node.status({fill:\"red\",shape:\"dot\",text:\"Too small\"});\n    x = 5000;\n}\nmsg.payload = x;\nreturn msg;","outputs":1,"noerr":0,"x":523,"y":2360,"wires":[["856f574e.d68b58"]]},{"id":"234b3f57.731a7","type":"change","z":"0918ee609bf69fc7","name":"","rules":[{"t":"set","p":"interval","pt":"msg","to":"payload","tot":"msg"},{"t":"set","p":"topic","pt":"msg","to":"interval","tot":"str"},{"t":"set","p":"SCAN_TIME","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":503,"y":2320,"wires":[["f39c5358.61c5c"]]},{"id":"283b720383513aee","type":"trigger","z":"0918ee609bf69fc7","name":"Optional","op1":"1","op2":"0","op1type":"str","op2type":"str","duration":"2","extend":false,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":983,"y":2360,"wires":[[]]},{"id":"ba84c1a32271fd38","type":"comment","z":"0918ee609bf69fc7","name":"Inject values here.","info":"","x":250,"y":2320,"wires":[]}]

It has a default value injected (top node) and a node to stop you entering too small a value on the input.

Just send it messages of how many milliseconds you want the delay to be.

There is also a optional trigger node on the right side.

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