question

getacca avatar image
getacca asked

Output power from four inverters in one sum. Node Red

Dashboard.jpgNodeRed.jpgA few days ago I built a dashboard in NodeRed. Now I have a problem that I need help with. I have four PV systems that produce electricity. I would like to see the current electricity production in watts for all four systems in one sum. My problem is that the four systems sometimes send their values with a time delay and this distorts the buzzers. How do I manage to add up the values of all four systems in NodeRed and display the total. The problem is the different receipt of messages from the PV systems. My current functions code looks like this:

var summe = 0;

// Iterieren Sie über alle Werte im Array und summieren Sie sie

for (var i = 0; i < msg.payload.length; i++) {

summe += parseFloat(msg.payload[i]);

}

// Aktualisieren Sie die Payload mit der Summe

msg.payload = summe;

// Senden Sie die Nachricht weiter

return msg;


Kann mir bitte jemand helfen?

Node-REDdashboard
dashboard.jpg (139.2 KiB)
nodered.jpg (97.2 KiB)
2 |3000

Up to 8 attachments (including images) can be used with a maximum of 190.8 MiB each and 286.6 MiB total.

6 Answers
matt1309 avatar image
matt1309 answered ·

Hi @Getacca

I get around this using global or flow variables.

So each PV write to their own global var.

Then in your function node code you sum the global vars. This will therefore use the latest value for each pv.

You may need/want to add logic to set global vars to zero in the event data isnt received after a specific time frame (ie inverters get shut off suddenly for whatever reason).


If i was writing the check for not updated recently i would:

Go from PV data node into a set node that does:

global.pv1 = msg.payload

And also

global.pv1Time = //store curret time here.



Then in function node

do

if global.pv1Time - currentTime > x seconds (Where x is the number number of seconds you'll allow pv to not provide data for)

global.pv1Time = //store current time
global.pv1 = 0; //as discconnected

2 |3000

Up to 8 attachments (including images) can be used with a maximum of 190.8 MiB each and 286.6 MiB total.

getacca avatar image
getacca answered ·

Thank you for the quick support. Unfortunately, I'm not an expert and I mostly copied the code from the forum. What I didn't understand is how to write the individual values of the inverters into global variables. Is it too much to ask you to send me this code? You would help me a lot with this, as I have already spent hours trying to solve the problem via chat-GPT. It's a little frustrating right now.

2 |3000

Up to 8 attachments (including images) can be used with a maximum of 190.8 MiB each and 286.6 MiB total.

matt1309 avatar image
matt1309 answered ·

Hi @Getacca

Few ways. To do it depending if you prefer built in nodes or functions but here's how i would do it. I'm not a javascript dev (or a dev at all), so this is probably terribly written but hopefully a good starting point for you.

I would combine everything into one function node just for it to look nice.

But first the node red function needs a way of knowing which PV is providing it with data so it can put it in the correct global variable. We can do this by setting the message topic Using the below flow:


PV1 -> change node that sets the topic of the message to PV1 -> central function node below

PV2 -> change node that sets topic of message to PV2 -> central function node below.

etc


Then output of those changes nodes all go into one central function node:


Which looks something like:

var currentTime = new Date();
var timeLimit = 30 * 1000; // 30 seconds

// Define array of PV variables and their last updated times
var pvVariables = [
    { name: 'PV1', lastUpdated: global.get('PV1LastUpdated'), value: global.get('PV1') },
    { name: 'PV2', lastUpdated: global.get('PV2LastUpdated'), value: global.get('PV2') }
    // Add more PV variables as needed
];




// Update PV values and last updated times based on input messages
for (var i = 0; i < pvVariables.length; i++) {
    if (msg.topic === pvVariables[i].name) {
        pvVariables[i].value = msg.payload;
        pvVariables[i].lastUpdated = currentTime;
        global.set(pvVariables[i].name, pvVariables[i].value);
        global.set(pvVariables[i].name + 'LastUpdated', currentTime);
        break; // Exit loop once value is updated as only one data point is input at a time
    }
}

/*
Loop through PV variables to check if they have provided data within the time limit
if not then they've turned off and we need to set PV output to 0. ie if we store 500w but it turns off and doesnt send 0w we'll still think there's 500w when there isn't. Adjust the timelimit var to long or short than 30seconds as needed. 
*/


for (var i = 0; i < pvVariables.length; i++) {
    if (currentTime - pvVariables[i].lastUpdated > timeLimit) {
        global.set(pvVariables[i].name, 0);
        pvVariables[i].value = 0;
    }
}


var summe;
for (var i = 0; i < pvVariables.length; i++) {
summe += parseFloat(pvVariables[i].value);

}






I've not tested this so please use node.warn('message for debugging in here'); to make sure everything is working as expected

2 |3000

Up to 8 attachments (including images) can be used with a maximum of 190.8 MiB each and 286.6 MiB total.

Dirk-Jan Faber (Victron Energy) avatar image
Dirk-Jan Faber (Victron Energy) answered ·

The Victron nodes already store the data into the global context. You can just use those. See https://www.victronenergy.com/blog/2024/02/23/node-red-update-for-victron-users/ for more info on that.

2 |3000

Up to 8 attachments (including images) can be used with a maximum of 190.8 MiB each and 286.6 MiB total.

matt1309 avatar image
matt1309 answered ·

@Getacca

Sorry I wasnt aware of Dirk's post, use that instead. So no need to write to global vars as they'll already exist.

I dont think i've got access to this feature yet (running 3.10) so might upgrade myself.

Thanks @Dirk-Jan Faber (Victron Energy)

2 |3000

Up to 8 attachments (including images) can be used with a maximum of 190.8 MiB each and 286.6 MiB total.

getacca avatar image
getacca answered ·

Thanks for the feedback, I installed and looked at it yesterday. Unfortunately I can't handle it yet and need to take a closer look at it.

2 |3000

Up to 8 attachments (including images) can be used with a maximum of 190.8 MiB each and 286.6 MiB total.