# How to change the order of CSV values written to a file

**URL:** https://discourse.nodered.org/t/how-to-change-the-order-of-csv-values-written-to-a-file/38286
**Category:** General
**Created:** [30 December 2020 13:13 UTC](https://discourse.nodered.org/t/how-to-change-the-order-of-csv-values-written-to-a-file/38286 "2020-12-30T13:13:24Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![cope](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cope/32/34515_2.png) [@cope](https://discourse.nodered.org/u/cope)
#### Post date: [30 December 2020 13:13 UTC](https://discourse.nodered.org/t/how-to-change-the-order-of-csv-values-written-to-a-file/38286/1 "2020-12-30T13:13:24Z")

</div>

I am using NodeRed with HomeSeer. In NodeRed I read two devices of a rainmeter, the daily and cumulative precipitation. In NodeRed I generate a timestamp and once a day I write the three values to a file.  
Now I would like to have the three values in a specific order and the timestamp first.  
This is what I have:

 ![2020-12-30_140023](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/8/a898a270b5378133a24a6c2f4f3c81eb2a0de476.png)

The CSV output is:

> 30-12-2020 14:00:33[node: f5ed2ce5.4fe4a](http://192.168.1.10:1880/#)daily : msg.payload : string[30]
> 
> "0.793,0.0,30-12-2020 13:57:42↵"

Here is my flow for this:

```auto
 [{"id":"c7e544f6.db5568","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"f5ed2ce5.4fe4a","type":"debug","z":"c7e544f6.db5568","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":950,"y":320,"wires":[]},{"id":"aa2f533d.fce7d","type":"hs-device","z":"c7e544f6.db5568","name":"Regenmeter cumulatief","device":"436","server":"ab4d84fb.9a1c88","feature":"438","reportonstartup":true,"x":170,"y":200,"wires":[["e769b1d6.eb9c8"]]},{"id":"56ba4997.ce12a8","type":"change","z":"c7e544f6.db5568","name":"Now","rules":[{"t":"set","p":"payload","pt":"msg","to":"$now('[D01]-[M01]-[Y0001] [H01]:[m01]:[s01]', '+0100')","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":350,"y":120,"wires":[["8751ac75.7d2ac"]]},{"id":"5aba7d4d.0378f4","type":"inject","z":"c7e544f6.db5568","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"05 00 * * *","once":false,"onceDelay":0.1,"topic":"current","payload":"","payloadType":"date","x":170,"y":120,"wires":[["56ba4997.ce12a8"]]},{"id":"e769b1d6.eb9c8","type":"function","z":"c7e544f6.db5568","name":"topic cum","func":"//read payload\n\nvar stringValue = msg.payload.value;\n\n//convert string to float to 3 decimal places\nvar floatValue = parseFloat(stringValue).toFixed(3);\n\n//set and return payload\nmsg.topic = \"cum\"\nmsg.payload = floatValue;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":500,"y":200,"wires":[["389b42b2.0e430e"]]},{"id":"8751ac75.7d2ac","type":"function","z":"c7e544f6.db5568","name":"topic tijd","func":"//read payload\n\nvar stringValue = msg.payload;\n\n//set and return payload\nmsg.topic = \"tijd\";\nmsg.payload = stringValue;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":500,"y":120,"wires":[["389b42b2.0e430e"]]},{"id":"389b42b2.0e430e","type":"join","z":"c7e544f6.db5568","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"3","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":690,"y":200,"wires":[["27a9715.7c4038e"]]},{"id":"6cb883a7.77ac8c","type":"file","z":"c7e544f6.db5568","name":"to file","filename":"/home/pi/Metingen/rain.txt","appendNewline":false,"createDir":false,"overwriteFile":"false","encoding":"none","x":1010,"y":200,"wires":[[]]},{"id":"9c966191.ce7eb","type":"hs-device","z":"c7e544f6.db5568","name":"Regenmeter dagelijks","device":"300","server":"ab4d84fb.9a1c88","feature":0,"reportonstartup":true,"x":160,"y":280,"wires":[["14d3efca.e617a"]]},{"id":"14d3efca.e617a","type":"function","z":"c7e544f6.db5568","name":"topic daily","func":"//read payload\n\nvar stringValue = msg.payload.value;\n\n//convert string to float to 1 decimal places\nvar floatValue = parseFloat(stringValue).toFixed(1);\n\n//set and return payload\nmsg.topic = \"daily\"\nmsg.payload = floatValue;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":500,"y":280,"wires":[["389b42b2.0e430e"]]},{"id":"27a9715.7c4038e","type":"csv","z":"c7e544f6.db5568","name":"","sep":",","hdrin":"","hdrout":"none","multi":"one","ret":"\\n","temp":"","skip":"0","strings":true,"include_empty_strings":"","include_null_values":"","x":850,"y":200,"wires":[["f5ed2ce5.4fe4a","6cb883a7.77ac8c"]]},{"id":"ab4d84fb.9a1c88","type":"hs-server","name":"RPi4-HS4","host":"192.168.1.20","port":"8080"}]

```

Is there a way to change the order?  
Thanks for any hint.  
-- Cor --

---

<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: [30 December 2020 14:00 UTC](https://discourse.nodered.org/t/how-to-change-the-order-of-csv-values-written-to-a-file/38286/2 "2020-12-30T14:00:31Z")

</div>

Add a change node after the join to reorder the items

 ![Screen Shot 2020-12-30 at 8.59.42 AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/f/ff34cc65a366c8f5d3e799aa55676519b66538f2.png)

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [30 December 2020 14:04 UTC](https://discourse.nodered.org/t/how-to-change-the-order-of-csv-values-written-to-a-file/38286/3 "2020-12-30T14:04:17Z")

</div>

or use a function node instead and set order.

```auto
[{"id":"9af07b82.dfe52","type":"join","z":"a67a8284.324fe","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"3","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":690,"y":200,"wires":[["b92f7ed8.fd5d9","6b25aced.283cbc"]]},{"id":"3e38352e.d3c6aa","type":"function","z":"a67a8284.324fe","name":"topic cum","func":"//read payload\n\nvar stringValue = msg.payload.value;\n\n//convert string to float to 3 decimal places\nvar floatValue = parseFloat(stringValue).toFixed(3);\n\n//set and return payload\nmsg.topic = \"cum\"\nmsg.payload = floatValue;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":500,"y":200,"wires":[["9af07b82.dfe52"]]},{"id":"2f649a7d.cc9ef6","type":"function","z":"a67a8284.324fe","name":"topic tijd","func":"//read payload\n\nvar stringValue = msg.payload;\n\n//set and return payload\nmsg.topic = \"tijd\";\nmsg.payload = stringValue;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":500,"y":120,"wires":[["9af07b82.dfe52"]]},{"id":"7568f758.3545a","type":"function","z":"a67a8284.324fe","name":"topic daily","func":"//read payload\n\nvar stringValue = msg.payload.value;\n\n//convert string to float to 1 decimal places\nvar floatValue = parseFloat(stringValue).toFixed(1);\n\n//set and return payload\nmsg.topic = \"daily\"\nmsg.payload = floatValue;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":500,"y":280,"wires":[["9af07b82.dfe52"]]},{"id":"b92f7ed8.fd5d9","type":"debug","z":"a67a8284.324fe","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":950,"y":320,"wires":[]},{"id":"6b25aced.283cbc","type":"function","z":"a67a8284.324fe","name":"","func":"msg.payload = msg.payload.tijd+\",\"+msg.payload.cum+\",\"+msg.payload.daily+\"\\n\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":840,"y":200,"wires":[["b92f7ed8.fd5d9"]]},{"id":"eede7c19.974728","type":"inject","z":"a67a8284.324fe","name":"","props":[{"p":"payload.value","v":"7.345","vt":"num"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":330,"y":200,"wires":[["3e38352e.d3c6aa"]]},{"id":"d599ced1.16f598","type":"change","z":"a67a8284.324fe","name":"Now","rules":[{"t":"set","p":"payload","pt":"msg","to":"$now('[D01]-[M01]-[Y0001] [H01]:[m01]:[s01]', '+0100')","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":350,"y":120,"wires":[["2f649a7d.cc9ef6"]]},{"id":"bed662f4.bdf738","type":"inject","z":"a67a8284.324fe","name":"","props":[{"p":"payload.value","v":"0","vt":"num"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":330,"y":260,"wires":[["7568f758.3545a"]]},{"id":"8dda4d07.fd9d1","type":"inject","z":"a67a8284.324fe","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"current","payload":"","payloadType":"date","x":160,"y":120,"wires":[["d599ced1.16f598"]]}]

```

---

<div class="post-metadata">

### Author: ![cope](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cope/32/34515_2.png) [@cope](https://discourse.nodered.org/u/cope)
#### Post date: [30 December 2020 14:27 UTC](https://discourse.nodered.org/t/how-to-change-the-order-of-csv-values-written-to-a-file/38286/4 "2020-12-30T14:27:03Z")

</div>

Thank you both for your quick reaction!  
It works fine now.

---

<div class="post-metadata">

### Author: ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)
#### Post date: [30 December 2020 14:57 UTC](https://discourse.nodered.org/t/how-to-change-the-order-of-csv-values-written-to-a-file/38286/5 "2020-12-30T14:57:11Z")

</div>

I think its as simple as defining the order of your comma seperated values  
in the **Columns setting** of the CSV node.

**Example:**  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/5/e5f24199af665b78396272aa410ce0cefbbf872f.png)

---

<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: [28 February 2021 14:57 UTC](https://discourse.nodered.org/t/how-to-change-the-order-of-csv-values-written-to-a-file/38286/6 "2021-02-28T14:57:20Z")

</div>

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