# Function Node "On Start" Tab

**URL:** <https://discourse.nodered.org/t/function-node-on-start-tab/77257>\
**Category:** General\
**Created:** [5 April 2023 06:04 UTC](https://discourse.nodered.org/t/function-node-on-start-tab/77257 "2023-04-05T06:04:15Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![andyp](https://avatars.discourse-cdn.com/v4/letter/a/8baadc/32.png) [@andyp](https://discourse.nodered.org/u/andyp)\
**Post date:** [5 April 2023 06:04 UTC](https://discourse.nodered.org/t/function-node-on-start-tab/77257/1 "2023-04-05T06:04:15Z")

</div>

Hi

I am looking for examples on how to use the 'On Start' tab in a function node.

Can I use it like Arduino 'Void Setup()'? For example (and my example does not work), can I put in 'On Start' something like this:

```auto
static int number = 0; // (like a static in other languages)

```

And then in 'On Message' something like this:

```auto
number = number + 1;

```

And now expect the number to be updated each time the node receives a message?

Or should I use flow.get and flow.set with the variable initially set in a 'set defaults' function run by a inject node at start up?

---

<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:** [5 April 2023 08:34 UTC](https://discourse.nodered.org/t/function-node-on-start-tab/77257/2 "2023-04-05T08:34:35Z")

</div>

I'll admit that I've never found a use for it myself.

It runs when Node-RED starts so that is only once. There is no direct carry-over from on start to the main function so, yes, you need to use context/flow/global variables in order to pass data over.

[https://nodered.org/docs/user-guide/writing-functions#running-code-on-start](https://nodered.org/docs/user-guide/writing-functions#running-code-on-start)

---

<div class="post-metadata">

**Author:** ![andyp](https://avatars.discourse-cdn.com/v4/letter/a/8baadc/32.png) [@andyp](https://discourse.nodered.org/u/andyp)\
**Post date:** [5 April 2023 08:52 UTC](https://discourse.nodered.org/t/function-node-on-start-tab/77257/3 "2023-04-05T08:52:59Z")

</div>

Thanks.... I wonder why data is not carried between on start and on message?

Anyway, I will change to use context variables rather than flow - so thank you for that.....

---

<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:** [5 April 2023 09:23 UTC](https://discourse.nodered.org/t/function-node-on-start-tab/77257/4 "2023-04-05T09:23:05Z")

</div>

> [@andyp](#):
>
> I wonder why data is not carried between on start and on message

For the same reason they are not carried between successive on message events. On start is just an initial event which is automatically triggered on startup.

---

<div class="post-metadata">

**Author:** ![cameo69](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cameo69/32/66593_2.png) [@cameo69](https://discourse.nodered.org/u/cameo69)\
**Post date:** [5 April 2023 14:14 UTC](https://discourse.nodered.org/t/function-node-on-start-tab/77257/5 "2023-04-05T14:14:46Z")

</div>

> [@TotallyInformation](#):
>
> It runs when Node-RED starts so that is only once.

Either what you state is not entirely correct or the documentation is wrong:

[Writing Functions : Node-RED](https://nodered.org/docs/user-guide/writing-functions#running-code-on-start) states  
"_[...] the Function node provides an `On Start` tab [..] where you can provide code that will run **whenever the node is started**._"

I understand it would also run upon redeployment of a node.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [5 April 2023 16:03 UTC](https://discourse.nodered.org/t/function-node-on-start-tab/77257/6 "2023-04-05T16:03:26Z")

</div>

![Untitled 4](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/6/561d605b3b01a4a4541869246edcdc1e7af90e29.jpeg)

- **On Start** code runs when Node-red starts up.
- **On Stop** and **On Start** code runs when you make a significant change to that node (eg wiring the output to a debug) and deploy.
- **On Stop** and **On Start** code runs when you do a full deploy.
- If you make a trivial change such as moving the node or connecting a wire to it's input and deploy, **neither** run.
- **On Stop** code does not run on **sudo systemctl stop nodered**

I suppose you could set global variables in the On Start code of the first function in the first flow and be reasonably sure they are in place.

---

<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:** [5 April 2023 16:16 UTC](https://discourse.nodered.org/t/function-node-on-start-tab/77257/7 "2023-04-05T16:16:06Z")

</div>

> [@cameo69](#):
>
> I understand it would also run upon redeployment of a node.

My bad. You are correct I believe. I should have been clearer in my wording.

> [@jbudd](#):
>
> I suppose you could set global variables in the On Start code of the first function in the first flow and be reasonably sure they are in place.

OK, I'l slink away quietly and contemplate my sins now .... 😁

---

<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:** [5 April 2023 16:30 UTC](https://discourse.nodered.org/t/function-node-on-start-tab/77257/8 "2023-04-05T16:30:37Z")

</div>

> [@jbudd](#):
>
> **On Stop** code does not run on **sudo systemctl stop nodered**

I don't think that is correct.  
Run node-red-log in one window and in a different window run sudo systemctl stop nodered and see what appears in the log window.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [5 April 2023 16:49 UTC](https://discourse.nodered.org/t/function-node-on-start-tab/77257/9 "2023-04-05T16:49:20Z")

</div>

Yes, it shows up in node-red-log.

A node.warn() statement in On Stop does not show up in the editor though.  
Presumably the browser connection is already cut by that time.

---

<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:** [5 April 2023 20:17 UTC](https://discourse.nodered.org/t/function-node-on-start-tab/77257/10 "2023-04-05T20:17:10Z")

</div>

> [@jbudd](#):
>
> Presumably the browser connection is already cut by that time.

That seems likely, yes. The important thing is that is is run so that it can release any resources it might have grabbed.

---

<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 June 2023 20:17 UTC](https://discourse.nodered.org/t/function-node-on-start-tab/77257/11 "2023-06-04T20:17:47Z")

</div>

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