# State engine as a subflow

**URL:** https://discourse.nodered.org/t/state-engine-as-a-subflow/78951
**Category:** General
**Created:** [5 June 2023 08:42 UTC](https://discourse.nodered.org/t/state-engine-as-a-subflow/78951 "2023-06-05T08:42:10Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![joachimstrom](https://avatars.discourse-cdn.com/v4/letter/j/46a35a/32.png) [@joachimstrom](https://discourse.nodered.org/u/joachimstrom)
#### Post date: [5 June 2023 08:42 UTC](https://discourse.nodered.org/t/state-engine-as-a-subflow/78951/1 "2023-06-05T08:42:10Z")

</div>

Hi.  
Is it possible to build a state engine as a subflow?  
I want to be able to pass a numeric value (1-10) and based on that there is 3 different status variables that can change. 1 of the variables is dependant of the 2 other, so the subflow must be able to read it's own variables to set the correct output values.  
I'm kinda new to node red, so please be gentle with me 🙂

---

<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: [5 June 2023 09:01 UTC](https://discourse.nodered.org/t/state-engine-as-a-subflow/78951/2 "2023-06-05T09:01:13Z")

</div>

Yes it is possible, each instant of the subflow will have its own variables and should not interfere with the other instances.

---

<div class="post-metadata">

### Author: ![joachimstrom](https://avatars.discourse-cdn.com/v4/letter/j/46a35a/32.png) [@joachimstrom](https://discourse.nodered.org/u/joachimstrom)
#### Post date: [5 June 2023 10:52 UTC](https://discourse.nodered.org/t/state-engine-as-a-subflow/78951/3 "2023-06-05T10:52:13Z")

</div>

thanks for the quick response. How would I access the variables from a subflow instance?  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/c/acbee2ac1253af7f9dac618f01e85ca9c2998b44.png)  
Here I have 2 instances of my subflow. can I add the instanse number as part of the address?  
something like this:  
var sts1 = flow.get("Pump01.OperationState")

---

<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: [5 June 2023 11:05 UTC](https://discourse.nodered.org/t/state-engine-as-a-subflow/78951/4 "2023-06-05T11:05:51Z")

</div>

You would set a subflow property in the template, then when you use a subflow you would give the property a value. This value can be accessed using `env.get("property_name")` in a function node.

---

<div class="post-metadata">

### Author: ![joachimstrom](https://avatars.discourse-cdn.com/v4/letter/j/46a35a/32.png) [@joachimstrom](https://discourse.nodered.org/u/joachimstrom)
#### Post date: [5 June 2023 11:27 UTC](https://discourse.nodered.org/t/state-engine-as-a-subflow/78951/5 "2023-06-05T11:27:38Z")

</div>

Sorry for being a Noob here, but how do I set the subflow property value? i used the flow.set inside the subflow to hold the values, and expose these in the msg object. If I were to use the env.get() do I have to specify the name of the subflow instance as well?

---

<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: [5 June 2023 11:35 UTC](https://discourse.nodered.org/t/state-engine-as-a-subflow/78951/6 "2023-06-05T11:35:00Z")

</div>

THe docs will tell you [setting subflow property](https://nodered.org/docs/user-guide/editor/workspace/subflows#editing-subflow-properties)

You may benefit form viewing the [essentials videos](https://m.youtube.com/watch?list=PLyNBB9VCLmo1hyO-4fIZ08gqFcXBkHy-6&v=ksGeUD26Mw0&embeds_referring_euri=https%3A%2F%2Fnodered.org%2F&source_ve_path=OTY3MTQ&feature=emb_imp_woyt)

---

<div class="post-metadata">

### Author: ![joachimstrom](https://avatars.discourse-cdn.com/v4/letter/j/46a35a/32.png) [@joachimstrom](https://discourse.nodered.org/u/joachimstrom)
#### Post date: [5 June 2023 11:36 UTC](https://discourse.nodered.org/t/state-engine-as-a-subflow/78951/7 "2023-06-05T11:36:24Z")

</div>

Thanks for the links. I've been trying to do some research but had trouble finding relevant topics.  
Thanks for all the help so far 😃

---

<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: [4 August 2023 11:36 UTC](https://discourse.nodered.org/t/state-engine-as-a-subflow/78951/8 "2023-08-04T11:36:26Z")

</div>

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