Help with file reading and message formatting

I have files which are created every time the machine is booted.

With fs-ops node/s I get a list of said files:

["Powered on at 2021-02-01 212108.db","Powered on at 2021-02-02 073139.db","Powered on at 2021-02-02 114601.db","Powered on at 2021-02-02 165843.db","Powered on at 2021-02-02 171120.db","Powered on at 2021-02-02 172557.db","Powered on at 2021-02-03 122336.db","Powered on at 2021-02-04 064446.db","Powered on at 2021-02-04 124940.db","Powered on at 2021-02-04 190643.db","Powered on at 2021-02-05 065420.db","Powered on at 2021-02-05 115718.db","Powered on at 2021-02-05 185952.db","Powered on at 2021-02-05 215415.db","Powered on at 2021-02-05 222908.db","Powered on at 2021-02-06 063001.db"]

What I want to do is make a list (as in a list displayed on the screen) of these files with the format:
the date and time of the file name + the first line of the file.

As the list is contained in msg.payload[x] I am at a loss how to split it up and spit out all those as lines which than can then be put through a file in node to read the first line.

I'm sure after my posting this, I will have that ah ha! moment, but as many of you who know me: It won't happen until I post this.

But any help would be appreciated.

Update:

(I told you)

Ok, so now I have this as the message:
up 4 hours, 28 minutesLast alive 2021-02-06 10:57:59

Expanded it has a \n in it. Or so I think.

(See picture)

Screenshot from 2021-02-06 11-01-34

I tried the string node and strip right \n but that doesn't work.

Further update:

This is a working flow, but I'm just curious if there is a nicer/neater way I could do it for formatting the message/s.

Foreign nodes
fs-ops-dir
ring-buffer
(Not important in the workings.)

