# XML page TO database INFLUXDB

**URL:** https://discourse.nodered.org/t/xml-page-to-database-influxdb/59388
**Category:** General
**Tags:** http-request, database, mqtt
**Created:** [4 March 2022 23:50 UTC](https://discourse.nodered.org/t/xml-page-to-database-influxdb/59388 "2022-03-04T23:50:41Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![radiolinux](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/radiolinux/32/57868_2.png) [@radiolinux](https://discourse.nodered.org/u/radiolinux)
#### Post date: [4 March 2022 23:50 UTC](https://discourse.nodered.org/t/xml-page-to-database-influxdb/59388/1 "2022-03-04T23:50:42Z")

</div>

Hello everybody.

I started using node-red recently and I discovered that it is a very powerful application with which infinite automations can be created. I searched in this forum but without success, an example or suggestions to read the data of an XML page of an address [http://192.168.2.8/bcc.xml](http://192.168.2.8/bcc.xml) and copy them in the INFLUXDB database to finally create graphs (GRAFANA) for the monitoring of my photovoltaic system (battery cell charger, panel production, etc.).

With my little experience in NODE-RED I was able to transform the XML page data into objects (arrays) or into a data string. I believe they need a JSON script to select only the single values and copy them into the INFLUXDB database.

Thanks in advance to those who could give me a little suggestion and help, thanks  
Regards

_this is the XML page_

 ![xml_1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/7/e7d8b56aade935a29dc9024e9941500979c9ed8d.png)

 ![node-red_1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/9/999f70dc1d3f159af0c4e5358c304911030d825c.png)

_this is the incorrect result of the influxd database with the wrong value field_

 ![show](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/4/e4d9c1c883e3f5ec9210227cc9a9c06ee0773f82.png)

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [5 March 2022 10:18 UTC](https://discourse.nodered.org/t/xml-page-to-database-influxdb/59388/3 "2022-03-05T10:18:53Z")

</div>

You can put this in a function node to move the values into msg.payload.

```auto
// move msg.payload.data into msg.payload
msg.payload = msg.payload.data
// take first element of the array in each entry
for (const [key, value] of Object.entries(msg.payload)) {
    msg.payload[key] = value[0]
}
return msg;

```

However I don't think that is enough. Presumably you want all the numeric fields as Number type rather than string. Also what do you want to do with the N/A values?

---

<div class="post-metadata">

### Author: ![radiolinux](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/radiolinux/32/57868_2.png) [@radiolinux](https://discourse.nodered.org/u/radiolinux)
#### Post date: [5 March 2022 11:24 UTC](https://discourse.nodered.org/t/xml-page-to-database-influxdb/59388/4 "2022-03-05T11:24:47Z")

</div>

Dear Mr. Colin, I often read your precious interventions and help and for this I thank you.  
All N / A values are numbers that correspond to the voltage reading of the individual cells that make up the battery.

I am attaching an example of the XML format in which only fields are already filled in

 ![value NA](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/a/6a2ac57e2acd132651a44485e5b49debc59b1712.png)

or if it can be of help and interest to other people for reading the photovoltaic data, this is the manual: [GWL/MODULAR Battery Communicator and Controller](https://files.gwl.eu/inc/_doc/attach/StoItem/7542/Manual_BCC_EN.pdf)

Thank you and have a good weekend

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [5 March 2022 11:46 UTC](https://discourse.nodered.org/t/xml-page-to-database-influxdb/59388/5 "2022-03-05T11:46:13Z")

</div>

So what do you want to do with those fields when you write them to influx? Possibly you want to leave them empty.

Also which fields do you want to convert to numbers and which leave as strings? You have to think about each field and decide what you want to put into influx. For example the relay1 and relay2 fields are strings, but do you want to set those to 0 and 1 so you can plot them on a graph (for instance) or do you want to leave them as strings? What about status?

Once you know what you want to do with each field then it can be coded up.

---

<div class="post-metadata">

### Author: ![radiolinux](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/radiolinux/32/57868_2.png) [@radiolinux](https://discourse.nodered.org/u/radiolinux)
#### Post date: [5 March 2022 12:54 UTC](https://discourse.nodered.org/t/xml-page-to-database-influxdb/59388/6 "2022-03-05T12:54:12Z")

</div>

Colin, you are a kind person who took my request for help to heart.

To simplify, I have decoded the type of fields in the INFLUXDB database which will be subsequently displayed by GRAFANA.  
All N / A fields need to be converted to numbers.  
The "connection" field must be string while the "relay1" and "relay2" fields will be set to 0 or 1 to plot a graph.

Best regards

```auto
<utotal> NUMBER </utotal>
<ucell1> NUMBER</ucell1>
<ucell2> NUMBER </ucell2>
<ucell3> NUMBER </ucell3>
<ucell4> NUMBER </ucell4>
<ucell5> NUMBER </ucell5>
<ucell6> NUMBER </ucell6>
<ucell7> NUMBER </ucell7>
<ucell8> NUMBER </ucell8>
<ucell9> NUMBER </ucell9>
<ucell10> NUMBER </ucell10>
<ucell11> NUMBER </ucell11>
<ucell12> NUMBER </ucell12>
<ucell13> NUMBER </ucell13>
<ucell14> NUMBER </ucell14>
<ucell15> NUMBER </ucell15>
<ucell16> NUMBER </ucell16>
<trimmer-0> NUMBER </trimmer-0>
<trimmer-1> NUMBER </trimmer-1>
<UMINset> NUMBER </UMINset>
<UMAXset> NUMBER </UMAXset>
<Umincell> NUMBER </Umincell>
<Umaxcell> NUMBER </Umaxcell>
<UmincellID> NUMBER </UmincellID>
<UmaxcellID> NUMBER </UmaxcellID>
<Udiff> NUMBER </Udiff>
<lastcellevent> NUMBER </lastcellevent>
<lastcellno> NUMBER </lastcellno>
<lastcellU> NUMBER </lastcellU>
<lastbattU> NUMBER </lastbattU>
<connection> STRING </connection>
<status> NUMBER </status>
<relay1> 1 OR 0 </relay1>
<relay2> 1 OR 0</relay2>

```

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [5 March 2022 13:42 UTC](https://discourse.nodered.org/t/xml-page-to-database-influxdb/59388/7 "2022-03-05T13:42:17Z")

</div>

> [@radiolinux](#):
>
> All N / A fields need to be converted to numbers.

What number for fields that are in the xml as N/A? Would it not be better to leave them as null in the database if they show N/A in the xml?

Assuming that what you want is for them to be left empty then this might do it. Make sure you check it out in a debug node before connecting to the influx node.

```auto
const data = msg.payload.data
msg.payload = {}
// take first element of the array in each entry
for (const [key, value] of Object.entries(data)) {
    if (key === "connection") {
        msg.payload[key] = value[0]
    } else if (value[0] != "N/A") {
        msg.payload[key] = Number(value[0])
    } else {
        // leave field empty if N/A
    }
}
return msg;

```

---

<div class="post-metadata">

### Author: ![radiolinux](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/radiolinux/32/57868_2.png) [@radiolinux](https://discourse.nodered.org/u/radiolinux)
#### Post date: [5 March 2022 14:08 UTC](https://discourse.nodered.org/t/xml-page-to-database-influxdb/59388/8 "2022-03-05T14:08:59Z")

</div>

Colin, we're almost there 🙂  
The "function" object works perfectly in fact it displays the values of the fields correctly in the debug.  
Influxdb object displays error 400 "unable to parse".

The value of the two relays does not work because the NaN value remains unchanged even if I try to change the state

Where am I doing wrong?

 ![errore-influxdb](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/e/4e92e0c578ae00479c613732ea534d1960e629b6.png)

 ![function](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/0/00c65392e5ca398cef6eb5cea17fc4188366de68.png)

![influxdb](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/7/97f47ce234bdeeecd5ad8e17719c8575e94bc630.png)

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [5 March 2022 14:56 UTC](https://discourse.nodered.org/t/xml-page-to-database-influxdb/59388/9 "2022-03-05T14:56:17Z")

</div>

The relays are not working because I forgot to handle them. Your original debug does not have lastcelevent, lastcellu or lastbattu. What is in them in the xml?  
What are the two string values for the relays that need to be converted to 1 and 0?

---

<div class="post-metadata">

### Author: ![radiolinux](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/radiolinux/32/57868_2.png) [@radiolinux](https://discourse.nodered.org/u/radiolinux)
#### Post date: [5 March 2022 15:11 UTC](https://discourse.nodered.org/t/xml-page-to-database-influxdb/59388/10 "2022-03-05T15:11:33Z")

</div>

Dear Colin,

- the values of the lines of the two relays is **NORMAL** or **SWITCHED** ;
- the values of **lastcellU** and " **lastbattU**" are numeric;
- the value of " **lastcellevent**" is a string.

if a good person, thank you

Best regards,  
Ilario DP

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [5 March 2022 15:13 UTC](https://discourse.nodered.org/t/xml-page-to-database-influxdb/59388/11 "2022-03-05T15:13:42Z")

</div>

Show me the xml in a debug node for an example including the last... values.

You have not said which is 0 and which is 1 for the relays.

---

<div class="post-metadata">

### Author: ![radiolinux](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/radiolinux/32/57868_2.png) [@radiolinux](https://discourse.nodered.org/u/radiolinux)
#### Post date: [5 March 2022 15:27 UTC](https://discourse.nodered.org/t/xml-page-to-database-influxdb/59388/12 "2022-03-05T15:27:22Z")

</div>

The status of the value 0 or 1 of the relays is indifferent.  
Attached is the screeshot of the card and some examples on how it works.

 ![relè](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/a/ca6b5d0f660ddd19427c333ba63df761e0d18dfa.png)

![relè-d](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/b/fb5cd0b17cd86da76c9a83860baf12bdabbed2b3.png)

![relè-c](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/d/8dbb4fe03724b2f639df34f62af76e5af75f46de.png)

![relè-b](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/6/36ea6beadf99970de6772f3da345961c1a24e988.png)

 ![relè-a](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/e/ee9999801bd5609dfc6f435e3ee4e2963d4b61d8.png)

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [5 March 2022 15:38 UTC](https://discourse.nodered.org/t/xml-page-to-database-influxdb/59388/13 "2022-03-05T15:38:04Z")

</div>

So lastcellU and lastbattU have a V on the end. Did you not see the NaN numbers and look back at the xml to see why they were different? Similarly had you looked at the lastcellevent value you would have seen it was a string, which would have saved us both some time.

Try this

```auto
const data = msg.payload.data
msg.payload = {}
// take first element of the array in each entry
for (const [key, value] of Object.entries(data)) {
    if (key === "connection" || key === "lastcellevent") {
        msg.payload[key] = value[0]
    } else if (key.startsWith("relay")) {
        msg.payload[key] = value[0] === "SWITCHED" ? 1 : 0
    } else if (value[0] != "N/A") {
        msg.payload[key] = parseFloat(value[0])
    } else {
        // leave field empty if N/A
    }
}
return msg;

```

If you still have any incorrect values then make sure to post the xml as well as the function output.

---

<div class="post-metadata">

### Author: ![radiolinux](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/radiolinux/32/57868_2.png) [@radiolinux](https://discourse.nodered.org/u/radiolinux)
#### Post date: [5 March 2022 15:56 UTC](https://discourse.nodered.org/t/xml-page-to-database-influxdb/59388/14 "2022-03-05T15:56:49Z")

</div>

**!!! WOW !!!**

Dear Colin, I'm sorry I wasted your time.  
Your invaluable support was very important that you gave me a happy weekend. If you come to Italy in Rome I will be happy to offer you a coffee.

Thank you again.  
Ilario DP

 ![WOW](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/9/39b596e608726240bee3c74e3c257a4d1880f5d1.png)

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [5 March 2022 16:39 UTC](https://discourse.nodered.org/t/xml-page-to-database-influxdb/59388/15 "2022-03-05T16:39:32Z")

</div>

Glad to be of help. Now spend some time working out how the function works so that next time you will be able to sort it yourself. 🙂

---

<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: [19 March 2022 16:39 UTC](https://discourse.nodered.org/t/xml-page-to-database-influxdb/59388/16 "2022-03-19T16:39:49Z")

</div>

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