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

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

SELECT gesamtverbrauch_kWh FROM fg_b17_balkonsolar ORDER BY time DESC LIMIT 1

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.

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) {
} 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,
    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

Exactly what do you get from the device?

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


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

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

Hey, I just tried a bit further:

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.


Then I tried to convert to json first, but it did not help.

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;


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.

[ ... ]

indicates you receive an array.

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

const input = msg.payload[0];

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

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.

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.

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 :slight_smile:

Thanks for your support.

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.

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