# Formatting of my battery responses and display in the dashboard

**URL:** https://discourse.nodered.org/t/formatting-of-my-battery-responses-and-display-in-the-dashboard/66331
**Category:** General
**Created:** [11 August 2022 13:19 UTC](https://discourse.nodered.org/t/formatting-of-my-battery-responses-and-display-in-the-dashboard/66331 "2022-08-11T13:19:45Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![Jean-Luc](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jean-luc/32/89996_2.png) [@Jean-Luc](https://discourse.nodered.org/u/Jean-Luc)
#### Post date: [11 August 2022 13:19 UTC](https://discourse.nodered.org/t/formatting-of-my-battery-responses-and-display-in-the-dashboard/66331/1 "2022-08-11T13:19:45Z")

</div>

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 🙃 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

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/f/0fac7fe344db8919d879f7f2c852d61bfaf24e73.png)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/0/50530ee3087c85d1cee972656b5dc707ae387434.png)

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

```auto
["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:

```auto
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.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/5/857cab226fee4c33abb41f34ef2f4b3df089fa8b.png)

I hope you were able to follow my explanations

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [11 August 2022 13:34 UTC](https://discourse.nodered.org/t/formatting-of-my-battery-responses-and-display-in-the-dashboard/66331/2 "2022-08-11T13:34:36Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Jean-Luc](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jean-luc/32/89996_2.png) [@Jean-Luc](https://discourse.nodered.org/u/Jean-Luc)
#### Post date: [11 August 2022 14:03 UTC](https://discourse.nodered.org/t/formatting-of-my-battery-responses-and-display-in-the-dashboard/66331/3 "2022-08-11T14:03:40Z")

</div>

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

```auto
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]
"
$$↵"

```

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [11 August 2022 17:07 UTC](https://discourse.nodered.org/t/formatting-of-my-battery-responses-and-display-in-the-dashboard/66331/4 "2022-08-11T17:07:37Z")

</div>

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. 🙂

```auto
[{"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"]]}]

```

```auto
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}

```

---

<div class="post-metadata">

### Author: ![Jean-Luc](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jean-luc/32/89996_2.png) [@Jean-Luc](https://discourse.nodered.org/u/Jean-Luc)
#### Post date: [11 August 2022 18:38 UTC](https://discourse.nodered.org/t/formatting-of-my-battery-responses-and-display-in-the-dashboard/66331/5 "2022-08-11T18:38:56Z")

</div>

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 🤭  
It lacks visually, the voltage and the word "absent" to then be able to eliminate it.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/b/cb5db786eb5061ac6f41ef571396a8cab3b7dc67.png)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/e/be976b2ba9412c19ef63e2ec45cf1a19034f1d54.png)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/8/f86ad868d6eab2672581ad1ee57bd2d16c30e0fc.png)

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [11 August 2022 19:42 UTC](https://discourse.nodered.org/t/formatting-of-my-battery-responses-and-display-in-the-dashboard/66331/6 "2022-08-11T19:42:50Z")

</div>

Here is another solution that may work for you

```auto
[{"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.

---

<div class="post-metadata">

### Author: ![Jean-Luc](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jean-luc/32/89996_2.png) [@Jean-Luc](https://discourse.nodered.org/u/Jean-Luc)
#### Post date: [11 August 2022 21:06 UTC](https://discourse.nodered.org/t/formatting-of-my-battery-responses-and-display-in-the-dashboard/66331/7 "2022-08-11T21:06:19Z")

</div>

I'm just getting home, taking your "flow" and woahhoo. Impressed 🤩  
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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/9/596f5a6f342cf46fe98116d7e199d2443a0db110.png)

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/e/0e7c0d42f9605d4dcd271d6b8825480dc65c9ae7.png)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/8/d81e74ed68e60e145bbb9df32c95a285a8bb3522.png)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/7/6792615c4393db5946d6e705ab4e744d1bc32307.png)

Well it's time to stop. More tomorrow

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [11 August 2022 21:20 UTC](https://discourse.nodered.org/t/formatting-of-my-battery-responses-and-display-in-the-dashboard/66331/8 "2022-08-11T21:20:17Z")

</div>

> [@Jean-Luc](#):
>
> **Charge**". This one can take three states.
> 
> - Charge
> - discharge
> - Idle

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"]`

---

<div class="post-metadata">

### Author: ![Jean-Luc](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jean-luc/32/89996_2.png) [@Jean-Luc](https://discourse.nodered.org/u/Jean-Luc)
#### Post date: [11 August 2022 21:33 UTC](https://discourse.nodered.org/t/formatting-of-my-battery-responses-and-display-in-the-dashboard/66331/9 "2022-08-11T21:33:26Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Jean-Luc](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jean-luc/32/89996_2.png) [@Jean-Luc](https://discourse.nodered.org/u/Jean-Luc)
#### Post date: [12 August 2022 08:32 UTC](https://discourse.nodered.org/t/formatting-of-my-battery-responses-and-display-in-the-dashboard/66331/10 "2022-08-12T08:32:18Z")

</div>

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 🍝 dish I had made for only 4 batteries.  
Thank you so much!

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/4/f4113cc07e162e95cd698be398082407f5444a82.png)

Edit:  
This is very powerful to reduce unwanted spaces 👍

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/b/cbe028935a40c3f9b5e20c891ef2abb765aa16b7.png)

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [26 August 2022 08:33 UTC](https://discourse.nodered.org/t/formatting-of-my-battery-responses-and-display-in-the-dashboard/66331/11 "2022-08-26T08:33:00Z")

</div>

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