# How to create a node that defines a global variable

**URL:** <https://discourse.nodered.org/t/how-to-create-a-node-that-defines-a-global-variable/33240>\
**Category:** General\
**Created:** [23 September 2020 16:45 UTC](https://discourse.nodered.org/t/how-to-create-a-node-that-defines-a-global-variable/33240 "2020-09-23T16:45:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![minhhn0205](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/minhhn0205/32/29062_2.png) [@minhhn0205](https://discourse.nodered.org/u/minhhn0205)\
**Post date:** [23 September 2020 16:45 UTC](https://discourse.nodered.org/t/how-to-create-a-node-that-defines-a-global-variable/33240/1 "2020-09-23T16:45:59Z")

</div>

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

 ![define](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/8/38d4a58b2aec78dc2628d09fc1b2df06eea60caa.png)

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

---

<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:** [23 September 2020 16:52 UTC](https://discourse.nodered.org/t/how-to-create-a-node-that-defines-a-global-variable/33240/2 "2020-09-23T16:52:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [23 September 2020 18:15 UTC](https://discourse.nodered.org/t/how-to-create-a-node-that-defines-a-global-variable/33240/4 "2020-09-23T18:15:40Z")

</div>

And you can put multiple conditions into a single change node

---

<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:** [7 October 2020 18:15 UTC](https://discourse.nodered.org/t/how-to-create-a-node-that-defines-a-global-variable/33240/5 "2020-10-07T18:15:45Z")

</div>

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