Start (setinterval) / stop (clearinterval) depending on msg.payload

Hi

I'm kind of lost. For serveral days now I tried to set up a setinterval/clearinterval function node. Actually I started with an smxstate node. Narrowing down the problem I ended up in a simple function node. Now I am really lost as I cannot make the simplest example work (which is driving me nuts).

Goal: I would like to have values sent/not sent depending on msg.payload which is sent into the node.

Here is my minimaly NOT working example:

[{"id":"366a43adb328cf95","type":"tab","label":"Flow 1","disabled":false,"info":"","env":[]},{"id":"9afe566e1146529d","type":"inject","z":"366a43adb328cf95","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"anything","payloadType":"str","x":460,"y":340,"wires":[["7bdc11e490a13e61"]]},{"id":"aefc713a35661715","type":"debug","z":"366a43adb328cf95","name":"debug 11","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":920,"y":380,"wires":[]},{"id":"7bdc11e490a13e61","type":"function","z":"366a43adb328cf95","name":"sendlowvalues","func":"\nconst minvalue = 6.2\nconst intvl = 1000 //Milliseconds\nvar test = msg.payload\n\nclearInterval(node.intl)\n\nfunction numgen() {\n    msg.payload = minvalue\n    return msg;\n}\n\nif (test == \"low\") { \n    node.intl = setInterval(numgen, intvl)\n} else {\n    clearInterval(node.intl)\n}\n\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":660,"y":380,"wires":[["aefc713a35661715"]]},{"id":"7e524e8530efba7c","type":"inject","z":"366a43adb328cf95","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"low","payloadType":"str","x":450,"y":400,"wires":[["7bdc11e490a13e61"]]}]

Any help would be greatly appreciated.
Thank you!!

You can not use return in your callback function, use node.send(), and you need to store the setInterval number between function node calls

[{"id":"9afe566e1146529d","type":"inject","z":"366a43adb328cf95","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"anything","payloadType":"str","x":460,"y":340,"wires":[["7bdc11e490a13e61"]]},{"id":"7bdc11e490a13e61","type":"function","z":"366a43adb328cf95","name":"sendlowvalues","func":"\nconst minvalue = 6.2\nconst intvl = 3000 //Milliseconds\nlet test = msg.payload\nnode.intl = context.get(\"interval\");\n clearInterval(node.intl)\n\nfunction numgen() {\n    msg.payload = minvalue\n    node.send(msg);\n}\n\nif (test == \"low\") { \n    node.intl = setInterval(numgen, intvl)\n    context.set(\"interval\",node.intl);\n} else {\n    clearInterval(node.intl)\n}\n\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":680,"y":380,"wires":[["aefc713a35661715"]]},{"id":"7e524e8530efba7c","type":"inject","z":"366a43adb328cf95","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"low","payloadType":"str","x":450,"y":400,"wires":[["7bdc11e490a13e61"]]},{"id":"aefc713a35661715","type":"debug","z":"366a43adb328cf95","name":"debug 11","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":920,"y":380,"wires":[]}][{"id":"9afe566e1146529d","type":"inject","z":"366a43adb328cf95","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"anything","payloadType":"str","x":460,"y":340,"wires":[["7bdc11e490a13e61"]]},{"id":"7bdc11e490a13e61","type":"function","z":"366a43adb328cf95","name":"sendlowvalues","func":"\nconst minvalue = 6.2\nconst intvl = 3000 //Milliseconds\nlet test = msg.payload\nnode.intl = context.get(\"interval\");\n clearInterval(node.intl)\n\nfunction numgen() {\n    msg.payload = minvalue\n    node.send(msg);\n}\n\nif (test == \"low\") { \n    node.intl = setInterval(numgen, intvl)\n    context.set(\"interval\",node.intl);\n} else {\n    clearInterval(node.intl)\n}\n\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":680,"y":380,"wires":[["aefc713a35661715"]]},{"id":"7e524e8530efba7c","type":"inject","z":"366a43adb328cf95","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"low","payloadType":"str","x":450,"y":400,"wires":[["7bdc11e490a13e61"]]},{"id":"aefc713a35661715","type":"debug","z":"366a43adb328cf95","name":"debug 11","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":920,"y":380,"wires":[]}][{"id":"9afe566e1146529d","type":"inject","z":"366a43adb328cf95","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"anything","payloadType":"str","x":460,"y":340,"wires":[["7bdc11e490a13e61"]]},{"id":"7bdc11e490a13e61","type":"function","z":"366a43adb328cf95","name":"sendlowvalues","func":"\nconst minvalue = 6.2\nconst intvl = 3000 //Milliseconds\nlet test = msg.payload\nnode.intl = context.get(\"interval\");\n clearInterval(node.intl)\n\nfunction numgen() {\n    msg.payload = minvalue\n    node.send(msg);\n}\n\nif (test == \"low\") { \n    node.intl = setInterval(numgen, intvl)\n    context.set(\"interval\",node.intl);\n} else {\n    clearInterval(node.intl)\n}\n\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":680,"y":380,"wires":[["aefc713a35661715"]]},{"id":"7e524e8530efba7c","type":"inject","z":"366a43adb328cf95","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"low","payloadType":"str","x":450,"y":400,"wires":[["7bdc11e490a13e61"]]},{"id":"aefc713a35661715","type":"debug","z":"366a43adb328cf95","name":"debug 11","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":920,"y":380,"wires":[]}]

Edit : it seems others understand the question better than me!

I think it would be better for you to better explain what you are trying to achieve. It is rare that you would want to do a setinterval in node-red.

Though if you really do, you will need to save a reference to the instance to a flow/global context variable. At the start of the function node, try to recover the reference. If it doesn't exist, then create a new one saving the reference and writing it to the context variable. Make sure that you cancel any existing one before creating a new one.

I am trying to simulate a machine which throws out measurements as soon as it is turned on. The values will change over time so I need an option to turn the initial function off and another one on (different mathematical function) and I need only a specific timerange of the thrown measurements. Ultimately I need this to test if my code to record measurements is sound and no buffer overflow occurs.

If I understand the requirement, you want to generate test data during a given time period?

Maybe something like this will work?
It generates test values continuously and a gate node node-red-config-simple-gate allows them to pass for a specified period.
As set up here it generates 10 random numbers per second and the gate is open for half a second but you can set whatever limits suit your simulation.

[{"id":"3dc98c35268edfca","type":"inject","z":"96f9dd36f2b458dc","name":"10 times a second","props":[{"p":"payload"}],"repeat":"0.1","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":510,"y":140,"wires":[["681aefc8c0a73ac0"]]},{"id":"681aefc8c0a73ac0","type":"function","z":"96f9dd36f2b458dc","name":"Simulate measurement","func":"msg.payload = Math.random()* 100)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":730,"y":140,"wires":[["96ef40876e612b10"]]},{"id":"96ef40876e612b10","type":"gate","z":"96f9dd36f2b458dc","name":"","controlTopic":"control","defaultState":"closed","openCmd":"open","closeCmd":"close","toggleCmd":"toggle","defaultCmd":"default","statusCmd":"status","persist":false,"storeName":"memory","x":790,"y":220,"wires":[["24ad20befc626139"]]},{"id":"7daa8721ed430ea5","type":"inject","z":"96f9dd36f2b458dc","name":"Open Gate","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"control","payload":"","payloadType":"date","x":480,"y":220,"wires":[["390dc87fc2d2022a"]]},{"id":"390dc87fc2d2022a","type":"trigger","z":"96f9dd36f2b458dc","name":"For 0.5 sec","op1":"open","op2":"close","op1type":"str","op2type":"str","duration":"500","extend":false,"overrideDelay":false,"units":"ms","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":630,"y":220,"wires":[["96ef40876e612b10"]]},{"id":"24ad20befc626139","type":"debug","z":"96f9dd36f2b458dc","name":"debug 10","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":920,"y":220,"wires":[]}]
1 Like

Thank you for all your help. Using context.set, solved the issue.

I take it you never looked at my example? As it showed you how to save the setIterval() number to context.

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