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

**URL:** <https://discourse.nodered.org/t/start-setinterval-stop-clearinterval-depending-on-msg-payload/68737>\
**Category:** General\
**Created:** [8 October 2022 21:51 UTC](https://discourse.nodered.org/t/start-setinterval-stop-clearinterval-depending-on-msg-payload/68737 "2022-10-08T21:51:59Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![akrea](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/akrea/32/68902_2.png) [@akrea](https://discourse.nodered.org/u/akrea)\
**Post date:** [8 October 2022 21:51 UTC](https://discourse.nodered.org/t/start-setinterval-stop-clearinterval-depending-on-msg-payload/68737/1 "2022-10-08T21:51:59Z")

</div>

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:

```auto
[{"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!!

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [8 October 2022 22:21 UTC](https://discourse.nodered.org/t/start-setinterval-stop-clearinterval-depending-on-msg-payload/68737/2 "2022-10-08T22:21:18Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [8 October 2022 22:27 UTC](https://discourse.nodered.org/t/start-setinterval-stop-clearinterval-depending-on-msg-payload/68737/3 "2022-10-08T22:27:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [9 October 2022 01:56 UTC](https://discourse.nodered.org/t/start-setinterval-stop-clearinterval-depending-on-msg-payload/68737/4 "2022-10-09T01:56:17Z")

</div>

> [@akrea](#):
>
> Goal: I would like to have values sent/not sent depending on msg.payload which is sent into the node.

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.

---

<div class="post-metadata">

**Author:** ![akrea](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/akrea/32/68902_2.png) [@akrea](https://discourse.nodered.org/u/akrea)\
**Post date:** [9 October 2022 19:57 UTC](https://discourse.nodered.org/t/start-setinterval-stop-clearinterval-depending-on-msg-payload/68737/5 "2022-10-09T19:57:13Z")

</div>

> [@TotallyInformation](#):
>
> 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.

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.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [9 October 2022 21:53 UTC](https://discourse.nodered.org/t/start-setinterval-stop-clearinterval-depending-on-msg-payload/68737/6 "2022-10-09T21:53:25Z")

</div>

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.

 ![Untitled 1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/6/56779b20008c6b904b0695f674b7fdc1d0f73b57.jpeg)

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

```

---

<div class="post-metadata">

**Author:** ![akrea](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/akrea/32/68902_2.png) [@akrea](https://discourse.nodered.org/u/akrea)\
**Post date:** [10 October 2022 18:14 UTC](https://discourse.nodered.org/t/start-setinterval-stop-clearinterval-depending-on-msg-payload/68737/7 "2022-10-10T18:14:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [10 October 2022 18:21 UTC](https://discourse.nodered.org/t/start-setinterval-stop-clearinterval-depending-on-msg-payload/68737/8 "2022-10-10T18:21:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [24 October 2022 18:21 UTC](https://discourse.nodered.org/t/start-setinterval-stop-clearinterval-depending-on-msg-payload/68737/9 "2022-10-24T18:21:40Z")

</div>

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