# Get data from InfluxDB and add to current counter + save again in Database

**URL:** <https://discourse.nodered.org/t/get-data-from-influxdb-and-add-to-current-counter-save-again-in-database/72528>\
**Category:** General\
**Created:** [21 December 2022 12:30 UTC](https://discourse.nodered.org/t/get-data-from-influxdb-and-add-to-current-counter-save-again-in-database/72528 "2022-12-21T12:30:04Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![stefan2210](https://avatars.discourse-cdn.com/v4/letter/s/e9bcb4/32.png) [@stefan2210](https://discourse.nodered.org/u/stefan2210)\
**Post date:** [21 December 2022 12:30 UTC](https://discourse.nodered.org/t/get-data-from-influxdb-and-add-to-current-counter-save-again-in-database/72528/1 "2022-12-21T12:30:04Z")

</div>

Again I need some help. I hope it will be easy for you.

I want to monitor a small PV-plant. Now I am so far, that I can get allso the current datas (energy, power).  
Actually my plan is to store also the total energy produced.

My plan:

1. get last entry from my Influx-DB-measurement
2. add total counter to current energy measurement
3. save everything back in an InfluxDB

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

InfluxDB-In-Node:  
SELECT gesamtverbrauch\_kWh FROM fg\_b17\_balkonsolar ORDER BY time DESC LIMIT 1

function:  
flow.set("gesamtverbrauch\_db", msg.payload.gesamtverbrauch\_kWh);  
if I show the debug, it says NaN, but also this did not help instead  
flow.set("gesamtverbrauch\_db", msg.payload.gesamtverbrauch\_kWh);

This is my main function to get the data and make it ready for influxDB.

```auto
var output = msg.payload.svalue.split(";");
var i=2000;
var momentanleistung;
var gesamtverbrauch = flow.get("gesamtverbrauch_db"); //wird über flow-Variable geholt

if(output[0]==0) {
    momentanleistung=0;
} else {
    momentanleistung = 0.5*3600/(output[2]/1000);
}

var energieverbrauch = output[0]*1000/i;

gesamtverbrauch = gesamtverbrauch + energieverbrauch/1000;

if(msg.payload.idx==1) { //Leistungszähler=1, BME=2
    msg.payload = {
        //Leistungszähler 0=differenz, 1=total, 2=zeit
        energieverbrauch_wh: energieverbrauch,
        momentanleistung_w: momentanleistung,
        gesamtverbrauch_kWh:gesamtverbrauch
    }
    return msg;
}

```

When I run it with the flow.get and flow.set(), then the flow does not work any more.

Or is there any easier way, how to get the current total energy from Influx-DB to my main function, so that I can calculate with it?

Best regards  
Stefan

---

<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:** [21 December 2022 14:41 UTC](https://discourse.nodered.org/t/get-data-from-influxdb-and-add-to-current-counter-save-again-in-database/72528/2 "2022-12-21T14:41:17Z")

</div>

> [@stefan2210](#):
>
> I can get allso the current datas (energy, power).

Exactly what do you get from the device?

---

<div class="post-metadata">

**Author:** ![stefan2210](https://avatars.discourse-cdn.com/v4/letter/s/e9bcb4/32.png) [@stefan2210](https://discourse.nodered.org/u/stefan2210)\
**Post date:** [23 December 2022 12:11 UTC](https://discourse.nodered.org/t/get-data-from-influxdb-and-add-to-current-counter-save-again-in-database/72528/3 "2022-12-23T12:11:15Z")

</div>

Hey, sorry for my late reply:  
this is what I get from the device for example:

{"idx":1,"RSSI":10,"nvalue":0,"svalue":"5.00;14.00;435.32"}

Usually it works fine, if I dont want to add the total counter from the database.

---

<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:** [23 December 2022 22:09 UTC](https://discourse.nodered.org/t/get-data-from-influxdb-and-add-to-current-counter-save-again-in-database/72528/4 "2022-12-23T22:09:48Z")

</div>

What do the numbers mean and what are you writing to the database?

---

<div class="post-metadata">

**Author:** ![stefan2210](https://avatars.discourse-cdn.com/v4/letter/s/e9bcb4/32.png) [@stefan2210](https://discourse.nodered.org/u/stefan2210)\
**Post date:** [24 December 2022 07:53 UTC](https://discourse.nodered.org/t/get-data-from-influxdb-and-add-to-current-counter-save-again-in-database/72528/5 "2022-12-24T07:53:55Z")

</div>

Hey, I just tried a bit further:

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

I use the MQTT-Out as debug node, as the Node-Red dubug-node does not work...  
First I get the result from the InfluxDB-Request, which seems to work. In influx the field gesamtverbrauch\_kWh in float.

`[{"time":"2022-12-24T07:46:00.982Z","gesamtverbrauch_kWh":0}]`

Then I tried to convert to json first, but it did not help.  
`[{"time":"2022-12-24T07:46:00.982Z","gesamtverbrauch_kWh":0}]`

In the function I just try to make some calculations, but the result is a NaN.  
flow.set("gesamtverbrauch\_db", msg.payload.gesamtverbrauch\_kWh);  
msg.payload = Number(msg.payload.gesamtverbrauch\_kWh) + 3;  
return msg;

`NaN`

Somhow, the gesamtverbrauch\_kWh seems not to be a Number, even though i tried to convert with Number().

What am I doing wrong here. I think this explains also, why it does not work when I set and get the variable and want to calculate after the get command.

The number that I get from the database is just the energy input in the public net. As I am still trying it is still 0. And the number can only grow if I get the old number and add the actual value and save the new accumulated value again.

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [24 December 2022 08:59 UTC](https://discourse.nodered.org/t/get-data-from-influxdb-and-add-to-current-counter-save-again-in-database/72528/6 "2022-12-24T08:59:48Z")

</div>

```auto
[...]

```

indicates you receive an array.

ie. msg.payload[0] = the first element in the array:

```auto
const input = msg.payload[0];

flow.set("gesamtverbrauch_db", input.gesamtverbrauch_kWh);
msg.payload = Number(input.gesamtverbrauch_kWh) + 3;

```

---

<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:** [24 December 2022 09:00 UTC](https://discourse.nodered.org/t/get-data-from-influxdb-and-add-to-current-counter-save-again-in-database/72528/7 "2022-12-24T09:00:52Z")

</div>

> [@stefan2210](#):
>
> the Node-Red dubug-node does not work

I imagine the debug node is the most used node in the node red palette. If it is not working for you then I think there is something very odd going on that you need to track down. You will find it very hard work developing node-red flows without the debug node.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [24 December 2022 09:08 UTC](https://discourse.nodered.org/t/get-data-from-influxdb-and-add-to-current-counter-save-again-in-database/72528/8 "2022-12-24T09:08:38Z")

</div>

When you send things to MQTT it will just send the characters for the thing you send it - so there is no way of knowing if it should be represent a number or not so it will come back as a string. You would be much better off using the debug node to debug this... if you don't think that is working please share your thoughts so we can try to help.

---

<div class="post-metadata">

**Author:** ![stefan2210](https://avatars.discourse-cdn.com/v4/letter/s/e9bcb4/32.png) [@stefan2210](https://discourse.nodered.org/u/stefan2210)\
**Post date:** [27 December 2022 18:47 UTC](https://discourse.nodered.org/t/get-data-from-influxdb-and-add-to-current-counter-save-again-in-database/72528/9 "2022-12-27T18:47:44Z")

</div>

Hey,  
it was in fact a array and bakmans solution helped me out.

There is some problem with the debug node and node red generally, but I do not know where it comes from. Maybe the server is just too weak...But this happened also to other users before. Now it works 🙂

Thanks for your support.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [28 December 2022 12:37 UTC](https://discourse.nodered.org/t/get-data-from-influxdb-and-add-to-current-counter-save-again-in-database/72528/10 "2022-12-28T12:37:29Z")

</div>

Can you point at one of these other users you say also has the problem? So far your answers do not indicate anything we can investigate.

---

<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 February 2023 12:37 UTC](https://discourse.nodered.org/t/get-data-from-influxdb-and-add-to-current-counter-save-again-in-database/72528/11 "2023-02-26T12:37:42Z")

</div>

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