# Function node doesn't return msg on Start

**URL:** https://discourse.nodered.org/t/function-node-doesnt-return-msg-on-start/50644
**Category:** General
**Created:** [5 September 2021 17:15 UTC](https://discourse.nodered.org/t/function-node-doesnt-return-msg-on-start/50644 "2021-09-05T17:15:01Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![GiovanniG](https://avatars.discourse-cdn.com/v4/letter/g/ecd19e/32.png) [@GiovanniG](https://discourse.nodered.org/u/GiovanniG)
#### Post date: [5 September 2021 17:15 UTC](https://discourse.nodered.org/t/function-node-doesnt-return-msg-on-start/50644/1 "2021-09-05T17:15:01Z")

</div>

Hi, I'm testing a simply flow:

`[{"id":"7de8e2b7.8f26f4","type":"inject","z":"781f6239.1ae304","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":210,"y":80,"wires":[["a0823c36.bffff"]]},{"id":"7eea49b8.e7f528","type":"inject","z":"781f6239.1ae304","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":210,"y":120,"wires":[["34c43814.f79de"]]},{"id":"34c43814.f79de","type":"function","z":"781f6239.1ae304","name":"Reset test","func":"global.set(\"test\",false);\nreturn null;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":400,"y":120,"wires":[[]]},{"id":"a0823c36.bffff","type":"function","z":"781f6239.1ae304","name":"Start node","func":"msg={payload:global.get(\"test\")};\nreturn msg;","outputs":1,"noerr":0,"initialize":"// Code added here will be run once\n// whenever the node is started.\nglobal.set(\"test\",true);\nvar t;\nmsg={payload:true};\nreturn msg;","finalize":"","libs":[],"x":410,"y":60,"wires":[["9c0c0c8.bc26ff"]]},{"id":"9c0c0c8.bc26ff","type":"debug","z":"781f6239.1ae304","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":650,"y":100,"wires":[]}]`

it works for the return on Message (and an input is provided), but it doesn't return the message placed on Start, why?  
Maybe it' too fast in the beginning for the debug node to catch it?  
The start works for variables  
Thank you

PS: my goal is to activate some icons on homekit/google home at the beginning

---

<div class="post-metadata">

### Author: ![rko](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rko/32/45807_2.png) [@rko](https://discourse.nodered.org/u/rko)
#### Post date: [5 September 2021 17:46 UTC](https://discourse.nodered.org/t/function-node-doesnt-return-msg-on-start/50644/2 "2021-09-05T17:46:09Z")

</div>

If you put "inject once after .." in the inject nodes it should set your global variable:

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/1/911519dd73178ecbbcd59869058850082dfd1604.png)

just make sure the `reset` happens before `start node` by putting different times. Something like this:

```auto
[{"id":"7de8e2b7.8f26f4","type":"inject","z":"7b845fa398045e79","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":"2","topic":"","payloadType":"date","x":340,"y":480,"wires":[["a0823c36.bffff"]]},{"id":"7eea49b8.e7f528","type":"inject","z":"7b845fa398045e79","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"","payloadType":"date","x":340,"y":430,"wires":[["34c43814.f79de"]]},{"id":"34c43814.f79de","type":"function","z":"7b845fa398045e79","name":"Reset test","func":"global.set(\"test\",false);\nreturn null;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":520,"y":430,"wires":[[]]},{"id":"a0823c36.bffff","type":"function","z":"7b845fa398045e79","name":"Start node","func":"msg={payload:global.get(\"test\")};\nreturn msg;","outputs":1,"noerr":0,"initialize":"// Code added here will be run once\n// whenever the node is started.\nglobal.set(\"test\",true);\nvar t;\nmsg={payload:true};\nreturn msg;","finalize":"","libs":[],"x":530,"y":480,"wires":[["9c0c0c8.bc26ff"]]},{"id":"9c0c0c8.bc26ff","type":"debug","z":"7b845fa398045e79","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":730,"y":480,"wires":[]}]

```

Please format the flow samples as `code` (Preformatted text) next time.

---

<div class="post-metadata">

### Author: ![GiovanniG](https://avatars.discourse-cdn.com/v4/letter/g/ecd19e/32.png) [@GiovanniG](https://discourse.nodered.org/u/GiovanniG)
#### Post date: [5 September 2021 18:01 UTC](https://discourse.nodered.org/t/function-node-doesnt-return-msg-on-start/50644/3 "2021-09-05T18:01:12Z")

</div>

thanks, I really don't know how to use correctly this button, I paste text, select it and press \</\> but it seems not working properly.

I understood what you mean, in this case I've to add this node and modify the code on the Message window. I hoped it was much easly using the Start one. Why Start can't send messages?

---

<div class="post-metadata">

### Author: ![rko](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rko/32/45807_2.png) [@rko](https://discourse.nodered.org/u/rko)
#### Post date: [5 September 2021 18:34 UTC](https://discourse.nodered.org/t/function-node-doesnt-return-msg-on-start/50644/4 "2021-09-05T18:34:47Z")

</div>

> [@GiovanniG](#):
>
> thanks, I really don't know how to use correctly this button, I paste text, select it and press \</\> but it seems not working properly.

I think it's best to press \</\> first and then paste the flow data from the clipboard.

> [@GiovanniG](#):
>
> I understood what you mean, in this case I've to add this node and modify the code on the Message window. I hoped it was much easly using the Start one. Why Start can't send messages?

`Inject nodes` don't fire on startup, unless you use the mentioned option above (inject once after ...).

**Edit:** You could use [`start-up-trigger`](https://flows.nodered.org/node/node-red-contrib-startup-trigger). But I am not sure if I understand the problem you are facing.

---

<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: [5 September 2021 18:35 UTC](https://discourse.nodered.org/t/function-node-doesnt-return-msg-on-start/50644/5 "2021-09-05T18:35:10Z")

</div>

Returning messages from the Start code does nothing.

If you really must send something use `node.send`, however that code is run whilst the flows are starting, so there's no guarantee any other nodes are ready to receive the message.

So you could do the same as the inject node, and wrap the `node.send` in a `setTimeout` function that delays it by 150ms or so.

---

<div class="post-metadata">

### Author: ![GiovanniG](https://avatars.discourse-cdn.com/v4/letter/g/ecd19e/32.png) [@GiovanniG](https://discourse.nodered.org/u/GiovanniG)
#### Post date: [6 September 2021 12:24 UTC](https://discourse.nodered.org/t/function-node-doesnt-return-msg-on-start/50644/6 "2021-09-06T12:24:12Z")

</div>

thank you a lot, I'll proceed then with inject at startup.  
You talked about `setTimeout` you mean this code can be written inside a function to delay something? For example, I would need to delay the messages on different output, let the first arrive to a node (for example inizialize device), then the second output. Can I delay inside a function? Will it keep the processor busy for some reasons?  
At this moment, I'm doing it using a variable delay, passing the property delay: on message  
Thank you

---

<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: [5 November 2021 12:25 UTC](https://discourse.nodered.org/t/function-node-doesnt-return-msg-on-start/50644/7 "2021-11-05T12:25:10Z")

</div>

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