# Flow - Auto Start loop

**URL:** https://discourse.nodered.org/t/flow-auto-start-loop/25223
**Category:** General
**Created:** [21 April 2020 06:53 UTC](https://discourse.nodered.org/t/flow-auto-start-loop/25223 "2020-04-21T06:53:39Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![ScheepersJohan](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/scheepersjohan/32/39556_2.png) [@ScheepersJohan](https://discourse.nodered.org/u/ScheepersJohan)
#### Post date: [21 April 2020 06:53 UTC](https://discourse.nodered.org/t/flow-auto-start-loop/25223/1 "2020-04-21T06:53:40Z")

</div>

Hi,

I have a function node with a loop in it, it generates random time delays.

I want it to auto star when the PI reboots or when the flow gets re-deployed.

Is the below correct? From my function node.

```auto
const minDelay = 1, maxDelay =5; // seconds
var a = 1;
loop();

msg.payload = a;

function loop() {
    if (a==1) {
        node.send(msgToSend);
        msgToSend = msg;
        let delay = Math.random() * (maxDelay - minDelay) + minDelay;
        setTimeout(loop,1000 * delay);
        node.status({fill:'green',text:delay.toFixed(2)});
    }
}

```

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [21 April 2020 07:10 UTC](https://discourse.nodered.org/t/flow-auto-start-loop/25223/2 "2020-04-21T07:10:01Z")

</div>

When posting code, click the button that looks like `</>` at the top.

I see you tried, but.....

Ok, break down:  
`msg.payload = a;`

Wrong way around - I think.  
You want `a` to be the `payload`.  
So it should be:  
`a = msg.payload`

In your loop:  
You only have testing for `a == 1` What if it is another value?

Reading your original code again where: `msg.payload = a`:  
So a will always be 1.  
Why test at all?

---

<div class="post-metadata">

### Author: ![ScheepersJohan](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/scheepersjohan/32/39556_2.png) [@ScheepersJohan](https://discourse.nodered.org/u/ScheepersJohan)
#### Post date: [21 April 2020 07:17 UTC](https://discourse.nodered.org/t/flow-auto-start-loop/25223/3 "2020-04-21T07:17:24Z")

</div>

Thank for the reply.

I want the payload to be one always.

Debug - error message

21/04/2020, 09:12:57node: d5575e9b.cc1ac  
function : (error)  
"ReferenceError: msgToSend is not defined"

I have taken the test out.

```auto
const minDelay = 1, maxDelay =5; // seconds
var a = 1;
loop();

msg.payload = a;

function loop() {

        node.send(msgToSend);
        msgToSend = msg;
        let delay = Math.random() * (maxDelay - minDelay) + minDelay;
        setTimeout(loop,1000 * delay);
        node.status({fill:'green',text:delay.toFixed(2)});

}

```

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [21 April 2020 07:18 UTC](https://discourse.nodered.org/t/flow-auto-start-loop/25223/4 "2020-04-21T07:18:53Z")

</div>

I think you have exceeded my skills sorry.

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [21 April 2020 07:44 UTC](https://discourse.nodered.org/t/flow-auto-start-loop/25223/5 "2020-04-21T07:44:16Z")

</div>

Whichever way you do it - you will need an inject (on start) node in front to trigger it to start running on boot.

---

<div class="post-metadata">

### Author: ![ScheepersJohan](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/scheepersjohan/32/39556_2.png) [@ScheepersJohan](https://discourse.nodered.org/u/ScheepersJohan)
#### Post date: [21 April 2020 07:48 UTC](https://discourse.nodered.org/t/flow-auto-start-loop/25223/6 "2020-04-21T07:48:37Z")

</div>

Thank you for the reply-

I have put a inject node in front of it injecting 1 on at 0.1, but it still don't start.

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/6/96b729ad2145df46728bddf980430a2f9adc60fe.png)

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [21 April 2020 08:13 UTC](https://discourse.nodered.org/t/flow-auto-start-loop/25223/7 "2020-04-21T08:13:13Z")

</div>

> [@ScheepersJohan](#):
>
> node.send(msgToSend);

As the error says, you have not defined msgToSend, you need `var msgToSend = something` somewhere before it is used. Alternatively perhaps you just need to change all uses of `msgToSend` to `msg` if it is actually a copy of the original msg that you want to send each time.

---

<div class="post-metadata">

### Author: ![ScheepersJohan](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/scheepersjohan/32/39556_2.png) [@ScheepersJohan](https://discourse.nodered.org/u/ScheepersJohan)
#### Post date: [21 April 2020 08:37 UTC](https://discourse.nodered.org/t/flow-auto-start-loop/25223/8 "2020-04-21T08:37:10Z")

</div>

Thank you @Colin, solved.

These two wrong way around

node.send(msgToSend);  
msgToSend = msg;

Correct is way  
msgToSend = msg;  
node.send(msgToSend);

Or just

```
    node.send(msg);

```

Can you have a var in setTimeout(loop,1000 \* delay); instead of "delay"?

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [21 April 2020 08:42 UTC](https://discourse.nodered.org/t/flow-auto-start-loop/25223/9 "2020-04-21T08:42:20Z")

</div>

> [@ScheepersJohan](#):
>
> Can you have a var in setTimeout(loop,1000 \* delay); instead of "delay"?

What do you mean? `delay` already is a variable.

---

<div class="post-metadata">

### Author: ![ScheepersJohan](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/scheepersjohan/32/39556_2.png) [@ScheepersJohan](https://discourse.nodered.org/u/ScheepersJohan)
#### Post date: [21 April 2020 09:56 UTC](https://discourse.nodered.org/t/flow-auto-start-loop/25223/10 "2020-04-21T09:56:02Z")

</div>

Thanks for your response

Meant something link this:

```auto

msg.payload = a;

loop();

function loop() {
        msg.paload = 1;
        node.send(msg);
        let delay1 = 10;
        setTimeout(loop,1000 * delay1);
        node.status({fill:'green',text:delay1.toFixed(2)});
        
        msg.paload = 0;
        node.send(msg);
        let delay2 = 5;
        setTimeout(loop,delay2);
        node.status({fill:'green',text:delay2.toFixed(2)});

}

```

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [21 April 2020 09:56 UTC](https://discourse.nodered.org/t/flow-auto-start-loop/25223/11 "2020-04-21T09:56:57Z")

</div>

Can you describe what you mean. From that code, I'm really not sure what you intend.

---

<div class="post-metadata">

### Author: ![ScheepersJohan](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/scheepersjohan/32/39556_2.png) [@ScheepersJohan](https://discourse.nodered.org/u/ScheepersJohan)
#### Post date: [21 April 2020 09:59 UTC](https://discourse.nodered.org/t/flow-auto-start-loop/25223/12 "2020-04-21T09:59:19Z")

</div>

send msg.payload =1 wait delay1 seconds and then send msg .payload = 0 wait delay2 seconds

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [21 April 2020 09:59 UTC](https://discourse.nodered.org/t/flow-auto-start-loop/25223/13 "2020-04-21T09:59:59Z")

</div>

Then what?

---

<div class="post-metadata">

### Author: ![ScheepersJohan](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/scheepersjohan/32/39556_2.png) [@ScheepersJohan](https://discourse.nodered.org/u/ScheepersJohan)
#### Post date: [21 April 2020 10:01 UTC](https://discourse.nodered.org/t/flow-auto-start-loop/25223/14 "2020-04-21T10:01:20Z")

</div>

> [@ScheepersJohan](#):
>
> send msg.payload =1 wait delay1 seconds and then send msg .payload = 0 wait delay2 seconds

send msg.payload =1 wait delay1 seconds and then send msg .payload = 0 wait delay2 seconds and then the first msg

---

<div class="post-metadata">

### Author: ![ScheepersJohan](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/scheepersjohan/32/39556_2.png) [@ScheepersJohan](https://discourse.nodered.org/u/ScheepersJohan)
#### Post date: [21 April 2020 10:02 UTC](https://discourse.nodered.org/t/flow-auto-start-loop/25223/15 "2020-04-21T10:02:02Z")

</div>

This node will have a interrupt statement in it

---

<div class="post-metadata">

### Author: ![ScheepersJohan](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/scheepersjohan/32/39556_2.png) [@ScheepersJohan](https://discourse.nodered.org/u/ScheepersJohan)
#### Post date: [21 April 2020 10:04 UTC](https://discourse.nodered.org/t/flow-auto-start-loop/25223/16 "2020-04-21T10:04:49Z")

</div>

```auto
var run = context.get('state') || false;
if (msg.topic.toLowerCase() === 'control') {
    run = ! run;
}
context.set('state',run);
node.status({fill:(run) ? 'green':'red'});

var msgToSend = null;
loop();
return null;

loop();

function loop() {
        msg.paload = 1;
        node.send(msg);
        let delay1 = flow.get("ontime");
        setTimeout(loop,1000 * delay1);
        node.status({fill:'green',text:delay.toFixed(2)});
        
        msg.paload = 0;
        node.send(msg);
        let delay2 = flow.get("offtime");
        setTimeout(loop,1000 * delay2);
        node.status({fill:'green',text:delay.toFixed(2)});

}

```

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [21 April 2020 10:09 UTC](https://discourse.nodered.org/t/flow-auto-start-loop/25223/18 "2020-04-21T10:09:25Z")

</div>

> [@ScheepersJohan](#):
>
> msg.paload = 1;

don't you mean `payload` instead of `paload`?

---

<div class="post-metadata">

### Author: ![ScheepersJohan](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/scheepersjohan/32/39556_2.png) [@ScheepersJohan](https://discourse.nodered.org/u/ScheepersJohan)
#### Post date: [21 April 2020 10:26 UTC](https://discourse.nodered.org/t/flow-auto-start-loop/25223/19 "2020-04-21T10:26:07Z")

</div>

Sorry Yes

```auto
var run = context.get('state') || false;
if (msg.topic.toLowerCase() === 'control') {
    run = ! run;
}
context.set('state',run);
node.status({fill:(run) ? 'green':'red'});

var msgToSend = null;
loop();
return null;

loop();

function loop() {
        msg.payload = 1;
        node.send(msg);
        let delay1 = flow.get("ontime");
        setTimeout(loop,1000 * delay1);
        node.status({fill:'green',text:delay.toFixed(2)});
        
        msg.payload = 0;
        node.send(msg);
        let delay2 = flow.get("offtime");
        setTimeout(loop,1000 * delay2);
        node.status({fill:'green',text:delay.toFixed(2)});

}

```

---

<div class="post-metadata">

### Author: ![ScheepersJohan](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/scheepersjohan/32/39556_2.png) [@ScheepersJohan](https://discourse.nodered.org/u/ScheepersJohan)
#### Post date: [21 April 2020 10:30 UTC](https://discourse.nodered.org/t/flow-auto-start-loop/25223/20 "2020-04-21T10:30:21Z")

</div>

@Colin This is what I want to achieve , I don't know what standard node I can use, as we suffer from a lot of power outages and most timing nodes don't give you a output once the on time are pass or you cant set the day off week from a message input.

If you have a idea I can use I will appreciate.

The code in the above is for the Pump control

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/1/d1b931e4d771721902359b22f202bb2c7f996c53.png)

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [21 April 2020 21:07 UTC](https://discourse.nodered.org/t/flow-auto-start-loop/25223/21 "2020-04-21T21:07:02Z")

</div>

I think probably the easiest way _is_ with a function node.

There are a couple of issues with your code.  
I don't know what the var msgToSend line is doing, nor the loop() function after the return null.  
You appear to be setting up two timers, I don't think you want to do that, you just want one, with the delay time alternating between the the two values.

[Next page](https://discourse.nodered.org/t/flow-auto-start-loop/25223.md?page=2)
