# Problems with global variable

**URL:** <https://discourse.nodered.org/t/problems-with-global-variable/44752>\
**Category:** General\
**Created:** [26 April 2021 14:15 UTC](https://discourse.nodered.org/t/problems-with-global-variable/44752 "2021-04-26T14:15:41Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lousa](https://avatars.discourse-cdn.com/v4/letter/l/b77776/32.png) [@Lousa](https://discourse.nodered.org/u/Lousa)\
**Post date:** [26 April 2021 14:15 UTC](https://discourse.nodered.org/t/problems-with-global-variable/44752/1 "2021-04-26T14:15:41Z")

</div>

Hello!

I am making a project that sends information to a file using a form node to get the inputs, wich in this case is some values of voltage. Later, i want to simulate the reception of other values, current values to be specific, using for now a inject node (i have to use a serial COM), and save them in the same file.  
The file in the end must have the inicial inputs in each row (Voltage and Value), then, the index (which i get using (parts.index +1) in change node) plus the inputs plus the reveived values, in 3 columns.  
I have already done the folowing, but my problem is to understand how to make the inputs a global variable to use in the end when i send all the information to the file.

```auto
[{"id":"4d5f22d9.f913ec","type":"ui_form","z":"62df8887.e2aba8","name":"Send voltages values","label":"","group":"b7e0c0ec.6c043","order":4,"width":0,"height":0,"options":[{"label":"Voltage (V):","value":"Voltage1","type":"number","required":true,"rows":null},{"label":"Voltage (V):","value":"Voltage2","type":"number","required":true,"rows":null},{"label":"Voltage (V):","value":"Voltage3","type":"number","required":true,"rows":null},{"label":"Voltage (V):","value":"Voltage4","type":"number","required":true,"rows":null}],"formValue":{"Voltage1":"","Voltage2":"","Voltage3":"","Voltage4":""},"payload":"","submit":"Start","cancel":"Stop","topic":"topic","topicType":"msg","splitLayout":"","x":140,"y":80,"wires":[["9eee3b3d.ebd678"]]},{"id":"9eee3b3d.ebd678","type":"change","z":"62df8887.e2aba8","name":"","rules":[{"t":"set","p":"numeric","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":140,"y":160,"wires":[["cda0b5d6.406dc8","7f60c615.9446d8"]]},{"id":"b8499ad7.ef1dd8","type":"debug","z":"62df8887.e2aba8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1150,"y":340,"wires":[]},{"id":"cda0b5d6.406dc8","type":"debug","z":"62df8887.e2aba8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":150,"y":220,"wires":[]},{"id":"7f60c615.9446d8","type":"split","z":"62df8887.e2aba8","name":"","splt":"\\n","spltType":"str","arraySplt":"2","arraySpltType":"len","stream":false,"addname":"","x":330,"y":160,"wires":[["5eb9c9d9.950cf8"]]},{"id":"5eb9c9d9.950cf8","type":"change","z":"62df8887.e2aba8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"\"Voltage\" & \" \" & payload","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":520,"y":160,"wires":[["a4ba5b0b.21ce18"]]},{"id":"a4ba5b0b.21ce18","type":"join","z":"62df8887.e2aba8","name":"","mode":"custom","build":"string","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":710,"y":160,"wires":[["73c9cf3d.b9b01"]]},{"id":"2ad7ffa.cbf5e","type":"template","z":"62df8887.e2aba8","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"55\n65\n75\n85","x":300,"y":360,"wires":[["e9cf4f03.c99cf","3b2f3445.4a02cc"]]},{"id":"e9cf4f03.c99cf","type":"split","z":"62df8887.e2aba8","name":"","splt":"\\n","spltType":"str","arraySplt":"1","arraySpltType":"len","stream":false,"addname":"","x":110,"y":440,"wires":[["4e01174b.78a358"]]},{"id":"4e01174b.78a358","type":"change","z":"62df8887.e2aba8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"(parts.index +1) & \" \" & payload ","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":400,"y":440,"wires":[["bf436231.5b8fb"]]},{"id":"bf436231.5b8fb","type":"join","z":"62df8887.e2aba8","name":"","mode":"auto","build":"string","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":570,"y":440,"wires":[["73c9cf3d.b9b01"]]},{"id":"73c9cf3d.b9b01","type":"file","z":"62df8887.e2aba8","name":"","filename":"Desktop\\Files\\teste1.txt","appendNewline":true,"createDir":true,"overwriteFile":"false","encoding":"none","x":790,"y":340,"wires":[["b8499ad7.ef1dd8","be686752.b3e738"]]},{"id":"22a3448.fcef5bc","type":"inject","z":"62df8887.e2aba8","name":"Receive Current","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":"","topic":"","payload":"","payloadType":"date","x":140,"y":360,"wires":[["2ad7ffa.cbf5e"]]},{"id":"3b2f3445.4a02cc","type":"debug","z":"62df8887.e2aba8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":490,"y":360,"wires":[]},{"id":"25f2fadf.7b0c76","type":"comment","z":"62df8887.e2aba8","name":"Blank line + header","info":"","x":390,"y":240,"wires":[]},{"id":"d8fb2f57.72e29","type":"inject","z":"62df8887.e2aba8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"\" \"","payloadType":"jsonata","x":570,"y":240,"wires":[["73c9cf3d.b9b01"]]},{"id":"582ed720.205bd8","type":"inject","z":"62df8887.e2aba8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Index Current Voltage","payloadType":"str","x":520,"y":280,"wires":[["73c9cf3d.b9b01"]]},{"id":"6de3c7e8.314528","type":"function","z":"62df8887.e2aba8","name":"Global context","func":"var V = global.get(\"voltage\");\nglobal.set(\"voltage\", msg.payload);\nreturn msg;\n\n//it will store the msg.payload, received on the input of the node\n//to a global variable, that can be used anytime/flow\n\n\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":980,"y":140,"wires":[[]]},{"id":"f5173a9a.7a1b18","type":"change","z":"62df8887.e2aba8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"Voltage","tot":"global"}],"action":"","property":"","from":"","to":"","reg":false,"x":980,"y":180,"wires":[[]]},{"id":"be686752.b3e738","type":"function","z":"62df8887.e2aba8","name":"","func":"var i = msg.payload.col1;\nglobal.set(\"$col1\",i);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":920,"y":420,"wires":[["b8499ad7.ef1dd8"]]},{"id":"f6aa8ca5.6e7fc","type":"comment","z":"62df8887.e2aba8","name":"Options to get global variable","info":"","x":980,"y":100,"wires":[]},{"id":"b7e0c0ec.6c043","type":"ui_group","name":"Parameter","tab":"30246011.589d1","order":1,"disp":true,"width":8,"collapse":false},{"id":"30246011.589d1","type":"ui_tab","name":"Simulation","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

```

The inputs and the file obtained are:  
 ![file](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/a/6a9292c4a82c7d86694a9c3e301a3337b95ee5f7.png)

 ![inputs](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/1/c173ce330e6177204341bc9146dd4c893d6fb90e.png)

I have tried to use a function in the beginning to define the global variable or even a change node (with set mode to make msg.payload to global):

var V = global.get("voltage");  
global.set("voltage", msg.payload);  
return msg;

And still, i don´t understand waht i am doing wrong. Other problem is when i save the file in csv format, all the values are in column 1. How can i distribute them in different columns?

Sorry for the long post, i hope someone can help in any form. Thank you!

---

<div class="post-metadata">

**Author:** ![Lousa](https://avatars.discourse-cdn.com/v4/letter/l/b77776/32.png) [@Lousa](https://discourse.nodered.org/u/Lousa)\
**Post date:** [5 May 2021 14:18 UTC](https://discourse.nodered.org/t/problems-with-global-variable/44752/2 "2021-05-05T14:18:51Z")

</div>

Hello again!

I have made a few changes:

```auto
[{"id":"4d5f22d9.f913ec","type":"ui_form","z":"62df8887.e2aba8","name":"Send voltages values","label":"","group":"b7e0c0ec.6c043","order":4,"width":0,"height":0,"options":[{"label":"Voltage (V):","value":"Voltage1","type":"number","required":true,"rows":null},{"label":"Voltage (V):","value":"Voltage2","type":"number","required":true,"rows":null},{"label":"Voltage (V):","value":"Voltage3","type":"number","required":true,"rows":null},{"label":"Voltage (V):","value":"Voltage4","type":"number","required":true,"rows":null}],"formValue":{"Voltage1":"","Voltage2":"","Voltage3":"","Voltage4":""},"payload":"","submit":"Start","cancel":"Stop","topic":"topic","topicType":"msg","splitLayout":"","x":120,"y":80,"wires":[["9eee3b3d.ebd678"]]},{"id":"9eee3b3d.ebd678","type":"change","z":"62df8887.e2aba8","name":"","rules":[{"t":"set","p":"numeric","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":80,"wires":[["f85c2bb4.71d2c8"]]},{"id":"b8499ad7.ef1dd8","type":"debug","z":"62df8887.e2aba8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":990,"y":300,"wires":[]},{"id":"7f60c615.9446d8","type":"split","z":"62df8887.e2aba8","name":"","splt":"\\n","spltType":"str","arraySplt":"1","arraySpltType":"len","stream":false,"addname":"","x":150,"y":180,"wires":[["5eb9c9d9.950cf8"]]},{"id":"5eb9c9d9.950cf8","type":"change","z":"62df8887.e2aba8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"\"Voltage\" & \",\" & payload","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":180,"wires":[["a4ba5b0b.21ce18"]]},{"id":"a4ba5b0b.21ce18","type":"join","z":"62df8887.e2aba8","name":"","mode":"custom","build":"string","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":510,"y":180,"wires":[["73c9cf3d.b9b01"]]},{"id":"2ad7ffa.cbf5e","type":"template","z":"62df8887.e2aba8","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"55\n65\n75\n85","x":320,"y":360,"wires":[["e9cf4f03.c99cf"]]},{"id":"e9cf4f03.c99cf","type":"split","z":"62df8887.e2aba8","name":"","splt":"\\n","spltType":"str","arraySplt":"1","arraySpltType":"len","stream":false,"addname":"","x":130,"y":440,"wires":[["4e01174b.78a358"]]},{"id":"4e01174b.78a358","type":"change","z":"62df8887.e2aba8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"(parts.index +1) & \",\" & payload & \",\" ","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":300,"y":440,"wires":[["f32643d2.a32ca","f165f7e9.a26db8"]]},{"id":"bf436231.5b8fb","type":"join","z":"62df8887.e2aba8","name":"","mode":"custom","build":"string","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":650,"y":440,"wires":[["73c9cf3d.b9b01"]]},{"id":"73c9cf3d.b9b01","type":"file","z":"62df8887.e2aba8","name":"","filename":"\\Desktop\\Files\\new.txt","appendNewline":true,"createDir":true,"overwriteFile":"false","encoding":"none","x":780,"y":300,"wires":[["b8499ad7.ef1dd8"]]},{"id":"22a3448.fcef5bc","type":"inject","z":"62df8887.e2aba8","name":"Receive Current","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":"","topic":"","payload":"","payloadType":"date","x":140,"y":360,"wires":[["2ad7ffa.cbf5e"]]},{"id":"25f2fadf.7b0c76","type":"comment","z":"62df8887.e2aba8","name":"Blank line + header","info":"","x":290,"y":240,"wires":[]},{"id":"d8fb2f57.72e29","type":"inject","z":"62df8887.e2aba8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"\" \"","payloadType":"jsonata","x":470,"y":240,"wires":[["73c9cf3d.b9b01"]]},{"id":"582ed720.205bd8","type":"inject","z":"62df8887.e2aba8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Index,Current,Voltage","payloadType":"str","x":360,"y":280,"wires":[["73c9cf3d.b9b01"]]},{"id":"f85c2bb4.71d2c8","type":"function","z":"62df8887.e2aba8","name":"Set global","func":"global.set(\"VoltageValue\", msg.payload);\nreturn msg;\n\n//Set the value of msg.payload in the variable \" \"","outputs":1,"noerr":0,"initialize":"","finalize":"","x":500,"y":80,"wires":[["7f60c615.9446d8"]]},{"id":"f32643d2.a32ca","type":"function","z":"62df8887.e2aba8","name":"Read Global","func":"var voltage = global.get(\"VoltageValue\");\nvar current = msg.payload;\nvar final = current + voltage; \nmsg.payload = final;\n\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":490,"y":440,"wires":[["bf436231.5b8fb","1a46a42b.c24dfc"]]},{"id":"f165f7e9.a26db8","type":"debug","z":"62df8887.e2aba8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":310,"y":520,"wires":[]},{"id":"1a46a42b.c24dfc","type":"debug","z":"62df8887.e2aba8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":650,"y":520,"wires":[]},{"id":"b7e0c0ec.6c043","type":"ui_group","name":"Parameter","tab":"30246011.589d1","order":1,"disp":true,"width":8,"collapse":false},{"id":"30246011.589d1","type":"ui_tab","name":"Simulation","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

```

But is not showing the values:

![imagem_2021-05-05_151628](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/c/fc036ff807e3965b71589d875240afac770c439c.png)

Can someone help? The problem must be in the reading of global variable.. Also, i have already found how to distribute the values in columns, in excel. Thank you again!

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [5 May 2021 21:29 UTC](https://discourse.nodered.org/t/problems-with-global-variable/44752/3 "2021-05-05T21:29:00Z")

</div>

Add a debug node (set to output the complete msg object) to the output to the `change` node just before the 'Read Global' `function` node and look at the results.

Alsp add the following to the `function` node after line 2:

```auto
node.warn("current="+current)
node.warn("voltage="+voltage)

```

and look at the results.

---

<div class="post-metadata">

**Author:** ![Lousa](https://avatars.discourse-cdn.com/v4/letter/l/b77776/32.png) [@Lousa](https://discourse.nodered.org/u/Lousa)\
**Post date:** [6 May 2021 10:30 UTC](https://discourse.nodered.org/t/problems-with-global-variable/44752/4 "2021-05-06T10:30:53Z")

</div>

First thank you for answering! I already added that debug node and the lines in the function node, and i receive this warning:

 ![foto](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/e/ceb65f6fffe2766ba7e50eba9fb471c7a1dfe200.png)

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [6 May 2021 10:54 UTC](https://discourse.nodered.org/t/problems-with-global-variable/44752/5 "2021-05-06T10:54:44Z")

</div>

So looking at those results you can see the values in those variables. In your code what are you trying to do with them next?

---

<div class="post-metadata">

**Author:** ![Lousa](https://avatars.discourse-cdn.com/v4/letter/l/b77776/32.png) [@Lousa](https://discourse.nodered.org/u/Lousa)\
**Post date:** [6 May 2021 11:13 UTC](https://discourse.nodered.org/t/problems-with-global-variable/44752/6 "2021-05-06T11:13:23Z")

</div>

Exacly, i can see the values. What i am trying to do in this step is to save in .txt file all the values in the format of index,current,voltage, in same row. And the voltage values are coming from the global variable. My doubt is why can´t get the voltage values in the end.  
After the values saved in that file, i will read the file, convert the string of values to integer and try to obtain a chart with this values, if am i'm thinking right in the way of doing this.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [6 May 2021 11:20 UTC](https://discourse.nodered.org/t/problems-with-global-variable/44752/7 "2021-05-06T11:20:38Z")

</div>

The global variable is an object so when you set 'voltage' to the global, voltage is now an object. If you want to get the parts of the object you need to use 'voltage.Voltage1' etc.

---

<div class="post-metadata">

**Author:** ![Lousa](https://avatars.discourse-cdn.com/v4/letter/l/b77776/32.png) [@Lousa](https://discourse.nodered.org/u/Lousa)\
**Post date:** [6 May 2021 11:41 UTC](https://discourse.nodered.org/t/problems-with-global-variable/44752/8 "2021-05-06T11:41:12Z")

</div>

Oh, makes sense! I was not understanding that, that way i get the first value, like you say:

![imagem_2021-05-06_123553](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/f/9f38e03bf9e7ac7b89ea36cf44cf942dce34391d.png)

If i want to get all the values, how do i write that in the function? I can´t have a "final" variable that makes the final result, because will always renew that. Should i make current variable also a global variable or it exists other way to do it? Sorry for the simple questions but i am still learning node-red 🙂

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [6 May 2021 12:17 UTC](https://discourse.nodered.org/t/problems-with-global-variable/44752/9 "2021-05-06T12:17:19Z")

</div>

can you show me an example of what you want the 'final' value to look like.

---

<div class="post-metadata">

**Author:** ![Lousa](https://avatars.discourse-cdn.com/v4/letter/l/b77776/32.png) [@Lousa](https://discourse.nodered.org/u/Lousa)\
**Post date:** [6 May 2021 12:18 UTC](https://discourse.nodered.org/t/problems-with-global-variable/44752/10 "2021-05-06T12:18:38Z")

</div>

Something like this:

Index,Voltage,Current  
1,50,55  
2,60,65  
3,70,75  
4,80,85

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [6 May 2021 14:43 UTC](https://discourse.nodered.org/t/problems-with-global-variable/44752/11 "2021-05-06T14:43:34Z")

</div>

Try this

```auto
[{"id":"b8499ad7.ef1dd8","type":"debug","z":"cb52647f.66b49","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":830,"y":420,"wires":[]},{"id":"2ad7ffa.cbf5e","type":"template","z":"cb52647f.66b49","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"55\n65\n75\n85","output":"str","x":360,"y":420,"wires":[["e9cf4f03.c99cf"]]},{"id":"e9cf4f03.c99cf","type":"split","z":"cb52647f.66b49","name":"","splt":"\\n","spltType":"str","arraySplt":"1","arraySpltType":"len","stream":false,"addname":"","x":130,"y":500,"wires":[["270a15ef.efa8fa","f32643d2.a32ca"]]},{"id":"bf436231.5b8fb","type":"join","z":"cb52647f.66b49","name":"","mode":"custom","build":"string","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":690,"y":500,"wires":[["73c9cf3d.b9b01","a84bc3f0.725108"]]},{"id":"73c9cf3d.b9b01","type":"file","z":"cb52647f.66b49","name":"","filename":"Users/Paul/tmp/new.txt","appendNewline":true,"createDir":true,"overwriteFile":"false","encoding":"none","x":830,"y":360,"wires":[["b8499ad7.ef1dd8"]]},{"id":"22a3448.fcef5bc","type":"inject","z":"cb52647f.66b49","name":"Receive Current","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":"","topic":"","payload":"","payloadType":"date","x":180,"y":420,"wires":[["2ad7ffa.cbf5e"]]},{"id":"f32643d2.a32ca","type":"function","z":"cb52647f.66b49","name":"Read Global","func":"var index = msg.parts.index+1\nvar current = msg.payload;\nnode.warn(\"current=\"+current)\n\nvar voltage_obj = global.get(\"VoltageValue\");\n//node.warn(voltage_obj);\n\nvar voltage_str = Object.values(voltage_obj);\n//node.warn(Object.values(voltage_obj));\n//node.warn(\"voltage_str\"+voltage_str);\n//node.warn(typeof voltage_str);\n\nvar voltage_arr = Array.from(voltage_str)\n//node.warn(\"voltage_array=\"+voltage_arr)\n//node.warn(typeof voltage_str);\n//node.warn(\"index=\" + index);\nvar voltage = voltage_arr[index-1];\n\nnode.warn(\"voltage=\"+voltage)\n\nvar final = index + \",\" + current + \",\" + voltage; \nmsg.payload = final;\n\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":530,"y":500,"wires":[["bf436231.5b8fb","1a46a42b.c24dfc"]]},{"id":"a84bc3f0.725108","type":"debug","z":"cb52647f.66b49","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":870,"y":520,"wires":[]}]

```

note you need to change the `file-out` node's file path.

---

<div class="post-metadata">

**Author:** ![Lousa](https://avatars.discourse-cdn.com/v4/letter/l/b77776/32.png) [@Lousa](https://discourse.nodered.org/u/Lousa)\
**Post date:** [6 May 2021 15:08 UTC](https://discourse.nodered.org/t/problems-with-global-variable/44752/12 "2021-05-06T15:08:23Z")

</div>

Thank you so much, it works!!

![imagem_2021-05-06_160917](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/0/f0a4babd0b4bdf5aacbd46d0f905eea1743552ba.png)

It is more simple than using the change node in this case, and you defined the variables and extract the necessary, i did'nt know this way. But it was posible to define the current as a global variable too, right?

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [6 May 2021 15:20 UTC](https://discourse.nodered.org/t/problems-with-global-variable/44752/13 "2021-05-06T15:20:10Z")

</div>

there are always more than one way to solve a problem. It might be possible to do it in `change` nodes, but because you already had a `function` node doing some of the work, I consolidated and got rid of your `change` node.

Why do you want to define the current as a global?

---

<div class="post-metadata">

**Author:** ![Lousa](https://avatars.discourse-cdn.com/v4/letter/l/b77776/32.png) [@Lousa](https://discourse.nodered.org/u/Lousa)\
**Post date:** [6 May 2021 16:01 UTC](https://discourse.nodered.org/t/problems-with-global-variable/44752/14 "2021-05-06T16:01:10Z")

</div>

Yes you are right, there is always many ways to do it. I was asking just for curiosity, this works perfectly. Thank you again 🙂

---

<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:** [20 May 2021 16:01 UTC](https://discourse.nodered.org/t/problems-with-global-variable/44752/15 "2021-05-20T16:01:11Z")

</div>

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