Formatting of my battery responses and display in the dashboard

Hello to all.

I try to format my battery response in a table format in the dashboard.
I have already succeeded but at the cost of a "lamentable" programming (see below the result, it looks nice but it's ugly on the programming side). Indeed I programmed in "hard coded" only for 4 batteries.

But there can be up to 16 pieces. So I want to redo my code so that it is adaptable and dynamic to the number of batteries.

To determine how many batteries there are, just find the lines where the word "Absent" is present :upside_down_face: and delete them from the array.
Then recover the new table without these useless lines and display them in the dashboard table

So in summary, can you help me to:

  • Eliminate the rows where the word "Absent" is present into the array
  • Split each row of the remaining table so that each part fits in its respective column by creating a JSON for the UI_TABLE node

Here is the debug of what I have as debug values to allow to reproduce my situation

Here is the array value of 16 lines, (the sign ↵ is automatically replaced by \r\r\n by doing a copy/paste)

["1     51384  18209  29500  28800  29600  3417   3437   Charge   Normal   Normal   Normal   92%      2022-08-11 20:49:03  Normal   Normal  28900    Normal  \r\r\n","2     51364  19623  29300  28700  29400  3418   3437   Charge   Normal   Normal   Normal   92%      2022-08-11 20:49:02  Normal   Normal  29100    Normal  \r\r\n","3     51412  15537  27100  27300  27800  3419   3447   Charge   Normal   Normal   Normal   89%      2022-08-11 20:49:01  Normal   Normal  27400    Normal  \r\r\n","4     -      -      -      -      -      -      -      Absent   -        -        -        -        -                    -        -       \r\r\n","5     -      -      -      -      -      -      -      Absent   -        -        -        -        -                    -        -       \r\r\n","6     -      -      -      -      -      -      -      Absent   -        -        -        -        -                    -        -       \r\r\n","7     -      -      -      -      -      -      -      Absent   -        -        -        -        -                    -        -       \r\r\n","8     -      -      -      -      -      -      -      Absent   -        -        -        -        -                    -        -       \r\r\n","9     -      -      -      -      -      -      -      Absent   -        -        -        -        -                    -        -       \r\r\n","10    -      -      -      -      -      -      -      Absent   -        -        -        -        -                    -        -       \r\r\n","11    -      -      -      -      -      -      -      Absent   -        -        -        -        -                    -        -       \r\r\n","12    -      -      -      -      -      -      -      Absent   -        -        -        -        -                    -        -       \r\r\n","13    -      -      -      -      -      -      -      Absent   -        -        -        -        -                    -        -       \r\r\n","14    -      -      -      -      -      -      -      Absent   -        -        -        -        -                    -        -       \r\r\n","15    -      -      -      -      -      -      -      Absent   -        -        -        -        -                    -        -       \r\r\n","16    -      -      -      -      -      -      -      Absent   -        -        -        -        -                    -        -       \r\n"]

Here is the value of one of the 16 line array:

1     51680  18089  29600  29000  29800  3429   3466   Charge   Normal   Normal   Normal   95%      2022-08-11 20:51:29  Normal   Normal  29100    Normal  


Bellow, is what I would like to achieve at the end, what is already working but at the cost of a calamitous and not evolving programming. I am not at all comfortable with the manipulation of arrays.

I hope you were able to follow my explanations

Is that source data tab delimited? If so, you should be able to use the CSV node to turn it into something useful.

This is what comes out of the serial port node. It is a series of "string" with CR & LF
I hope you can read it easily

11.08.2022 15:57:07node: Sourcemsg.payload : string[4]
"pwr↵"
11.08.2022 15:57:07node: Sourcemsg.payload : string[5]
"
@
↵"
11.08.2022 15:57:07node: Sourcemsg.payload : string[159]
"Power Volt   Curr   Tempr  Tlow   Thigh  Vlow   Vhigh  Base.St  Volt.St  Curr.St  Temp.St  Coulomb  Time                 B.V.St   B.T.St   MosTempr M.T.St  
↵"
11.08.2022 15:57:08node: Sourcemsg.payload : string[158]
"1     49898  9693   29800  28600  29700  3326   3328   Charge   Normal   Normal   Normal   98%      2022-08-11 22:13:38  Normal   Normal  28400    Normal  
↵"
11.08.2022 15:57:08node: Sourcemsg.payload : string[158]
"2     49889  9305   30000  28600  29500  3325   3327   Charge   Normal   Normal   Normal   98%      2022-08-11 22:13:36  Normal   Normal  28800    Normal  
↵"
11.08.2022 15:57:08node: Sourcemsg.payload : string[158]
"3     49899  10524  27900  26900  27700  3326   3328   Charge   Normal   Normal   Normal   98%      2022-08-11 22:13:36  Normal   Normal  27400    Normal  
↵"
11.08.2022 15:57:08node: Sourcemsg.payload : string[141]
"4     -      -      -      -      -      -      -      Absent   -        -        -        -        -                    -        -       
↵"
11.08.2022 15:57:08node: Sourcemsg.payload : string[141]
"5     -      -      -      -      -      -      -      Absent   -        -        -        -        -                    -        -       
↵"
11.08.2022 15:57:08node: Sourcemsg.payload : string[141]
"6     -      -      -      -      -      -      -      Absent   -        -        -        -        -                    -        -       
↵"
11.08.2022 15:57:08node: Sourcemsg.payload : string[141]
"7     -      -      -      -      -      -      -      Absent   -        -        -        -        -                    -        -       
↵"
11.08.2022 15:57:08node: Sourcemsg.payload : string[141]
"8     -      -      -      -      -      -      -      Absent   -        -        -        -        -                    -        -       
↵"
11.08.2022 15:57:08node: Sourcemsg.payload : string[141]
"9     -      -      -      -      -      -      -      Absent   -        -        -        -        -                    -        -       
↵"
11.08.2022 15:57:08node: Sourcemsg.payload : string[141]
"10    -      -      -      -      -      -      -      Absent   -        -        -        -        -                    -        -       
↵"
11.08.2022 15:57:08node: Sourcemsg.payload : string[141]
"11    -      -      -      -      -      -      -      Absent   -        -        -        -        -                    -        -       
↵"
11.08.2022 15:57:08node: Sourcemsg.payload : string[141]
"12    -      -      -      -      -      -      -      Absent   -        -        -        -        -                    -        -       
↵"
11.08.2022 15:57:08node: Sourcemsg.payload : string[141]
"13    -      -      -      -      -      -      -      Absent   -        -        -        -        -                    -        -       
↵"
11.08.2022 15:57:08node: Sourcemsg.payload : string[141]
"14    -      -      -      -      -      -      -      Absent   -        -        -        -        -                    -        -       
↵"
11.08.2022 15:57:08node: Sourcemsg.payload : string[141]
"15    -      -      -      -      -      -      -      Absent   -        -        -        -        -                    -        -       
↵"
11.08.2022 15:57:08node: Sourcemsg.payload : string[140]
"16    -      -      -      -      -      -      -      Absent   -        -        -        -        -                    -        -       ↵"
11.08.2022 15:57:08node: Sourcemsg.payload : string[33]
"
Command completed successfully↵"
11.08.2022 15:57:08node: Source
msg.payload : string[5]
"
$$↵"

Looks like a fixed column width with space delimeters.

This isn't quite right as you will see because there are still too many spaces so the columns are thrown out of alignment but it may give you enough clues. Probably better ways to do it but this was my quick attempt and I need to help my wife with dinner now. :slight_smile:

[{"id":"4aafb27f932d8b2d","type":"inject","z":"4a3fcf357a20bf56","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[\"Power Volt   Curr   Tempr  Tlow   Thigh  Vlow   Vhigh  Base.St  Volt.St  Curr.St  Temp.St  Coulomb  Time                 B.V.St   B.T.St   MosTempr M.T.St  \\r\\r\\n\",\"1     51384  18209  29500  28800  29600  3417   3437   Charge   Normal   Normal   Normal   92%      2022-08-11 20:49:03  Normal   Normal  28900    Normal  \\r\\r\\n\",\"2     51364  19623  29300  28700  29400  3418   3437   Charge   Normal   Normal   Normal   92%      2022-08-11 20:49:02  Normal   Normal  29100    Normal  \\r\\r\\n\",\"3     51412  15537  27100  27300  27800  3419   3447   Charge   Normal   Normal   Normal   89%      2022-08-11 20:49:01  Normal   Normal  27400    Normal  \\r\\r\\n\",\"4     -      -      -      -      -      -      -      Absent   -        -        -        -        -                    -        -       \\r\\r\\n\",\"5     -      -      -      -      -      -      -      Absent   -        -        -        -        -                    -        -       \\r\\r\\n\",\"6     -      -      -      -      -      -      -      Absent   -        -        -        -        -                    -        -       \\r\\r\\n\",\"7     -      -      -      -      -      -      -      Absent   -        -        -        -        -                    -        -       \\r\\r\\n\",\"8     -      -      -      -      -      -      -      Absent   -        -        -        -        -                    -        -       \\r\\r\\n\",\"9     -      -      -      -      -      -      -      Absent   -        -        -        -        -                    -        -       \\r\\r\\n\",\"10    -      -      -      -      -      -      -      Absent   -        -        -        -        -                    -        -       \\r\\r\\n\",\"11    -      -      -      -      -      -      -      Absent   -        -        -        -        -                    -        -       \\r\\r\\n\",\"12    -      -      -      -      -      -      -      Absent   -        -        -        -        -                    -        -       \\r\\r\\n\",\"13    -      -      -      -      -      -      -      Absent   -        -        -        -        -                    -        -       \\r\\r\\n\",\"14    -      -      -      -      -      -      -      Absent   -        -        -        -        -                    -        -       \\r\\r\\n\",\"15    -      -      -      -      -      -      -      Absent   -        -        -        -        -                    -        -       \\r\\r\\n\",\"16    -      -      -      -      -      -      -      Absent   -        -        -        -        -                    -        -       \\r\\n\"]","payloadType":"json","x":210,"y":980,"wires":[["f2702d45592440c6"]]},{"id":"b6bf82d7ec8df45c","type":"debug","z":"4a3fcf357a20bf56","name":"debug 5","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":580,"y":980,"wires":[]},{"id":"f2702d45592440c6","type":"function","z":"4a3fcf357a20bf56","name":"function 6","func":"let cols = []\nconst out = []\n\nmsg.payload.forEach( (line,i) => {\n    if ( i === 0 ) {\n        cols = line.split(' ')\n    } else {\n\n        const lc = line.split(' ')\n        const lineOut = {}\n\n        lc.forEach( (col, j) => {\n            let colName = cols[j]\n            lineOut[colName] = col\n        })\n        out.push(lineOut)\n\n    }\n})\n\nreturn {payload: out}","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":400,"y":980,"wires":[["b6bf82d7ec8df45c"]]}]
let cols = []
const out = []

msg.payload.forEach( (line,i) => {
    if ( i === 0 ) {
        cols = line.split(' ')
    } else {

        const lc = line.split(' ')
        const lineOut = {}

        lc.forEach( (col, j) => {
            let colName = cols[j]
            lineOut[colName] = col
        })
        out.push(lineOut)

    }
})

return {payload: out}
1 Like

As we say in our country, "a hungry stomach has no ears".
I hope the English translation will be understandable.
Yes I am trying to figure out the nested loops as well as the column breakdown. But I have to be honest I haven't understood everything yet. I must be too hungry to understand.
Thanks a lot for the code, this one doesn't work yet. I will also have to check which line contains the word "absent" to delete it. I have a feeling I'll spend a few days there with my limited programming skills.
But I'm going to stick with it.
Here's what I tried before I went to lunch :face_with_hand_over_mouth:
It lacks visually, the voltage and the word "absent" to then be able to eliminate it.



Here is another solution that may work for you

[{"id":"3340d0ba.b2a6d","type":"change","z":"30af2d3e.d94ea2","name":"","rules":[{"t":"change","p":"payload","pt":"msg","from":"\\s+","fromt":"re","to":" ","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":580,"y":60,"wires":[["aafee4d5.975418"]]},{"id":"e0c7428b.a27508","type":"split","z":"30af2d3e.d94ea2","name":"simulate incoming data","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":310,"y":60,"wires":[["3340d0ba.b2a6d"]]},{"id":"aafee4d5.975418","type":"join","z":"30af2d3e.d94ea2","name":"","mode":"custom","build":"string","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"16","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":150,"y":120,"wires":[["b1b1856c.cf05c8","70669b60.a16f54"]]},{"id":"d31b1878.08bf48","type":"inject","z":"30af2d3e.d94ea2","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[\"1     51384  18209  29500  28800  29600  3417   3437   Charge   Normal   Normal   Normal   92%      2022-08-11 20:49:03  Normal   Normal  28900    Normal  \\r\\r\\n\",\"2     51364  19623  29300  28700  29400  3418   3437   Charge   Normal   Normal   Normal   92%      2022-08-11 20:49:02  Normal   Normal  29100    Normal  \\r\\r\\n\",\"3     51412  15537  27100  27300  27800  3419   3447   Charge   Normal   Normal   Normal   89%      2022-08-11 20:49:01  Normal   Normal  27400    Normal  \\r\\r\\n\",\"4     -      -      -      -      -      -      -      Absent   -        -        -        -        -                    -        -       \\r\\r\\n\",\"5     -      -      -      -      -      -      -      Absent   -        -        -        -        -                    -        -       \\r\\r\\n\",\"6     -      -      -      -      -      -      -      Absent   -        -        -        -        -                    -        -       \\r\\r\\n\",\"7     -      -      -      -      -      -      -      Absent   -        -        -        -        -                    -        -       \\r\\r\\n\",\"8     -      -      -      -      -      -      -      Absent   -        -        -        -        -                    -        -       \\r\\r\\n\",\"9     -      -      -      -      -      -      -      Absent   -        -        -        -        -                    -        -       \\r\\r\\n\",\"10    -      -      -      -      -      -      -      Absent   -        -        -        -        -                    -        -       \\r\\r\\n\",\"11    -      -      -      -      -      -      -      Absent   -        -        -        -        -                    -        -       \\r\\r\\n\",\"12    -      -      -      -      -      -      -      Absent   -        -        -        -        -                    -        -       \\r\\r\\n\",\"13    -      -      -      -      -      -      -      Absent   -        -        -        -        -                    -        -       \\r\\r\\n\",\"14    -      -      -      -      -      -      -      Absent   -        -        -        -        -                    -        -       \\r\\r\\n\",\"15    -      -      -      -      -      -      -      Absent   -        -        -        -        -                    -        -       \\r\\r\\n\",\"16    -      -      -      -      -      -      -      Absent   -        -        -        -        -                    -        -       \\r\\n\"]","payloadType":"json","x":110,"y":60,"wires":[["e0c7428b.a27508"]]},{"id":"b1b1856c.cf05c8","type":"csv","z":"30af2d3e.d94ea2","name":"","sep":" ","hdrin":"","hdrout":"none","multi":"mult","ret":"\\n","temp":"Power,Volt,Curr,Tempr,Tlow,Thigh,Vlow,Vhigh,Baset,SVolt,SCurr,Temp,Coulomb,Time,B_V,B_T,MosTempr,M_T,ttt","skip":"0","strings":true,"include_empty_strings":"","include_null_values":"","x":270,"y":120,"wires":[["b081ddb9.d645f"]]},{"id":"70669b60.a16f54","type":"debug","z":"30af2d3e.d94ea2","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":790,"y":80,"wires":[]},{"id":"b081ddb9.d645f","type":"change","z":"30af2d3e.d94ea2","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$$.payload[Baset = \"Charge\"]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":460,"y":120,"wires":[["70669b60.a16f54","38f8f4c7.447894"]]},{"id":"38f8f4c7.447894","type":"ui_table","z":"30af2d3e.d94ea2","group":"80c78b5e6273ec34","name":"","order":1,"width":0,"height":0,"columns":[],"outputs":0,"cts":false,"x":600,"y":160,"wires":[]},{"id":"80c78b5e6273ec34","type":"ui_group","name":"test","tab":"b7e619d0975936a4","order":4,"disp":true,"width":"17","collapse":false,"className":""},{"id":"b7e619d0975936a4","type":"ui_tab","name":"batch_entry","icon":"dashboard","order":1,"disabled":false,"hidden":false}]den":false}]

I had to simulate your incoming, then remove extra spaces, as there gaps were not tabs. You can remove columns by deleting names in cvs node column input field, but leaving commas in place.
Hope it helps.

1 Like

I'm just getting home, taking your "flow" and woahhoo. Impressed :star_struck:
The 3 lines are displayed in the table.
The "Jisonata" have struck again. Ah sometimes I wish I knew how to program.

So I'll look at this more closely tomorrow, but I understand that the search is on the word "Charge". This one can take three states.

  • Charge
  • discharge
  • Idle
    Is it possible to take into account these 3 values and make it work for one of the 3, these appear according to the charge and discharge?
    If it is rather possible to base on the fact that if there is no battery, the table indicates "Absent"

I will soon be able to manage 16 batteries in an elegant way. Well, I don't have 16 yet, but I know one person who will be happy soon.

Again, I really appreciate your help and time spent helping others.
Which I do a lot with my inverter management software. Happily and with much pleasure

image

Well it's time to stop. More tomorrow

It's hard to be perfect with only half the info. Now you have shown the states you can just change the expression to $$.payload[Baset in ["Charge", "discharge", "Idle"]] or $$.payload[Baset != "Absent"]

2 Likes

Totally fair, I didn't think of it at the time. It was when I saw your code that I woke up. Sorry and thanks for adapting this one.
Well I really need to stop for tonight. I'll be happy to test tomorrow.
Have a nice evening

Following your instruction, it works absolutely as desired.
I will be able to integrate this into my overall solution, I like it more than the spaghetti :spaghetti: dish I had made for only 4 batteries.
Thank you so much!

Edit:
This is very powerful to reduce unwanted spaces :+1:

image

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