# Global variable to CSV file

**URL:** <https://discourse.nodered.org/t/global-variable-to-csv-file/81740>\
**Category:** General\
**Created:** [3 October 2023 09:07 UTC](https://discourse.nodered.org/t/global-variable-to-csv-file/81740 "2023-10-03T09:07:46Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![robertobuti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/robertobuti/32/81936_2.png) [@robertobuti](https://discourse.nodered.org/u/robertobuti)\
**Post date:** [3 October 2023 09:07 UTC](https://discourse.nodered.org/t/global-variable-to-csv-file/81740/1 "2023-10-03T09:07:46Z")

</div>

Hello,  
i'm begginer on node-red , i do many thing using forum & tutorial, however some thing like this seem not so friendly for dummies...  
My goal is get the status & value of some nodes in the flow and save them in a CSV file, i'm trying now with just one to start.  
I will save values & status on global variables, but i'm in difficult to get it and save .  
Take a look on "change node" named generate single payload, where i'm wrong ?  
Json content :  
{"phone": global.get("phone\_number")}

thank for any suggestion

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/2/22aa6ab5debb1590f825a5fa770e27803fbe23f0.png)

```auto
[{"id":"e9546682.b39898","type":"csv","z":"985a6a5573abba9f","name":"","sep":",","hdrin":false,"hdrout":"none","multi":"one","ret":"\\n","temp":"a,b","skip":"0","strings":false,"include_empty_strings":true,"include_null_values":true,"x":670,"y":260,"wires":[["3b580ccc8de65992"]]},{"id":"3b580ccc8de65992","type":"file","z":"985a6a5573abba9f","name":"config file","filename":"config/config.csv","filenameType":"str","appendNewline":false,"createDir":true,"overwriteFile":"true","encoding":"none","x":840,"y":260,"wires":[["4b39ba893e1d4001"]]},{"id":"e12283a855930fa6","type":"ui_button","z":"985a6a5573abba9f","name":"Save cfg","group":"4f5948e028bd912a","order":2,"width":0,"height":0,"passthru":false,"label":"Salva configurazione","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"dummy","payloadType":"str","topic":"topic","topicType":"msg","x":120,"y":260,"wires":[["5200f14e01daa8ad"]]},{"id":"da7ab0d5cc9369b5","type":"ui_switch","z":"985a6a5573abba9f","name":"","label":"Abilitazione allarme 1","tooltip":"","group":"4f5948e028bd912a","order":3,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"topic","topicType":"msg","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","animate":false,"className":"","x":160,"y":140,"wires":[[]]},{"id":"63d09d2e8feb5cc9","type":"function","z":"985a6a5573abba9f","name":"store to phone_number","func":"global.set(\"phone_number\",msg)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":450,"y":80,"wires":[[]]},{"id":"d4ab8b989e7d00c9","type":"ui_text_input","z":"985a6a5573abba9f","name":"","label":"","tooltip":"","group":"4f5948e028bd912a","order":3,"width":0,"height":0,"passthru":false,"mode":"text","delay":300,"topic":"topic","sendOnBlur":true,"className":"","topicType":"msg","x":120,"y":80,"wires":[["63d09d2e8feb5cc9"]]},{"id":"df87a7e2bc0a0eaf","type":"inject","z":"985a6a5573abba9f","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":140,"y":340,"wires":[["5200f14e01daa8ad"]]},{"id":"5200f14e01daa8ad","type":"change","z":"985a6a5573abba9f","name":"Generate single payload","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"phone\": global.get(\"phone_number\")}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":430,"y":260,"wires":[["e9546682.b39898"]]},{"id":"4b39ba893e1d4001","type":"debug","z":"985a6a5573abba9f","name":"debug 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1000,"y":260,"wires":[]},{"id":"4f5948e028bd912a","type":"ui_group","z":"985a6a5573abba9f","name":"gestione configurazioni","tab":"19578a1bc2ab85e0","order":1,"disp":true,"width":"6","collapse":false,"className":""},{"id":"19578a1bc2ab85e0","type":"ui_tab","name":"Configurazione","icon":"dashboard","order":5,"disabled":false,"hidden":false}]

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [3 October 2023 09:31 UTC](https://discourse.nodered.org/t/global-variable-to-csv-file/81740/2 "2023-10-03T09:31:01Z")

</div>

`{"phone": global.get("phone_number")}` is not valid.

For JSONata, it would be...

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/f/7fef9e40d1e9f91f1becca6323152ec2bb48c3da.png)

I find it easier to use a change node to grab the value from global than messing with JSONata  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/2/c27d83163ac224995ced680a8596533cf520e575.png)

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [3 October 2023 09:43 UTC](https://discourse.nodered.org/t/global-variable-to-csv-file/81740/3 "2023-10-03T09:43:15Z")

</div>

Do you really want to store the whole message object in the global variable?  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/7/776bcd097119ec7f1900c0f2e49a54df1e0c9cd2.png)

It seems more logical to store just the payload `"011 39 345 678 9012"`,  
an object `{name: "robertobuti", phone: "011 39 345 678 9012"}`  
or even array of objects `[{name: "robertobuti", phone: "011 39 345 678 9012"}]`

---

<div class="post-metadata">

**Author:** ![robertobuti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/robertobuti/32/81936_2.png) [@robertobuti](https://discourse.nodered.org/u/robertobuti)\
**Post date:** [3 October 2023 09:48 UTC](https://discourse.nodered.org/t/global-variable-to-csv-file/81740/4 "2023-10-03T09:48:41Z")

</div>

Thank a lot, now it's working.

---

<div class="post-metadata">

**Author:** ![robertobuti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/robertobuti/32/81936_2.png) [@robertobuti](https://discourse.nodered.org/u/robertobuti)\
**Post date:** [3 October 2023 09:50 UTC](https://discourse.nodered.org/t/global-variable-to-csv-file/81740/5 "2023-10-03T09:50:27Z")

</div>

yes probaly you are right,i like save in CSV two column for each row, the name and value.... but really the object is not necessary, just to have easy file to read for user.

---

<div class="post-metadata">

**Author:** ![robertobuti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/robertobuti/32/81936_2.png) [@robertobuti](https://discourse.nodered.org/u/robertobuti)\
**Post date:** [3 October 2023 11:45 UTC](https://discourse.nodered.org/t/global-variable-to-csv-file/81740/6 "2023-10-03T11:45:11Z")

</div>

Fine i like save payload value only as suggested, i'm expecting now for field "a" the phone number and foir field "b" the status true or false, but i have from debug

"{""payload"":""+3333331"",""socketid"":""K18TNTfTDhc9ksKRAAAT"",""\_msgid"":""accf8ee3a87354b9""}","{""payload"":true,""socketid"":""K18TNTfTDhc9ksKRAAAT"",""\_msgid"":""c32d9da647c6d4cc""}"

not only the payload values, whats' wrong ?

```auto
[{"id":"e9546682.b39898","type":"csv","z":"985a6a5573abba9f","name":"","sep":",","hdrin":false,"hdrout":"none","multi":"one","ret":"\\n","temp":"a,b","skip":"0","strings":false,"include_empty_strings":false,"include_null_values":false,"x":670,"y":260,"wires":[["3b580ccc8de65992"]]},{"id":"3b580ccc8de65992","type":"file","z":"985a6a5573abba9f","name":"config file","filename":"config/config.csv","filenameType":"str","appendNewline":false,"createDir":true,"overwriteFile":"true","encoding":"none","x":880,"y":260,"wires":[[]]},{"id":"e12283a855930fa6","type":"ui_button","z":"985a6a5573abba9f","name":"Save cfg","group":"4f5948e028bd912a","order":2,"width":0,"height":0,"passthru":false,"label":"Salva configurazione","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"dummy","payloadType":"str","topic":"topic","topicType":"msg","x":120,"y":260,"wires":[["5200f14e01daa8ad"]]},{"id":"da7ab0d5cc9369b5","type":"ui_switch","z":"985a6a5573abba9f","name":"","label":"Abilitazione allarme 1","tooltip":"","group":"4f5948e028bd912a","order":3,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"topic","topicType":"msg","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","animate":false,"className":"","x":160,"y":140,"wires":[["28365c301ce8ae3c"]]},{"id":"63d09d2e8feb5cc9","type":"function","z":"985a6a5573abba9f","name":"store to phone_number","func":"global.set(\"phone_number\",msg)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":450,"y":80,"wires":[[]]},{"id":"d4ab8b989e7d00c9","type":"ui_text_input","z":"985a6a5573abba9f","name":"","label":"","tooltip":"","group":"4f5948e028bd912a","order":3,"width":0,"height":0,"passthru":false,"mode":"text","delay":300,"topic":"topic","sendOnBlur":true,"className":"","topicType":"msg","x":120,"y":80,"wires":[["63d09d2e8feb5cc9"]]},{"id":"df87a7e2bc0a0eaf","type":"inject","z":"985a6a5573abba9f","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":140,"y":340,"wires":[["5200f14e01daa8ad"]]},{"id":"5200f14e01daa8ad","type":"change","z":"985a6a5573abba9f","name":"Generate single payload","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\t \"a\": $globalContext(\"phone_number\"),\t \"b\": $globalContext(\"allarme1\")\t}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":430,"y":260,"wires":[["e9546682.b39898","44c5d625df5b6afd"]]},{"id":"44c5d625df5b6afd","type":"debug","z":"985a6a5573abba9f","name":"debug 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":880,"y":160,"wires":[]},{"id":"28365c301ce8ae3c","type":"function","z":"985a6a5573abba9f","name":"store to allarme 1","func":"global.set(\"allarme1\",msg)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":430,"y":140,"wires":[[]]},{"id":"4f5948e028bd912a","type":"ui_group","z":"985a6a5573abba9f","name":"gestione configurazioni","tab":"19578a1bc2ab85e0","order":1,"disp":true,"width":"6","collapse":false,"className":""},{"id":"19578a1bc2ab85e0","type":"ui_tab","name":"Configurazione","icon":"dashboard","order":5,"disabled":false,"hidden":false}]

```

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [3 October 2023 12:10 UTC](https://discourse.nodered.org/t/global-variable-to-csv-file/81740/7 "2023-10-03T12:10:10Z")

</div>

A message **object** has several properties, for example:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/b/cbbdbe8df9e1a32ac2e3d0a075dfded5bf107253.png)

You are setting your global variable to the complete msg object.  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/7/47eeae26d7d4504d060dfb622062abd16974034b.png)

If you only want the payload, use `global.set("phone_number", msg.payload)`

---

<div class="post-metadata">

**Author:** ![robertobuti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/robertobuti/32/81936_2.png) [@robertobuti](https://discourse.nodered.org/u/robertobuti)\
**Post date:** [3 October 2023 12:18 UTC](https://discourse.nodered.org/t/global-variable-to-csv-file/81740/8 "2023-10-03T12:18:22Z")

</div>

I patched it, msg to msg.payload, but wa snecessary also a refresh from ui to set the new values, now it's OK.  
thank a lot.

---

<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:** [3 October 2023 12:44 UTC](https://discourse.nodered.org/t/global-variable-to-csv-file/81740/9 "2023-10-03T12:44:42Z")

</div>

You might be better off using the ui-form so both items will be sent at the same time.

The ui-text node sends a msg everything that been typed in at 300 ms intervals.

Stick a debug node on the output of the ui-text so you can see what happens.

---

<div class="post-metadata">

**Author:** ![robertobuti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/robertobuti/32/81936_2.png) [@robertobuti](https://discourse.nodered.org/u/robertobuti)\
**Post date:** [4 October 2023 13:03 UTC](https://discourse.nodered.org/t/global-variable-to-csv-file/81740/10 "2023-10-04T13:03:48Z")

</div>

yes you are right, but im my case is not a problem, value is stored in global variable and saved on CSV file only with a manual button "save" click.  
thank for suggestion.  
r

---

<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:** [18 October 2023 13:04 UTC](https://discourse.nodered.org/t/global-variable-to-csv-file/81740/11 "2023-10-18T13:04:15Z")

</div>

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