[{"id":"f6edc695.44c1a","type":"split","z":"8bb4de19.f72c88","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":2080,"y":1330,"wires":[["5affc1e5.4dd268"]]},{"id":"5affc1e5.4dd268","type":"change","z":"8bb4de19.f72c88","name":"set msg.filename","rules":[{"t":"move","p":"payload","pt":"msg","to":"filename","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":2165,"y":1330,"wires":[["9da1bb26.a6eb58"]],"l":false},{"id":"a72d260.586b358","type":"change","z":"8bb4de19.f72c88","name":"set msg.files","rules":[{"t":"move","p":"files","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1995,"y":1330,"wires":[["f6edc695.44c1a"]],"l":false},{"id":"9da1bb26.a6eb58","type":"template","z":"8bb4de19.f72c88","name":"","field":"filename","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"/home/me/.node-red/public/history/{{filename}}","output":"str","x":2260,"y":1330,"wires":[["33e7cf5b.473a98"]]},{"id":"5da9bf43.2ee768","type":"fs-ops-dir","z":"8bb4de19.f72c88","name":"List files","path":"/home/me/.node-red/public/history","pathType":"str","filter":"*","filterType":"str","dir":"files","dirType":"msg","x":1890,"y":1330,"wires":[["a72d260.586b358"]]},{"id":"33e7cf5b.473a98","type":"file in","z":"8bb4de19.f72c88","name":"Log","filename":"","format":"utf8","chunk":false,"sendError":false,"encoding":"none","x":1880,"y":1380,"wires":[["6830cf4c.36f878"]]},{"id":"a5dec011.09a168","type":"inject","z":"8bb4de19.f72c88","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1900,"y":1260,"wires":[["5da9bf43.2ee768"]]},{"id":"668363a0.da2484","type":"ui_button","z":"8bb4de19.f72c88","name":"Create history list","group":"8a97b9d.4e64d48","order":1,"width":"2","height":"1","passthru":false,"label":"Show","tooltip":"","color":"","bgcolor":"","icon":"","payload":"","payloadType":"str","topic":"","x":1920,"y":1210,"wires":[["5da9bf43.2ee768"]]},{"id":"6830cf4c.36f878","type":"string","z":"8bb4de19.f72c88","name":"filename","methods":[{"name":"getRightMost","params":[{"type":"str","value":"/"}]},{"name":"delRightMost","params":[{"type":"str","value":"."}]}],"prop":"filename","propout":"filename","object":"msg","objectout":"msg","x":2030,"y":1380,"wires":[["a0199d1e.23bd58"]]},{"id":"a0199d1e.23bd58","type":"string","z":"8bb4de19.f72c88","name":"","methods":[{"name":"delRightMost","params":[{"type":"str","value":"minutes"}]},{"name":"chompLeft","params":[{"type":"str","value":"up "}]}],"prop":"payload","propout":"payload","object":"msg","objectout":"msg","x":2170,"y":1380,"wires":[["d600953e.5b1e4"]]},{"id":"d600953e.5b1e4","type":"template","z":"8bb4de19.f72c88","name":"Structure","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{{filename}} for {{payload}} minutes","output":"str","x":2310,"y":1380,"wires":[["b355ebea.1d25a"]]},{"id":"b355ebea.1d25a","type":"ring-buffer","z":"8bb4de19.f72c88","name":"Cache","capacity":16,"order":"new-to-old","sendOnlyIfFull":false,"pushAfterClear":false,"extra":false,"perTopic":false,"x":2450,"y":1380,"wires":[["dfebd910.415f88"]]},{"id":"3ddb8752.20aff8","type":"change","z":"8bb4de19.f72c88","name":"Wipe","rules":[{"t":"set","p":"clear","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":2255,"y":1210,"wires":[["b355ebea.1d25a"]],"l":false},{"id":"dfebd910.415f88","type":"ui_template","z":"8bb4de19.f72c88","group":"8a97b9d.4e64d48","name":"Hisotry list","order":3,"width":"9","height":"8","format":"<table id=\"table\" border=\"1\">\n <tr>\n <th>History</th> \n </tr>\n <tbody>\n <tr ng-repeat=\"row in msg.payload track by $index\">\n <td class=\"text\" >{{row}}</td>\n </tr>\n </tbody>\n</table>\n","storeOutMessages":true,"fwdInMessages":true,"resendOnRefresh":true,"templateScope":"local","x":2630,"y":1380,"wires":[[]]},{"id":"d57a1888.e83428","type":"ui_button","z":"8bb4de19.f72c88","name":"Create history list","group":"8a97b9d.4e64d48","order":2,"width":"2","height":"1","passthru":false,"label":"Wipe","tooltip":"","color":"","bgcolor":"","icon":"","payload":"blah","payloadType":"str","topic":"","x":2130,"y":1210,"wires":[["3ddb8752.20aff8"]]},{"id":"8a97b9d.4e64d48","type":"ui_group","name":"Group 1","tab":"718742c7.3798cc","order":1,"disp":true,"width":"9","collapse":false},{"id":"718742c7.3798cc","type":"ui_tab","name":"Machine Usage","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

Is that a list of file names or the contents of a file, or what?

Colin, fs-ops is a set of nodes for the file system. one of them (dir) gives to an array of filenames/dir names in the specified directory. II find the fs-ops nodes to be pretty decent and well thought out for getting lists of files, getting file properties, copying, moving, etc.

@Trying_to_learn
Just a thought, left of field, instead of saving your logs as plain text then trying to parse them, you could use the flogger node to save JSON - then its pretty easy to pick out the parts you want.

Another possibility, write your logs to a database? or write to syslog?

1 Like

If they are “live” logs and this is just for display of one line then rather than go via the file why not save the current line in context at the same time it is written now to the log, so it can then just be grabbed.

1 Like

Yes, I know that, I just couldn't believe that the files are named as shown in the array. To give them names like that is just making life difficult.

Its done like that so you can send the array of file names to the next fs-ops node - e.g. to get permissions or properties for all files in a directory - it works really well TBH.

My bad - just re-read yours and Andrews posts - haha yes, those file names are somewhat :thinking: unusual :wink:

Flogger is the way to go @Trying_to_learn because it takes care of the housekeeping, for example it will rotate and compress your log file, preventing it from getting too large.
It also adds a timestamp automatically.

1 Like

I believe I have tried flogger and last time I tried, it didn't work.

Ok, it is working - only just - but good enough for what I want.

This is a screen grab of what I get:

Which shows me that the machine was booted at.... (first part of message) and was on for (second part of the message).

This kind of shows me how much time per day I am sitting here getting square eyes.

The filenames - which are a bit weird are created when the machine is booted.
It gets the last log file and renames it to a filename which shows now.
YYYY-MM-DD hhmmss
That file has two lines.
The first is how long it has been up and...... I forget what the second The second is a time stamp of when it was last up.

That is handy so I can see when it was last alive.

I was just wanting to tidy up the line but worked around it with the string nodes.
Though it would be nice to know how to strip the carriage return as shown in the first post and indicated by the red line.

But I stripped to the right of minutes and restored the word in the template node.
So I guess all is good at the end of the day.

I may try flogger again soon.

Maybe it wasn't it didn't work, but maybe something else stuck out as a problem for me.

I can't remember. I'll go back and see if I can remember.

Thanks though to all replies.

Update:

Ok, it was logger I had. It came with a mismatch of what it does to what it claims.

Got flogger now and shall try to start applying it to machines tomorrow.

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