Reading CSV and make a history

Good evening lads,

finding and reading the right manuels to some topic is horrible. Even if you dont know how to declare it to google.

So, im tryin here, after google isnt giving me answers.

I can create and write a .csv file for my deployment sensor. It works fine. Now i wont to show me the 5 latest entries in a history table. Therefor i have to put them in an array. Is theire any option to do that? Reading the .csv and show me the five latest?

Thanks for all help.

Hi ..

you can read the csv with the Read File node ( a msg per line)

image

use the Join node to make the individual msgs into a single array

Use JS to slice the last 5 elements from the array

Test Flow :

[{"id":"c28fc7517ecaa404","type":"file in","z":"54efb553244c241f","name":"","filename":"c:\\Share\\test.txt","filenameType":"str","format":"lines","chunk":false,"sendError":false,"encoding":"none","allProps":false,"x":380,"y":1760,"wires":[["922118e93d68597f"]]},{"id":"922118e93d68597f","type":"join","z":"54efb553244c241f","name":"","mode":"custom","build":"array","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"num","reduceFixup":"","x":570,"y":1760,"wires":[["2b67c91d7f017942"]]},{"id":"b2e4905caef8626e","type":"debug","z":"54efb553244c241f","name":"debug 23","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":880,"y":1760,"wires":[]},{"id":"6531d35ffed78268","type":"inject","z":"54efb553244c241f","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":200,"y":1760,"wires":[["c28fc7517ecaa404"]]},{"id":"2b67c91d7f017942","type":"function","z":"54efb553244c241f","name":"Slice 5","func":"msg.payload = msg.payload.slice(-5)  // slice 5 last\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":710,"y":1760,"wires":[["b2e4905caef8626e"]]}]

However, if that is something you want to do you would probably be better using a database rather than a CSV file.

Ah, nice. Thats the exact thing i looked for. Thanks alot.

The Files arent that big, so i can store it in a .csv

Just for info.
You can do this just with the file in and csv nodes, both set to a single message array.
You can select the entries required using the columns input field, and give the properties names.
e.g.

[{"id":"6531d35ffed78268","type":"inject","z":"9a86d59e88442f2c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":350,"y":400,"wires":[["da28d0ba.59f458"]]},{"id":"da28d0ba.59f458","type":"template","z":"9a86d59e88442f2c","name":"simulate file in","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"1,2,3,4,5,6,7,8,9,0\nq,w,e,r,t,y,u,i,o,p\na,s,d,f,g,h,j,k,l,z\nx,c,v,b,n,m,<,>,?,@\n","output":"str","x":290,"y":460,"wires":[["13d6da58.add826"]]},{"id":"13d6da58.add826","type":"csv","z":"9a86d59e88442f2c","name":"Select required using columns","sep":",","hdrin":"","hdrout":"none","multi":"mult","ret":"\\n","temp":",,,,,a,b,c,d,e","skip":"0","strings":true,"include_empty_strings":"","include_null_values":"","x":590,"y":460,"wires":[["b2e4905caef8626e"]]},{"id":"b2e4905caef8626e","type":"debug","z":"9a86d59e88442f2c","name":"debug 23","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":730,"y":500,"wires":[]}]
1 Like

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