How to create a node that defines a global variable

Before going into the main function, I want to define the functions and variables common for the node-red application.

I created a flow like below, but when start then the main function is executed 4 times

[{"id":"7f7eddb6.083e34","type":"inject","z":"dc9ac215.0a6df","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"str","x":1430,"y":1220,"wires":[["8889ad9f.486fe","a9caa0bc.d18bf","1460f46b.33728c","7ae4178f.2156f8"]]},{"id":"8889ad9f.486fe","type":"function","z":"dc9ac215.0a6df","name":"Common function","func":"return msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1630,"y":1160,"wires":[["6ea98c00.ec07e4"]]},{"id":"a9caa0bc.d18bf","type":"change","z":"dc9ac215.0a6df","name":"INI file definition","rules":[{"t":"set","p":"ss1","pt":"global","to":"SESSION1","tot":"str"},{"t":"set","p":"ss1k1","pt":"global","to":"KEY1","tot":"str"},{"t":"set","p":"ss1k2","pt":"global","to":"KEY2","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1620,"y":1200,"wires":[["6ea98c00.ec07e4"]]},{"id":"1460f46b.33728c","type":"change","z":"dc9ac215.0a6df","name":"Path definition","rules":[{"t":"set","p":"imgPath","pt":"global","to":"/Temp/images","tot":"str"},{"t":"set","p":"docPath","pt":"global","to":"/Temp/docs","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1620,"y":1240,"wires":[["6ea98c00.ec07e4"]]},{"id":"7ae4178f.2156f8","type":"change","z":"dc9ac215.0a6df","name":"Message definition","rules":[{"t":"set","p":"msgIniNotFound","pt":"global","to":"The INI file cannot be found.","tot":"str"},{"t":"set","p":"msgFilenameInvalid","pt":"global","to":"The file name is invalid.","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1630,"y":1280,"wires":[["6ea98c00.ec07e4"]]},{"id":"6ea98c00.ec07e4","type":"function","z":"dc9ac215.0a6df","name":"Main Function","func":"msg.payload = 'Hello Node-RED';\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1860,"y":1220,"wires":[["779dfbba.d44114"]]},{"id":"779dfbba.d44114","type":"debug","z":"dc9ac215.0a6df","name":"mainFunc","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":2020,"y":1220,"wires":[]}]

In this case I want to execute the main function once after 4 definitions have been done.
What should I do?
Any hint will be great.
Thanks.

If you don't want four messages then don't split your flow into four separate branches - keep the nodes in serial.

More generally, if this is a one-time setup of things in context, use a separate flow that is triggered by an Inject node configured to fire once on startup.

1 Like

And you can put multiple conditions into a single change node

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