# Save a daily csv file of sensor data

**URL:** <https://discourse.nodered.org/t/save-a-daily-csv-file-of-sensor-data/19562>\
**Category:** General\
**Created:** [24 December 2019 22:28 UTC](https://discourse.nodered.org/t/save-a-daily-csv-file-of-sensor-data/19562 "2019-12-24T22:28:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![YairHach](https://avatars.discourse-cdn.com/v4/letter/y/6bbea6/32.png) [@YairHach](https://discourse.nodered.org/u/YairHach)\
**Post date:** [24 December 2019 22:28 UTC](https://discourse.nodered.org/t/save-a-daily-csv-file-of-sensor-data/19562/1 "2019-12-24T22:28:14Z")

</div>

Hey,

I connected 2 xiaomi temp sensors to my Node-RED.

They send a msg every 30 min, i want to save the data to a csv file and "close" the file every day.

Every timestamp with data get a row in the csv with the same headers.

I'm pretty new to Node-RED but i think it should be easy task

Thanks, Yair

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [25 December 2019 07:38 UTC](https://discourse.nodered.org/t/save-a-daily-csv-file-of-sensor-data/19562/2 "2019-12-25T07:38:35Z")

</div>

Do this in small steps, first concentrate on fetching the data, once you have achieved that then move on to the next phase.

---

<div class="post-metadata">

**Author:** ![YairHach](https://avatars.discourse-cdn.com/v4/letter/y/6bbea6/32.png) [@YairHach](https://discourse.nodered.org/u/YairHach)\
**Post date:** [25 December 2019 08:02 UTC](https://discourse.nodered.org/t/save-a-daily-csv-file-of-sensor-data/19562/3 "2019-12-25T08:02:44Z")

</div>

Thank you for the answer,

I already fetching the data using json and csv nodes.

I have the data in csv format  
Now I want to create every day file that include the date and year on the file name and append every msg from the sensor to the file

And at the end of the day to send the file

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [25 December 2019 08:39 UTC](https://discourse.nodered.org/t/save-a-daily-csv-file-of-sensor-data/19562/4 "2019-12-25T08:39:00Z")

</div>

OK, again then start with the first step. Work out how to use the File Out node to write the data to a file. Once you have that going then move on to the next phase.

---

<div class="post-metadata">

**Author:** ![Navybrandon](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/navybrandon/32/13021_2.png) [@Navybrandon](https://discourse.nodered.org/u/Navybrandon)\
**Post date:** [26 December 2019 19:33 UTC](https://discourse.nodered.org/t/save-a-daily-csv-file-of-sensor-data/19562/5 "2019-12-26T19:33:06Z")

</div>

Try the below.

```auto
[{"id":"50b1a1d1.ed892","type":"file","z":"4a8e69ed.f81b98","name":"Add Device file location","filename":"","appendNewline":true,"createDir":true,"overwriteFile":"true","encoding":"none","x":550,"y":80,"wires":[["bda46195.b38b1"]]},{"id":"c6679eea.8efd","type":"function","z":"4a8e69ed.f81b98","name":"Data Capture","func":"var d = new Date();\nvar t = d.getTime();\nvar n = d.getTimezoneOffset();\npayload={\"date/time\": d,\"Time Zone Diffrence in minutes\": n,\"IP\": msg.ip,\"Bacnet\": msg.Bacnet, \"Function\": msg.Function, \"type\": msg.ObjectID,\"App Tag\": msg.Apptag, \"Property ID\": msg.PropertyID, \"Write Value\": msg.Wvalue, \"payload\":msg.payload};\nmsg.payload=payload;\nreturn msg;","outputs":1,"noerr":0,"x":190,"y":80,"wires":[["eafe30f8.658b"]]},{"id":"eafe30f8.658b","type":"json","z":"4a8e69ed.f81b98","name":"","property":"payload","action":"","pretty":false,"x":350,"y":80,"wires":[["50b1a1d1.ed892"]]},{"id":"bda46195.b38b1","type":"debug","z":"4a8e69ed.f81b98","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":750,"y":80,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [26 December 2019 19:45 UTC](https://discourse.nodered.org/t/save-a-daily-csv-file-of-sensor-data/19562/6 "2019-12-26T19:45:32Z")

</div>

Hi @YairHach

My take on this is something like:

Use an `inject` node to _pulse_ an output everyday at a given time.  
The file name should be either a `flow` or `global` context variable so if ever you want to change it, you can easily.  
Though obviously you will need a different file name for each day. But that can be derived from the date.  
On reception of the pulse, the flow moves the file from its existing name to the new name which is unique.  
There are a few ways of doing that. You can use the `exec` node to do that or use part of th `fs-ops` set which have a lot of tools for that sort of stuff.
