A comma separated CSV file consists of only two columns (datetime, pressure) but many rows. Does anyone know whether it is possible to read only the x most recent measurements from that CSV file?
Any help is welcome!
A comma separated CSV file consists of only two columns (datetime, pressure) but many rows. Does anyone know whether it is possible to read only the x most recent measurements from that CSV file?
Any help is welcome!
Presuming the last 2 lines are the latest entries, then you would read the csv file, then output the last 2 array values.
e.g.
[{"id":"965ef7ad.16d87","type":"inject","z":"9b3f9f31.c45298","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":230,"y":800,"wires":[["d16bafd0.71ee88"]]},{"id":"d16bafd0.71ee88","type":"template","z":"9b3f9f31.c45298","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"date,pressure\n12/06/2020,45\n13/06/2020,47\n15/06/2020,50","output":"str","x":110,"y":860,"wires":[["21a21fc0.7d62"]]},{"id":"21a21fc0.7d62","type":"csv","z":"9b3f9f31.c45298","name":"","sep":",","hdrin":true,"hdrout":"none","multi":"mult","ret":"\\n","temp":"","skip":"0","strings":true,"include_empty_strings":"","include_null_values":"","x":260,"y":860,"wires":[["a82f5588.69ba6"]]},{"id":"a82f5588.69ba6","type":"change","z":"9b3f9f31.c45298","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload[[-2,-1]]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":460,"y":860,"wires":[["a4e8e83.9c24918"]]},{"id":"a4e8e83.9c24918","type":"debug","z":"9b3f9f31.c45298","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":650,"y":860,"wires":[]}]
Thanks for your help, this works well to get the last rows out of the CSV file.
But now I see I was not clear with my question, sorry. It is not the intention to put those last rows in an array, I want to generate a new (different) CSV file with it. Is that also possible?
Then feed the array back through a cvs node and it will turn the 2 element array back into a csv.
e.g.
[{"id":"cc9edeb9.ff689","type":"inject","z":"c74669a0.6a34f8","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":330,"y":400,"wires":[["70ac867e.3b19d"]]},{"id":"70ac867e.3b19d","type":"template","z":"c74669a0.6a34f8","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"date,pressure\n12/06/2020,45\n13/06/2020,47\n15/06/2020,50","output":"str","x":210,"y":460,"wires":[["892ca195.f46cb"]]},{"id":"892ca195.f46cb","type":"csv","z":"c74669a0.6a34f8","name":"","sep":",","hdrin":true,"hdrout":"none","multi":"mult","ret":"\\n","temp":"","skip":"0","strings":true,"include_empty_strings":"","include_null_values":"","x":360,"y":460,"wires":[["dab29fbd.3cc03"]]},{"id":"dab29fbd.3cc03","type":"change","z":"c74669a0.6a34f8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload[[-2,-1]]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":560,"y":460,"wires":[["9d223cc0.c2a008"]]},{"id":"9d223cc0.c2a008","type":"csv","z":"c74669a0.6a34f8","name":"","sep":",","hdrin":true,"hdrout":"all","multi":"mult","ret":"\\n","temp":"","skip":"0","strings":true,"include_empty_strings":"","include_null_values":"","x":670,"y":500,"wires":[["14ddc802.a51cb8"]]},{"id":"14ddc802.a51cb8","type":"debug","z":"c74669a0.6a34f8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":770,"y":460,"wires":[]}]
Exactly what I meant and it works well thanks for your help! It is a pleasure to work with Node-RED because there are always people on this forum who want to help others
Hi E1cid. Unfortunately, things are not going well yet, maybe this is due to the somewhat strange datetime format in my CSV file. That is generated externally and I cannot change it. I have now put that CSV file in the template (see flow), can you see again how I can save the last 20 rows in another CSV file?
[{"id":"c074fcd7.0f257","type":"template","z":"f88f5df4.1e07f","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"datetime,pressure\n17-05-2021 08:29,998\n17-05-2021 08:45,999\n17-05-2021 09:01,1000\n17-05-2021 09:15,1000\n17-05-2021 09:30,1001\n17-05-2021 09:45,1001\n17-05-2021 10:00,1001\n17-05-2021 10:15,1001\n17-05-2021 10:30,1001\n17-05-2021 10:45,1001\n17-05-2021 11:00,1001\n17-05-2021 11:15,1001\n17-05-2021 11:30,1001\n17-05-2021 11:45,1001\n17-05-2021 12:00,1001\n17-05-2021 12:09,1001\n17-05-2021 12:10,1001\n17-05-2021 12:12,1001\n17-05-2021 12:31,1001\n17-05-2021 12:46,1001\n17-05-2021 13:16,1002\n17-05-2021 13:31,1002\n17-05-2021 13:46,1002\n17-05-2021 14:01,1002\n17-05-2021 14:15,1001\n17-05-2021 14:21,1003\n17-05-2021 14:36,1003\n17-05-2021 14:50,1002\n17-05-2021 15:06,1003\n17-05-2021 15:21,1004\n\n","output":"str","x":240,"y":240,"wires":[["2527816e.78f46e"]]}]
You neede to change the jsonata expression in the change node
[[-2,-1]] should be [[-20..-1]] the .. denotes a range.
[{"id":"cc9edeb9.ff689","type":"inject","z":"c74669a0.6a34f8","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":390,"y":340,"wires":[["c074fcd7.0f257"]]},{"id":"c074fcd7.0f257","type":"template","z":"c74669a0.6a34f8","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"datetime,pressure\n17-05-2021 08:29,998\n17-05-2021 08:45,999\n17-05-2021 09:01,1000\n17-05-2021 09:15,1000\n17-05-2021 09:30,1001\n17-05-2021 09:45,1001\n17-05-2021 10:00,1001\n17-05-2021 10:15,1001\n17-05-2021 10:30,1001\n17-05-2021 10:45,1001\n17-05-2021 11:00,1001\n17-05-2021 11:15,1001\n17-05-2021 11:30,1001\n17-05-2021 11:45,1001\n17-05-2021 12:00,1001\n17-05-2021 12:09,1001\n17-05-2021 12:10,1001\n17-05-2021 12:12,1001\n17-05-2021 12:31,1001\n17-05-2021 12:46,1001\n17-05-2021 13:16,1002\n17-05-2021 13:31,1002\n17-05-2021 13:46,1002\n17-05-2021 14:01,1002\n17-05-2021 14:15,1001\n17-05-2021 14:21,1003\n17-05-2021 14:36,1003\n17-05-2021 14:50,1002\n17-05-2021 15:06,1003\n17-05-2021 15:21,1004\n\n","output":"str","x":210,"y":460,"wires":[["892ca195.f46cb"]]},{"id":"892ca195.f46cb","type":"csv","z":"c74669a0.6a34f8","name":"","sep":",","hdrin":true,"hdrout":"none","multi":"mult","ret":"\\n","temp":"","skip":"0","strings":true,"include_empty_strings":"","include_null_values":"","x":420,"y":400,"wires":[["dab29fbd.3cc03"]]},{"id":"dab29fbd.3cc03","type":"change","z":"c74669a0.6a34f8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload[[-20..-1]]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":620,"y":400,"wires":[["9d223cc0.c2a008"]]},{"id":"9d223cc0.c2a008","type":"csv","z":"c74669a0.6a34f8","name":"","sep":",","hdrin":true,"hdrout":"all","multi":"mult","ret":"\\n","temp":"","skip":"0","strings":true,"include_empty_strings":"","include_null_values":"","x":730,"y":440,"wires":[["14ddc802.a51cb8"]]},{"id":"14ddc802.a51cb8","type":"debug","z":"c74669a0.6a34f8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":830,"y":400,"wires":[]}]
The two dots make the difference, I have been helped fantastic again. Thanks!
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.