Problems with global variable

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.

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

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!

Hello again!

I have made a few changes:

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

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!

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:

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

and look at the results.

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

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?

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.

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.

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

imagem_2021-05-06_123553

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 :slight_smile:

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

Something like this:

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

Try this

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

Thank you so much, it works!!

imagem_2021-05-06_160917

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?

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?

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

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