Global variable to CSV file

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

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

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

For JSONata, it would be...

I find it easier to use a change node to grab the value from global than messing with JSONata
image

1 Like

Do you really want to store the whole message object in the global variable?
image

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"}]

Thank a lot, now it's working.

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.

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 ?

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

A message object has several properties, for example:
image

You are setting your global variable to the complete msg object.
image

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

1 Like

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.

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.

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

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