Function node doesn't return msg on Start

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

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

image

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

[{"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.

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?

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

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

Edit: You could use start-up-trigger. But I am not sure if I understand the problem you are facing.

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.

1 Like

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

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