# Questions about "Sub-Flows" I may be missing something

**URL:** <https://discourse.nodered.org/t/questions-about-sub-flows-i-may-be-missing-something/7545>\
**Category:** General\
**Created:** [4 February 2019 03:28 UTC](https://discourse.nodered.org/t/questions-about-sub-flows-i-may-be-missing-something/7545 "2019-02-04T03:28:56Z")\
**Posts on this page:** 7\
**Page:** 1

<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:** [4 February 2019 03:28 UTC](https://discourse.nodered.org/t/questions-about-sub-flows-i-may-be-missing-something/7545/1 "2019-02-04T03:28:56Z")

</div>

I'm a bit stuck.

I get that subflows are a kind of Macro of a group of nodes.

They are limited to **one** input and I am not exactly sure about outputs. (But I'll look into that.)

So, I have a group of nodes which is repeated in multiple places in the flow.  
Ideally this is where a subflow is handy, because if there is a problem with the code/functionality, you only have to change one, and that propagates to the rest.

_Easy Peasy_

Where I am getting stuck is that although I have a lot of duplicated stuff which in ways would be terrific to do this with, there are a lot of variables which are not the same.

In my set up these variables are variables set for that specific part/machine/thing.  
So I have 10 machines, the function is repeated ten times (one for each machine) and can't be "macro'd" because the machine name is different in each.

I am starting to see how to get around this, but just want to ask, to help clear the confusion.

The **one input** is a sticking point for me.

I'll stop here because, for reasons unknown, the fog is actually thickening just now.

---

<div class="post-metadata">

**Author:** ![diramazioni](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/diramazioni/32/5668_2.png) [@diramazioni](https://discourse.nodered.org/u/diramazioni)\
**Post date:** [4 February 2019 04:34 UTC](https://discourse.nodered.org/t/questions-about-sub-flows-i-may-be-missing-something/7545/2 "2019-02-04T04:34:09Z")

</div>

Hi,  
I would stop calling it macro, subflows should be seen as a group of node that do a defined set of functionality. If these function-set are common to many machine they are good candidate to be reused and go in sub-flows as you said.  
Variables can have [Context scopes](https://nodered.org/docs/user-guide/context), they can be context, flow or global visibility.  
Using your example I would put in different flow each machine with custom flow-variable and put there the subflow that read the flow variables

---

<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:** [4 February 2019 04:38 UTC](https://discourse.nodered.org/t/questions-about-sub-flows-i-may-be-missing-something/7545/3 "2019-02-04T04:38:49Z")

</div>

I called them the best name I could at the time.

There is one machine with many many **many** copies of similar/same code/nodes.

I am wanting to work out if/how I could use this "sub flow" thing to reduce the flow complexity.

So, I am still slightly confused.

---

<div class="post-metadata">

**Author:** ![diramazioni](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/diramazioni/32/5668_2.png) [@diramazioni](https://discourse.nodered.org/u/diramazioni)\
**Post date:** [4 February 2019 05:54 UTC](https://discourse.nodered.org/t/questions-about-sub-flows-i-may-be-missing-something/7545/4 "2019-02-04T05:54:51Z")

</div>

what part are you confused about?  
The suggestion to use flow variable to "customize" you subflow execution should solve your problem. Otherwise you can pass it as input.  
I forgot to add that although the input node can only be one, it can be an array of different objects. You can use the join and split nodes for that.

---

<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:** [4 February 2019 06:06 UTC](https://discourse.nodered.org/t/questions-about-sub-flows-i-may-be-missing-something/7545/5 "2019-02-04T06:06:25Z")

</div>

Here is a **very small** part of how big my flows are:

You can see there is a lot f duplication.  
**BUT** Rationalising that to "sub flows" - at this point in time - is beyond me.  
Thus my asking.

I shall have to look at the structures and how the variables set.  
Most of this is/was built as needed. (On the fly, if you will.)  
As I got more RPIs I had to add another iteration of the code for the new machine.  
It is just getting difficult to do updates and if I find a bug (well, between finding them) it is **painful beyond belief** that I have to edit so many nodes.  
And so that could give rise to more bugs. 😜

 ![Screenshot%20from%202019-02-04%2016-59-43](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/3/3042c23cb3debe13bae66fe460ec19d380070d86.png) ![Screenshot%20from%202019-02-04%2016-59-59](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/1/1f8518f5a2116b0308da07bb9ed55f0dedd96ba7.png)

---

<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:** [4 February 2019 07:15 UTC](https://discourse.nodered.org/t/questions-about-sub-flows-i-may-be-missing-something/7545/6 "2019-02-04T07:15:35Z")

</div>

> [@Trying\_to\_learn](#):
>
> In my set up these variables are variables set for that specific part/machine/thing.  
> So I have 10 machines, the function is repeated ten times (one for each machine) and can't be "macro'd" because the machine name is different in each.
> 
> I am starting to see how to get around this, but just want to ask, to help clear the confusion.
> 
> The **one input** is a sticking point for me.

The easiest current approach would be to add the variables to your input msg's before calling the sub-flow. You can put a change node immediately before calling the sub-flow to add a new property to the msg with the data the sub-flow needs.

Alternatively, if the variable is independent of calling (e.g. a day/night variable), then use a global variable instead.

This was all covered in another recent thread.

---

<div class="post-metadata">

**Author:** ![realjax](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/realjax/32/895_2.png) [@realjax](https://discourse.nodered.org/u/realjax)\
**Post date:** [4 February 2019 07:59 UTC](https://discourse.nodered.org/t/questions-about-sub-flows-i-may-be-missing-something/7545/7 "2019-02-04T07:59:30Z")

</div>

The easiest way to find out how sublows can benefit you is to simply start. Start with a small repeatable block and create a subflow of that small piece of code. Then replace your original code with the subflow and take it from there. It's all a lot easier and more straightforward then it sounds.
