question

larsea-dk avatar image
larsea-dk asked

Help with "not always working" NodeRed code

Hi

to control my feedin to the battery, I have a code tweaking the Setpoint made by help from some guys from Germany a while ago.

Due to sometimes we need to pay for delivering to the net at full battery, I have tried to make a code that reset this tweak simply by setting the setpoint to 0 if the price is too low.

Id to though not always work and the input to the code from Nordpool gives the corrct input for the code to work.

Any inputs why it seems not always to work?

1714304153042.png

Thank you very much :-)

battery chargingNode-REDfeed-in limit
1714304153042.png (11.6 KiB)
4 comments
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 commented ·
HI @larsea-dk


Do you mind sharing how you're defining the variables? Is there a chance that sometimes one of the var's is undefined when the code is running. Which is causing the inconsistent outcome.

0 Likes 0 ·
Show more comments
1 Answer
matt1309 avatar image
matt1309 answered ·

In addition to my comment I thought it might be useful to share some code i wrote for someone awhile back doing something similar but using time criteria as well as ac and dc solar: My guess is the reading last available value from global is the bit that's causing your issues as this prevents vars being undefined.


As noted in comment you can read victron data directly from global context in more recently versions of venus os (I've put link for more details on that in the comment reply above)


//setting up time variables
var currentTime = new Date();
var hours = currentTime.getHours();
var minutes = currentTime.getMinutes();
 
//setting up data variables. If using up to date venus os you could get these directly via global context. 
var soc = global.get('soc');
var acSolar = global.get('acSolar');
var dcSolar = global.get('dcSolar');
var gridSetpoint = global.get('gridSetpoint');
 
//setting soc variable depending on inputs from flows
if (msg.topic === "soc") {
  soc = msg.payload; // Extract payload from input 1
  global.set('soc', soc);
}
 
if (msg.topic === "acSolar") {
  acSolar = msg.payload; // Extract payload from input 2
  global.set('acSolar', acSolar);
}
 
if (msg.topic === "dcSolar") {
  dcSolar = msg.payload; // Extract payload from input 3
  global.set('dcSolar', dcSolar);
}
 
//this is used to check if gridsetpoint is already 0, if it is then we won't bother updating it.
if (msg.topic === "gridSetpoint") {
  gridSetpoint = msg.payload;
  global.set('gridSetpoint', gridSetpoint);
}
 
if (msg.topic === "acLoad") {
 
  acLoad = msg.payload; // Extract payload from input 2
 
  global.set('acLoad ', acLoad);
 
}
 
//two examples of times
if (hours >= 10 && hours < 14) { //if time is between 10am-1.59pm
  if (soc > 70 && acLoads < 10000) { //if state of charge is above 70%
    //return the new grid setpoint
    gridSetpoint = -Math.min(acSolar + dcSolar, 6000);
  } else {
    gridSetpoint = 0;
  }
} else if (hours >= 14 && hours < 19) { //if time is between 2pm and 6.59pm
  if (soc > 50 && acLoads < 10000) { //if state of charge is above 50%
    //return the new grid setpoint
    gridSetpoint = -Math.min(acSolar + dcSolar, 6000);
  } else {
    gridSetpoint = 0;
  }
} else if (hours >= 19 && hours < 23) { //if time is between 7pm and 10.59pm
  if (soc > 30 && acLoads < 10000) { //if state of charge is above 30%
    //return the new grid setpoint
    gridSetpoint = -Math.min(acSolar + dcSolar, 6000);
  } else {
    gridSetpoint = 0;
  }
} else {
  gridSetpoint = 0;
}
 
if (gridSetpoint !== global.get('gridSetpoint')) { // Check if gridSetpoint is going to change
  global.set('gridSetpoint', Math.min(gridSetpoint, 0));
  msg.payload = Math.min(gridSetpoint, 0);
  return msg;
}
4 comments
2 |3000

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

larsea-dk avatar image larsea-dk commented ·

I did some check at the msg controlling if feedin is active or deactivated. This is controlled by an input from a hourly check of the market price. So this msg is only populated once per hour and rest seems to be populated almost at the same time. …they do at least all show on the debug list. The feedin msg do though miss even though the value has been populated once.

Is this the fault perhaps.

I am thinking of having this check somewhere else than in this function, but what could be an idea. Before the function tweaking the grid Setpoint maybe, cause if price is low, the grid Setpoint do not need to be tweaked actually.


0 Likes 0 ·
matt1309 avatar image matt1309 larsea-dk commented ·

Hi @larsea-dk


Just so I'm understanding correctly. You're saying all your variables update frequently except for price which updates hourly and you're then having issues where all the frequent variables have updated but the price is becoming undefined?


If that's the issue/if I've understood correctly you could adjust your flow to write the price to global context. ie

//in the node where you get price;

global.set("price",msg.payload);


Then in your gridsetpoint function you access it

var price = global.get("price");

And used something like


if(price > x){

//orignal code you have

}else if(gridsetpoint !== 0) { //stops you spamming new gridsetpoint messages if it's already 0;

//set gridsetpoint = 0;

}


However like you said you could do this externally to the function so the function doesnt even run if it's not at the right price.


I'm happy to help with more code if you know exactly what you're looking for.

0 Likes 0 ·
larsea-dk avatar image larsea-dk matt1309 commented ·

Thank you very much, really appriciated!! I might need to show the entire code for you to understand. But I'll do some test with the global var to see if it works.

BR,

Lars

1 Like 1 ·
matt1309 avatar image matt1309 larsea-dk commented ·
No worries, happy to lend a hand where i can
0 Likes 0 ·