Show a simple text file as a list with dashboard 'template'

A simple task for NR/ui_template, but the solution is not obvious to me!?
A text file is to be read and displayed as a list on a dashboard with ui_template.
The list is stored on a RPI and is read by NR ('read file') and converted to an object with '(msg.payload).split("\n")'.
However, in the subsequent 'template' with ng-repeat, the list is not generated as expected!?

Here is the flow:
flow_testfile
flow_testfile.png.json (6.6 KB)

The main code part is this: (in 'Function List testing')

let msg1 = null;   //  INI1 part:  test string 
let ini1 = ["Zeile 1a", "Zeile 2, 11, 22, 33", "Zeile 3", "Zeile 4, 111, 222, 333, 444", "Zeile 5", "Zeile 6", "Zeile 7"];

let ptype1 = [typeof ini1] 
msg1 = {
        payload: ini1
}


let msg2 = null;   // INI 2 part:  handling the test.txt file / object
let ini2 = (msg.payload).split("\n");
let ptype2 = [typeof ini2] 
msg2 = {
        payload: ini2
}

let msg3 = null;      // just check the type together with test text  
msg3 = {
        payload: [ini2, ptype2]
}

return [msg1, msg2, msg3];

The generated screen:

And Debug Listings with //comments:

  node: debug test.txt    //  read test file
msg.payload : string[118]
"Zeile 1↵ Zeile 2, 11, 22, 33↵ Zeile 3↵ Zeile 4, 111, 222, 333, 444↵ Zeile 5, 5, 55, 555, 5555↵Zeile 6, 66↵Zeile 7, 7↵↵"

  node: debug fct 1       // an example string handled in a separate template  
msg.payload : array[7]
[ "Zeile 1a", "Zeile 2, 11, 22, 33", "Zeile 3", "Zeile 4, 111, 222, 333, 444", "Zeile 5", "Zeile 6", "Zeile 7" ]

  node: debug fct 2       // test file converted to object
msg.payload : array[9]
[ "Zeile 1", " Zeile 2, 11, 22, 33", " Zeile 3", " Zeile 4, 111, 222, 333, 444", " Zeile 5, 5, 55, 555, 5555", "Zeile 6, 66", "Zeile 7, 7", "", "" ]

  node: debug ini 1       // example string converted to object
msg.payload : array[7]
[ "Zeile 1a", "Zeile 2, 11, 22, 33", "Zeile 3", "Zeile 4, 111, 222, 333, 444", "Zeile 5", "Zeile 6", "Zeile 7" ]

  node: debug ini 2       // output/passthru of 'template'
msg.payload : array[9]
[ "Zeile 1", " Zeile 2, 11, 22, 33", " Zeile 3", " Zeile 4, 111, 222, 333, 444", " Zeile 5, 5, 55, 555, 5555", "Zeile 6, 66", "Zeile 7, 7", "", "" ]

Who can shed some light on this? Any help / explanation is greatly appreciated

So it looks like the text data coming in has a bunch of "↵" in it. You need to convert them to separate lines and remove the last two lines.

Try this as the function:

// 2023-07-15_11

let msg1 = null;
let ini1 = ["Zeile 1a", "Zeile 2, 11, 22, 33", "Zeile 3", "Zeile 4, 111, 222, 333, 444", "Zeile 5", "Zeile 6", "Zeile 7"];

let ptype1 = [typeof ini1] 
msg1 = {
        payload: ini1
}


let msg2 = null;
node.warn(msg.payload)
let m = msg.payload
m = m.replace("↵↵","")
let ini2 = m.split("↵");
node.warn(ini2)
let ptype2 = [typeof ini2] 
node.warn(ptype2)

msg2 = {
        payload: ini2
}

let msg3 = null;
msg3 = {
        payload: [ini2, ptype2]
}

return [msg1, msg2, msg3];

The file I use is basically the same "string" as with the one node (see last array[9] above).
But here is a dump on the RPI/ssh:

~/schedulePlus/data $ cat test.txt
Zeile 1
 Zeile 2, 11, 22, 33
 Zeile 3
 Zeile 4, 111, 222, 333, 444
 Zeile 5, 5, 55, 555, 5555
Zeile 6, 66
Zeile 7, 7

If I change

to this:

msg2 = {
        payload: [ini2]
}

for the "Listing INI 2" it generates one line with

* ["Zeile 1"," Zeile 2, 11, 22, 33"," Zeile 3"," Zeile 4, 111, 222, 333, 444"," Zeile 5, 5, 55, 555, 5555","Zeile 6, 66","Zeile 7, 7","",""]

but it's not resolving like with the first "test.txt" string in "Listing INI 1"

See my prior msg since I updated it while you responded.

GREAT catch!
I was expecting it to be a simple problem, but finding that .... phew
THANKS!

Well, it is not quite solved yet.
If the read file "test.txt" contains a blank line, the code gets swallowed.
In the example here I have in the file after the "line 3" a blank line.
In the output "Listing INI 2" nothing is shown, but "Listing INI 3" are line 3 and line 4 as one element of the object.


Each line should be processed and also appear in the output as a separate line. Empty line is still debatable.

Hmmm, it worked for me but that's probably because I put the data in an inject node. Try changing this line
m = m.replace("↵↵","")
to
m = m.replace("↵↵","\n")

How is the file created in the first place?

For the moment ...
The problem seems to be with empty line(s).
If only one line a sort of replace could work, but had problem with multiple lines.
Will come back with more details later ..

After some testing it is clear that the discussed problem is not causally related to empty lines in the imported file.
From what I can see, the problem is how ng-repeat deals with an array where elements are null. The 'null' elements are caused by the .split("\n") of the imported file at the locations of the blank lines. ng-repeat simply aborts processing without a error message. At least I can't find any message in the logs.
For my use case ng-repeat is probably not suitable. However the missing log is a bug ... for me!

Any further help / solution to process a file with "empty" lines as a complete array will be greatly appreciated.

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