# How to keep flow running after one of the triggers is false?

**URL:** <https://discourse.nodered.org/t/how-to-keep-flow-running-after-one-of-the-triggers-is-false/27079>\
**Category:** General\
**Created:** [22 May 2020 10:25 UTC](https://discourse.nodered.org/t/how-to-keep-flow-running-after-one-of-the-triggers-is-false/27079 "2020-05-22T10:25:46Z")\
**Posts on this page:** 11\
**Page:** 2

<div class="post-metadata">

**Author:** ![lesmar96](https://avatars.discourse-cdn.com/v4/letter/l/b5a626/32.png) [@lesmar96](https://discourse.nodered.org/u/lesmar96)\
**Post date:** [23 May 2020 15:00 UTC](https://discourse.nodered.org/t/how-to-keep-flow-running-after-one-of-the-triggers-is-false/27079/21 "2020-05-23T15:00:51Z")

</div>

I'm sorry I didn't mean to say that i don't want your help. I intended to say that those nodes are working right like I need them. I plan to upgrade it and make it better as I go, but I was hoping to get the switch working properly first. Since everything else is working, I wanted to leave it like it is as not to mess it up. Thanks for your help.

---

<div class="post-metadata">

**Author:** ![lesmar96](https://avatars.discourse-cdn.com/v4/letter/l/b5a626/32.png) [@lesmar96](https://discourse.nodered.org/u/lesmar96)\
**Post date:** [23 May 2020 15:02 UTC](https://discourse.nodered.org/t/how-to-keep-flow-running-after-one-of-the-triggers-is-false/27079/22 "2020-05-23T15:02:39Z")

</div>

Hi Colin,  
Thanks for your pointers. The reason cycle is not set at all is because this is unfinished. Steve wanted to see what I had in the function node.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [23 May 2020 15:03 UTC](https://discourse.nodered.org/t/how-to-keep-flow-running-after-one-of-the-triggers-is-false/27079/23 "2020-05-23T15:03:50Z")

</div>

@lesmar96 I updated that previous post.

> [@Steve-Mcl](#):
>
> ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/6/56c56f18bcb5c10d8463b15f4cb0a53261863396.png)
> 
> IMO there is much wrong (and while it may work, you should correct it).
> 
> Mostly, you are working like a PLC.
> 
> 1. I would change al the nodes from "GET" style to "INPUT" style and be 100% event based.
> 2. lose the pointless delay
> 3. change those SET\_ and Compile\_ functions to one function that build a proper object
> 4. lose the JSON node
> 
> NOTE: It will STILL ouptut the exact same value as before, just that the data will be consistent and correct

Please read it.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [23 May 2020 15:05 UTC](https://discourse.nodered.org/t/how-to-keep-flow-running-after-one-of-the-triggers-is-false/27079/24 "2020-05-23T15:05:53Z")

</div>

Please show me the output of this debug node (a screen shot and the copied data please)

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

---

<div class="post-metadata">

**Author:** ![lesmar96](https://avatars.discourse-cdn.com/v4/letter/l/b5a626/32.png) [@lesmar96](https://discourse.nodered.org/u/lesmar96)\
**Post date:** [23 May 2020 15:09 UTC](https://discourse.nodered.org/t/how-to-keep-flow-running-after-one-of-the-triggers-is-false/27079/25 "2020-05-23T15:09:40Z")

</div>

5/23/2020, 11:06:32 AM[node: 13e14267.55c63e](https://10.16.32.1/node-red/#)

msg.payload : Object

object

cycleID: "undefined"

Project: "LoadNGo"

TimeStamp: "5/23/2020, 3:06:31 PM"

M1: 0

M2: 0

M3: 0

M4: 0

M5: 0

M6: 0

Flow: -133.97463989257812

Pressure: 0.007999999448657036

Temp: 65.84

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/8/78358200e20b629ef329635227c032c1a6fc93ac.png)  
I do not understand yet what is wrong with the set & compile nodes. But that is okay.  
I am reading and researching your suggestions.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [23 May 2020 15:25 UTC](https://discourse.nodered.org/t/how-to-keep-flow-running-after-one-of-the-triggers-is-false/27079/26 "2020-05-23T15:25:47Z")

</div>

> [@lesmar96](#):
>
> I do not understand yet what is wrong with the set & compile nodes

You are making an object out of strings instead of making an object.

This is how I would do this...

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/0/e028cc997b337e786f3b226117182fcdbfedbe7c.png)

- No delay (causes inconsistent data as 5s later the machine might have stopped!)
- 1 function that does EXACTLY the same but using objects instead of concatenating strings to make JSON then converting to an object

Here are the 2 functions....

```auto
[{"id":"3000cec0.1c2dc2","type":"function","z":"80a62cbb.ffa6a","name":"Compile_message","func":"\n//use the consistent values sent in when the sample was taken\nmsg.payload = {\n cycleID: msg.cycleID,\n Project: msg.ProjectName,\n TimeStamp: msg.datetime,\n M1: msg.manifold1,\n M2: msg.manifold2,\n M3: msg.manifold3,\n M4: msg.manifold4,\n M5: msg.manifold5,\n M6: msg.manifold6,\n Flow: msg.flow,\n Pressure: msg.pressure,\n Temp: msg.tempF\n}\n\nreturn msg;\n\n","outputs":1,"noerr":0,"x":550,"y":460,"wires":[["ead8fc11.ac877"]]},{"id":"e1860782.2d3118","type":"function","z":"80a62cbb.ffa6a","name":"Machine_Running?","func":"//get all data in one place & pass it on to next node\n//this ensures data is consistent & unchanged before sending to database\n//i.e. any change in the GLOBALS will not affect these samples\n\nmsg.ProjectName = \"LoadNGo\";\nmsg.cycleID = global.get(\"cycleID\");\nmsg.datetime = global.get('datetime') || 0;\nmsg.flow = global.get('flow') || 0;\nmsg.pressure = global.get('pressure') || 0;\nmsg.tempF = global.get('TempF') || 0;\nmsg.manifold1 = global.get('manifold1') || 0;\nmsg.manifold2 = global.get('manifold2') || 0;\nmsg.manifold3 = global.get('manifold3') || 0;\nmsg.manifold4 = global.get('manifold4') || 0;\nmsg.manifold5 = global.get('manifold5') || 0;\nmsg.manifold6 = global.get('manifold6') || 0;\n\nvar manifoldON;\n\nif (msg.manifold1 || msg.manifold2 || msg.manifold3 || msg.manifold4 || msg.manifold5 || msg.manifold6) {\n manifoldON = true;\n}\n\nif (msg.manifoldON || (msg.pressure > 0)) {\n return msg;\n}\n\nreturn null; //halt flow","outputs":1,"noerr":0,"x":330,"y":460,"wires":[["3000cec0.1c2dc2"]]}]

```

This is the final output (ignore the faked values - the structure is the same as what you had - thats whats important)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/0/807480a0a8cd12b7a9d899e36b75b85caa4fc524.png)

* * *

### EDIT:

The logic in "Machine\_running" is this...  
"If any manifold == true OR pressure is \> 0 THEN proceed"

```auto
if (msg.manifoldON || (msg.pressure > 0)) {
    return msg;
}

```

to

If you need "If any manifold == true AND pressure is \> 0 THEN proceed" change the below...

```auto
if (msg.manifoldON && (msg.pressure > 0)) {
    return msg;
}

```

* * *

### EDIT2:

While testing, in the function Machine\_running, I was returning msg at the end - I've updated the flow export above to `return null`

---

<div class="post-metadata">

**Author:** ![lesmar96](https://avatars.discourse-cdn.com/v4/letter/l/b5a626/32.png) [@lesmar96](https://discourse.nodered.org/u/lesmar96)\
**Post date:** [23 May 2020 15:41 UTC](https://discourse.nodered.org/t/how-to-keep-flow-running-after-one-of-the-triggers-is-false/27079/27 "2020-05-23T15:41:10Z")

</div>

Thanks Steve,  
That makes sense. Am I correct that this functions basically the same as I had it, but this is definitely neater?

But my main question still remains unanswered. This has been puzzling me for several days. Let me keep thinking though.

EDIT: I see your updated post. We are getting closer. Thanks!!

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [23 May 2020 15:43 UTC](https://discourse.nodered.org/t/how-to-keep-flow-running-after-one-of-the-triggers-is-false/27079/28 "2020-05-23T15:43:50Z")

</div>

> [@lesmar96](#):
>
> Am I correct that this functions basically the same as I had it

Yes with the benefit of "sampling" the data at one place to ENSURE consistency in your data

> [@lesmar96](#):
>
> but this is definitely neater?

Neater? urm yes, but also, that is actually how you SHOULD make JS objects

> [@lesmar96](#):
>
> But my main question still remains unanswered. This has been puzzling me for several days. Let me keep thinking though.

What is that? Are you refering to the required go/no-go logic?

does this help...

> [@Steve-Mcl](#):
>
> The logic in "Machine\_running" is this...  
> "If any manifold == true OR pressure is \> 0 THEN proceed"
> 
> ```auto
> if (msg.manifoldON || (msg.pressure > 0)) {
> return msg;
> }
> 
> ```
> 
> to
> 
> If you need "If any manifold == true AND pressure is \> 0 THEN proceed" change the below...
> 
> ```auto
> if (msg.manifoldON && (msg.pressure > 0)) {
> return msg;
> }
> 
> ```

---

<div class="post-metadata">

**Author:** ![lesmar96](https://avatars.discourse-cdn.com/v4/letter/l/b5a626/32.png) [@lesmar96](https://discourse.nodered.org/u/lesmar96)\
**Post date:** [23 May 2020 15:47 UTC](https://discourse.nodered.org/t/how-to-keep-flow-running-after-one-of-the-triggers-is-false/27079/29 "2020-05-23T15:47:10Z")

</div>

OK. so I understand your AND & OR functions. that all makes sense to me, but apparently I am not being clear. This is where I am puzzled.

We must return msg the entire length of the cycle that this machine runs. `if (msg.manifoldON && (msg.pressure > 0)) {` will signal that the cycle has started. The cycle must continue to run until pressure = 0 no matter the status of the manifold (the manifolds will turn on and off during the cycle but the pump will continue to run). That is why neither OR or AND is satisfactory.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [23 May 2020 16:05 UTC](https://discourse.nodered.org/t/how-to-keep-flow-running-after-one-of-the-triggers-is-false/27079/30 "2020-05-23T16:05:15Z")

</div>

Like this?

 ![2mOdL5Tw2g](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/8/68e36f29b22a4726f0256052212802e3cff55f86.gif)

in the function "Machine\_running"

```auto
//get all data in one place & pass it on to next node
//this ensures data is consistent & unchanged before sending to database
//i.e. any change in the GLOBALS will not affect these samples

msg.ProjectName = "LoadNGo";
msg.cycleID = global.get("cycleID");
msg.datetime = global.get('datetime') || 0;
msg.flow = global.get('flow') || 0;
msg.pressure = global.get('pressure') || 0;
msg.tempF = global.get('TempF') || 0;
msg.manifold1 = global.get('manifold1') || 0;
msg.manifold2 = global.get('manifold2') || 0;
msg.manifold3 = global.get('manifold3') || 0;
msg.manifold4 = global.get('manifold4') || 0;
msg.manifold5 = global.get('manifold5') || 0;
msg.manifold6 = global.get('manifold6') || 0;

var manifoldON;
var running = !!global.get("running");

if (msg.manifold1 || msg.manifold2 || msg.manifold3 || 
    msg.manifold4 || msg.manifold5 || msg.manifold6) {
    manifoldON = true;
}

//if both are true, start running
if (manifoldON && msg.pressure){
    running = true;
}

//if both are false, stop running
if (!manifoldON && !msg.pressure) {
    running = false;
}
global.set("running", running);

if (running) {
    return msg;
}

return null; //halt flow

```

play with that logic to suit

---

<div class="post-metadata">

**Author:** ![lesmar96](https://avatars.discourse-cdn.com/v4/letter/l/b5a626/32.png) [@lesmar96](https://discourse.nodered.org/u/lesmar96)\
**Post date:** [23 May 2020 17:15 UTC](https://discourse.nodered.org/t/how-to-keep-flow-running-after-one-of-the-triggers-is-false/27079/31 "2020-05-23T17:15:11Z")

</div>

Great. Thanks! I knew there was some way and I just couldn't think of it. I will be able to take care of it from there. Thanks!

[Previous page](https://discourse.nodered.org/t/how-to-keep-flow-running-after-one-of-the-triggers-is-false/27079.md?page=1)
