# Flow variable initialization

**URL:** <https://discourse.nodered.org/t/flow-variable-initialization/41810>\
**Category:** General\
**Created:** [1 March 2021 13:17 UTC](https://discourse.nodered.org/t/flow-variable-initialization/41810 "2021-03-01T13:17:23Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![fmarzocca](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/fmarzocca/32/35901_2.png) [@fmarzocca](https://discourse.nodered.org/u/fmarzocca)\
**Post date:** [1 March 2021 13:17 UTC](https://discourse.nodered.org/t/flow-variable-initialization/41810/1 "2021-03-01T13:17:23Z")

</div>

I have several flow variables that need to be initialized at first deploying. I am using the "Setup" tab in a function node for this purpose.  
Am I right?  
What happens when NodeRed is restarted?

---

<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:** [1 March 2021 13:31 UTC](https://discourse.nodered.org/t/flow-variable-initialization/41810/2 "2021-03-01T13:31:18Z")

</div>

The setup tab of a function node will get run once whenever the node is started. That happens when the runtime is started and when updated flows are deployed.

You'll want to add a check to make sure the flow variables are undefined before setting them to avoid overwriting them when you deploy changes.... unless of course that's what you want to happen.

---

<div class="post-metadata">

**Author:** ![fmarzocca](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/fmarzocca/32/35901_2.png) [@fmarzocca](https://discourse.nodered.org/u/fmarzocca)\
**Post date:** [1 March 2021 13:32 UTC](https://discourse.nodered.org/t/flow-variable-initialization/41810/3 "2021-03-01T13:32:23Z")

</div>

Yea, and I am adding that check in the "setup" tab. Is that correct?

I have added this in the setup tab of the function:

`if (flow.get("enable_ir_osp") == null) flow.set("enable_ir_osp", true);`

Am I protected also from NodeRed restarts?

---

<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:** [1 March 2021 13:34 UTC](https://discourse.nodered.org/t/flow-variable-initialization/41810/4 "2021-03-01T13:34:02Z")

</div>

> [@fmarzocca](#):
>
> Yea, and I am adding that check in the "setup" tab. Is that correct?

Yes - add a check in your code before you set anything.

---

<div class="post-metadata">

**Author:** ![fmarzocca](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/fmarzocca/32/35901_2.png) [@fmarzocca](https://discourse.nodered.org/u/fmarzocca)\
**Post date:** [1 March 2021 13:34 UTC](https://discourse.nodered.org/t/flow-variable-initialization/41810/5 "2021-03-01T13:34:24Z")

</div>

I have edited my previous post

---

<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:** [1 March 2021 13:36 UTC](https://discourse.nodered.org/t/flow-variable-initialization/41810/6 "2021-03-01T13:36:00Z")

</div>

I'm sure you could quickly test it and see.

---

<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:** [1 March 2021 13:36 UTC](https://discourse.nodered.org/t/flow-variable-initialization/41810/7 "2021-03-01T13:36:44Z")

</div>

You can also use an inject node configured to fire a startup feeding a change node (or other) to initialise them.

---

<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:** [15 March 2021 13:36 UTC](https://discourse.nodered.org/t/flow-variable-initialization/41810/8 "2021-03-15T13:36:47Z")

</div>

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