Deel 3 van de Flow "Targets"
In verband met de gelimiteerde ruimte in een post is de JavaScript tekst van functie "Calculate Targets" hier separaat opgenomen.
Kopieer deze tekst en plak die in de functie "Calculate Targets"in de Node Red Flow.
Deel twee staat in de volgende post.
In verband met de gelimiteerde ruimte in een post is de JavaScript tekst van functie "Calculate Targets" hier separaat opgenomen.
Kopieer deze tekst en plak die in de functie "Calculate Targets"in de Node Red Flow.
Deel twee staat in de volgende post.
// Function Calculate Targets
const TraceOn = false;
const Now = new Date()
const NowHour = Now.getHours();
const NowMinutes = Now.getMinutes();
const NowInSeconds = Math.floor(Now.getTime() / 1000);
let NowTime = ("0" + Now.getHours()).slice(-2) + ":"
+ ("0" + Now.getMinutes()).slice(-2) + ":"
+ ("0" + Now.getSeconds()).slice(-2)
;
const Batt_Capacity_Wh = Math.floor(3 * 5.12 * 1000); // three batteries
const SoC_Full_Perc = 98; // when Battery is realistically full
const Grid_Setpoint_W_Limit = 10000;
const Solar_Charge_Perc = 98; // max perc to use for charging
// Sell only if price difference > n.n Ct
const Sell_Price_Limit_Ct = 3.0;
const Conversion_Loss_Factor = 0.9; // conversion losses
// Variables to read from Global
var SoC_Min_Manual_Perc = null;
var Grid_Setpoint_Manual_W = null;
var DayAhead_PriceArray_Ct = null;
var Consump_Forecast_Wh = null;
var Solar_Forecast = null;
var Solar_Forecast_Modified = null;
var Solar_Sunrise = null;
var Solar_Sunset = null;
var DebugText = "";
var ErrorText = "";
var Solar_Today_Wh = 0;
var Solar_Remain_Wh = 0;
var Solar_Remain_Zero_Price_Wh = 0;
var Solar_Remain_VeryLow_Price_Wh = 0;
var Solar_Remain_Low_Price_Wh = 0;
var Solar_Remain_Rest_Price_Wh = 0;
var Solar_Tomorrow_Wh = 0;
var Sunrise_Hour = -1;
var Sunset_Hour = -1;
var Solar_To_Batt_Perc = 0;
var Batt_Forecast_Wh = [];
var Batt_Full_Wh = 0;
var Batt_Min_Manual_Wh = 0;
var Batt_Min_Bottom_Wh = 0;
var Batt_Now_Wh = 0;
var Batt_Free_Wh = 0;
var Batt_Max_Current_Grid_A = 0;
var Batt_Max_Current_Solar_A = 0;
var Batt_Charger_Only_Target = false;
var Energy_from_Grid_Needed_Wh = 0;
var Energy_to_Sell_Wh = 0; // when negative price comes
var Energy_for_Discharge_W = 0;
var Forecast_Horizon_Hour = 0;
var Next_High_Sell_Hour = -1;
var Next_High_Sell_Price_Ct = 0;
var Next_Cheap_Charge_Hour = -1;
var Next_Cheap_Charge_Price_Ct = 999999;
var SoC_Min_Bottom_Perc = 0;
var SoC_Min_Target_Perc = 0;
var Grid_Setpoint_Unload_W = 0;
var Grid_Setpoint_Target_W = 0;
var Max_Inverter_Power_Target_W = 0;
var Charge_Disable_Target = 0; // 1 = disabled; 0 = charging enabled
var Solar_Charger_Target = 1; // 1 = ON; 4 OFF
var Relay_2_Target = 0; // 1 = ON; 0 = Off
var msg_DayAhead_Rule = null;
var msg_Batt_Max_Current_Target_A = null;
var msg_SoC_Min_Target_Perc = null;
var msg_Grid_Setpoint_Target_W = null;
var msg_Max_Inverter_Power_Target_W = null;
var msg_Charge_Disable_Target = null;
var msg_Solar_Charger_Target = null;
var msg_Relay_2_Target = null;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function AddDebug(NewText)
{
DebugText += NewText;
} // AddDebug
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function Not(p_Boolean)
{
return !p_Boolean;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function HourAsText(H)
{
if (Number(H) <= 23) {
return Number(H).toString();
}
else {
return "+" + (Number(H)-24).toString();
}
} // HourAsText
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function MinutesRemainFactor() {
return (60 - NowMinutes) / 60;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function WhAskWhText(Wh)
{
if (Wh > 1000) {
return (Round(Wh/100)/10).toString() + " kWh";
}
else {
return Round(Wh).toString() + " Wh";
}
} // WhAskWhText
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function kWhToWh(x)
{
return Round(x * 1000);
} // kWhToWh
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function Round(x, dec)
{
if (dec == null)
{ return Math.round(x) }
else if (dec == 0)
{ return Math.round(x) }
else if (dec == 1)
{ return Math.round(x*10) / 10 }
else if (dec == 2)
{ return Math.round(x*100) / 100 }
} // Round
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function IsInArrayOfPairs(p_ArrayOfPairs, p_SearchHours)
{ // See if any of the items within array p_SearchHours
// are in array p_ArrayOfPairs{hour, value]
let Result = false;
for (let i = 0; i < p_SearchHours.length; i++) {
for ( let j = 0; j < p_ArrayOfPairs.length; j++)
{
if ( p_ArrayOfPairs[j][0] == p_SearchHours[i] )
{
Result = true;
break;
} // Item was found
} // for j
if (Result) break;
} // all items
return Result; // not found
} // IsInArrayOfPairs
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function ClearContext()
{
// Clear other reference-values
context.Last_DESS_Mode = -1;
context.Last_Solar_Sunrise = "";
context.Last_Solar_Sunset = "";
context.Last_Solar_Today_Wh = 0;
context.Last_Solar_Remain_Wh = 0;
context.Last_Solar_Tomorrow_Wh = 0;
context.Last_Batt_Forecast_Wh = -1;
context.Last_SoC_Min_Manual_Perc = -1;
context.Last_SoC_Now_Perc = -1;
context.Last_Batt_High_Value = -1;
context.Last_Batt_Low_Value = -1;
context.Last_Forecast_Horizon_Hour = -1;
context.Last_Next_High_Sell_Hour = -1;
context.Last_Next_Cheap_Charge_Hour = -1;
context.Last_Energy_from_Grid_to_Stay_Above_Minimum_Wh = -100;
context.Last_Energy_from_Grid_to_Full_at_Sunset_Today_Wh = -1;
context.Last_Energy_from_Grid_to_Full_at_Sunset_Tomorrow_Wh = -1;
context.Last_Energy_to_Sell_Wh = -1;
context.Last_SoC_Min_Target_Perc = -1;
context.Last_Batt_Max_Current_Grid_A = -2;
context.Last_Batt_Max_Current_Solar_A = -2;
context.Last_Batt_Max_Current_Target_A = -2;
context.last_Max_Inverter_Power_Target_W = -2;
context.Last_Batt_Charger_Only_Target = false;
context.Last_Grid_Setpoint_Target_W = -2;
context.Last_Charge_Disable_Target = -1;
context.Last_Solar_Charger_Target = -1;
context.Last_Relay_2_Target = -1;
context.Last_DayAheadRule = "";
} // ClearContext
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function Verify_External_Data()
{
let ErrText = "";
let Result = true;
if (context.Energy_from_Solar_Now_W == null)
{ ErrText += "context.Energy_from_Solar_Now_W \r";
Result = false;
}
// From Flow DayAhead
if (context.HighPriceArray == null)
{ ErrText += "context.HighPriceArray \r";
Result = false;
}
if (context.LowPriceArray == null)
{ ErrText += "context.LowPriceArray \r";
Result = false;
}
if (context.VeryLowPriceArray == null)
{ ErrText += "context.VeryLowPriceArray \r";
Result = false;
}
if (context.ZeroPriceArray == null)
{ ErrText += "context.ZeroPriceArray \r";
Result = false;
}
if (context.NegativePriceArray == null)
{ ErrText += "context.NegativePriceArray \r";
Result = false;
}
// Victron States
if (context.DESS_Mode == null)
{ ErrText += "context.DESS_Mode \r";
Result = false;
}
if (context.SoC_Now_Perc == null)
{ ErrText += "context.SoC_Now_Perc \r";
Result = false;
}
if (context.SoC_Min_Venus == null)
{ ErrText += "context.SoC_Min_Venus \r";
Result = false;
}
if (context.Max_Charge_Voltage_V == null)
{ ErrText += "context.Max_Charge_Voltage_V \r";
Result = false;
}
if (context.Max_Charge_Current_A == null)
{ ErrText += "context.Max_Charge_Current_A \r";
Result = false;
}
if (context.Grid_Setpoint_W == null)
{ ErrText += "context.Grid_Setpoint_W \r";
Result = false;
}
if (context.Charge_Disable == null)
{ ErrText += "context.Charge_Disable \r";
Result = false;
}
if (context.Solar_Charger == null)
{ ErrText += "context.Solar_Charger \r";
Result = false;
}
if (context.Relay_2 == null)
{ ErrText += "context.Relay_2 \r";
Result = false;
}
if (context.Grid_to_Batt_Limit_A == null)
{ ErrText += "Grid_to_Batt_Limit_A \r";
Result = false;
}
if (Grid_Setpoint_Manual_W != null)
{ ErrText += "Grid_Setpoint_Manual_W \r";
Result = false;
}
if (Solar_Forecast != null)
{ ErrText += "Solar_Forecast \r";
Result = false;
}
if (Solar_Sunrise != null)
{ ErrText += "Solar_Sunrise \r";
Result = false;
}
if (Solar_Sunset != null)
{ ErrText += "Solar_Sunset \r";
Result = false;
}
if (context.Max_Inverter_Power_W == null)
{ ErrText += "Max_Inverter_Power_W \r";
Result = false;
}
if (ErrText > "")
{ node.warn("Missing Data: \r" + ErrText);
}
return Result;
} // Verify_External_Data
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function GlobalGetAll()
{
let Result = true;
// Get general settings
SoC_Min_Manual_Perc = global.get("Victron_MESS.Manual.SoC_Min_Perc");
if (SoC_Min_Manual_Perc == null) {
node.warn("Victron_MESS.Manual.SoC_Min_Perc: null");
Result = false;
}
SoC_Min_Bottom_Perc = Math.min(15, SoC_Min_Manual_Perc);
Grid_Setpoint_Manual_W = global.get("Victron_MESS.Manual.Grid_Setpoint");
if (Grid_Setpoint_Manual_W == null) {
node.warn("Victron_MESS.Manual.Grid_Setpoint: null");
Result = false;
}
Consump_Forecast_Wh = global.get("Victron_MESS.Consump.Forecast");
if (Consump_Forecast_Wh == null) {
ErrorText += "Victron_MESS.Consump.Forecast: null" + "\r";
Result = false;
}
Solar_Forecast = global.get("Victron_MESS.Solar.Forecast.All");
if (Solar_Forecast == null) {
ErrorText += "Victron_MESS.Solar.Forecast.All: null" + "\r";
Result = false;
}
Solar_Forecast_Modified = global.get("Victron_MESS.Solar.Forecast.Modified");
if (Solar_Forecast_Modified == null) {
ErrorText += "Victron_MESS.Solar.Forecast.Modified: null" + "\r";
Result = false;
}
Solar_Sunrise = global.get("Victron_MESS.Solar.Sunrise");
if (Solar_Sunrise == null) {
ErrorText += "Victron_MESS.Solar.Sunrise: null" + "\r";
Result = false;
}
Solar_Sunset = global.get("Victron_MESS.Solar.Sunset");
if (Solar_Sunset == null) {
ErrorText += "Victron_MESS.Solar.Sunset: null" + "\r";
Result = false;
}
DayAhead_PriceArray_Ct = global.get("Victron_MESS.DayAhead.PriceArray_Ct");
if (DayAhead_PriceArray_Ct == null) {
ErrorText += "Victron_MESS.DayAhead.PriceArray_Ct: null" + "\r";
Result = false;
}
if (ErrorText > "")
{ node.warn("GlobalGetAll: " + ErrorText);
}
return Result;
} // GlobalGetAll
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function SolarSetSunriseAndSunsetHours()
{
// Find Sunrise and Sunset
if (TraceOn) node.warn("SolarSetSunriseAndSunsetHours...");
Sunrise_Hour = Number(Solar_Sunrise.substring(0,2)) + 1;
Sunset_Hour = Number(Solar_Sunset.substring(0,2));
if ( (context.Last_Solar_Sunrise != Solar_Sunrise)
|| (context.Last_Solar_Sunset != Solar_Sunset) )
{
AddDebug("Daylight: "
+ Solar_Sunrise + " - " + Solar_Sunset + "\r");
}
context.Last_Solar_Sunrise = Solar_Sunrise;
context.Last_Solar_Sunset = Solar_Sunset;
} // SolarSetSunriseAndSunsetHours
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function SolarMakeForecasts()
{
// Calculate Solar Forecast per Hour
if (TraceOn) node.warn("SolarMakeForecasts...");
for (let H = 0; H <= 23; H++) {
Solar_Today_Wh += Solar_Forecast_Modified[H];
}
// Add Remainder of current Hour
Solar_Remain_Wh +=
Round(Solar_Forecast_Modified[NowHour] * MinutesRemainFactor());
// Startvalue(s)
if ( IsZeroPrice(NowHour) ) {
Solar_Remain_Zero_Price_Wh = Solar_Remain_Wh;
}
else if ( IsVeryLowPrice(NowHour) ) {
Solar_Remain_VeryLow_Price_Wh = Solar_Remain_Wh;
}
else if ( IsAnyLowPrice(NowHour) ) {
Solar_Remain_Low_Price_Wh = Solar_Remain_Wh;
}
else {
Solar_Remain_Rest_Price_Wh = Solar_Remain_Wh;
}
for (let H = NowHour+1; H <= 23; H++) {
Solar_Remain_Wh += Solar_Forecast_Modified[H];
// what part of Solar has low price
if ( IsZeroPrice(H) ) {
Solar_Remain_Zero_Price_Wh += Solar_Forecast_Modified[H];
}
else if ( IsVeryLowPrice(H) ) {
Solar_Remain_VeryLow_Price_Wh += Solar_Forecast_Modified[H];
}
else if ( IsAnyLowPrice(H) ) {
Solar_Remain_Low_Price_Wh += Solar_Forecast_Modified[H];
}
else {
Solar_Remain_Rest_Price_Wh += Solar_Forecast_Modified[H];
}
} // Next Hour until midnight
if ( (Round(context.Last_Solar_Today_Wh / 100)
!= Round(Solar_Today_Wh / 100))
|| (Round(context.Last_Solar_Remain_Wh / 100)
!= Round(Solar_Remain_Wh / 100) ) )
{
AddDebug("Solar:");
if (Solar_Remain_Zero_Price_Wh > 0)
{
AddDebug(" zero: ");
AddDebug(WhAskWhText(Solar_Remain_Zero_Price_Wh));
}
if (Solar_Remain_VeryLow_Price_Wh > 0)
{
AddDebug(" very_low: ");
AddDebug(WhAskWhText(Solar_Remain_VeryLow_Price_Wh));
}
if (Solar_Remain_Low_Price_Wh > 0)
{
AddDebug(" low: ");
AddDebug(WhAskWhText(Solar_Remain_Low_Price_Wh));
}
if (Solar_Remain_Rest_Price_Wh > 0)
{
AddDebug(" rest: ");
AddDebug(WhAskWhText(Solar_Remain_Rest_Price_Wh));
}
AddDebug(" / ");
AddDebug(WhAskWhText(Solar_Today_Wh) + "\r");
context.Last_Solar_Today_Wh = Solar_Today_Wh;
context.Last_Solar_Remain_Wh = Solar_Remain_Wh;
}
for (let H = 24; H < Solar_Forecast.length; H++)
{
Solar_Tomorrow_Wh += Solar_Forecast_Modified[H];
}
if ( Round(context.Last_Solar_Tomorrow_Wh / 100)
!= Round(Solar_Tomorrow_Wh / 100) )
{
AddDebug("Solar Tomorrow: ");
AddDebug(WhAskWhText(Solar_Tomorrow_Wh));
AddDebug("\r");
context.Last_Solar_Tomorrow_Wh = Solar_Tomorrow_Wh;
}
} // SolarMakeForecasts
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function BatteryCalculateLevels()
{
// Calculate Expected Battery energy-levels
// without charging from Grid
if (TraceOn) node.warn("BatteryCalculateLevels...");
Batt_Full_Wh =
Round(Batt_Capacity_Wh * SoC_Full_Perc / 100);
Batt_Now_Wh =
Round(Batt_Capacity_Wh * context.SoC_Now_Perc / 100);
Batt_Free_Wh = Batt_Full_Wh - Batt_Now_Wh;
Batt_Min_Manual_Wh =
Round(Batt_Capacity_Wh * SoC_Min_Manual_Perc / 100);
Batt_Min_Bottom_Wh =
Round(Batt_Capacity_Wh * SoC_Min_Bottom_Perc / 100);
for (let H = 0; H < Solar_Forecast_Modified.length; H++) {
Batt_Forecast_Wh[H] = 0; // initialise
}
Batt_Forecast_Wh[NowHour] =
Math.min(Batt_Full_Wh, // full is full
Round( Batt_Now_Wh
+ Solar_Forecast_Modified[NowHour] * MinutesRemainFactor()
- Consump_Forecast_Wh[NowHour] * MinutesRemainFactor()
)
);
for (let H = NowHour+1; H < Solar_Forecast_Modified.length; H++)
{
if (H <= 23) { // today, modified Solar
Batt_Forecast_Wh[H] =
Math.min(Batt_Full_Wh, // full is full
Round(Batt_Forecast_Wh[H-1]
+ Solar_Forecast_Modified[H]
- Consump_Forecast_Wh[H] ) );
}
else { // tomorrow, forecast Solar
Batt_Forecast_Wh[H] =
Math.min(Batt_Full_Wh, // full is full
Round(Batt_Forecast_Wh[H-1]
+ Solar_Forecast[H]
- Consump_Forecast_Wh[H] ) );
}
} // calculate Battery Forecast per Hour
global.set("Victron_MESS.Battery.Forecast", Batt_Forecast_Wh);
let Batt_Low_Value = Batt_Forecast_Wh[NowHour];
let Batt_Low_Hour = Batt_Forecast_Wh.length;
let Batt_Low_Hour_Text = "";
let Batt_High_Value = Batt_Forecast_Wh[NowHour];
let Batt_High_Hour = Batt_Forecast_Wh.length;
let Batt_High_Hour_Text = "";
for (let H = Batt_Forecast_Wh.length; H >= NowHour; H--)
{
if (Batt_Forecast_Wh[H] <= Batt_Low_Value)
{
Batt_Low_Value = Batt_Forecast_Wh[H];
Batt_Low_Hour = H; // end of this hour
}
if (Batt_Forecast_Wh[H] >= Batt_High_Value)
{
Batt_High_Value = Batt_Forecast_Wh[H];
Batt_High_Hour = H; // end of this hour
}
}
Batt_Low_Hour_Text = HourAsText(Batt_Low_Hour) + ":59: ";
Batt_High_Hour_Text = HourAsText(Batt_High_Hour) + ":59: ";
if (context.Last_SoC_Min_Manual_Perc != SoC_Min_Manual_Perc)
{
AddDebug("Batt_Capacity: ");
AddDebug(WhAskWhText(Batt_Capacity_Wh) + "\r");
AddDebug("SoC_Perc_Min_Manual: ");
AddDebug(SoC_Min_Manual_Perc + "% (");
AddDebug(WhAskWhText(Batt_Min_Manual_Wh) + ")\r");
AddDebug("SoC_Perc_Min_Bottom: ");
AddDebug(SoC_Min_Bottom_Perc + "% (");
AddDebug(WhAskWhText(Batt_Min_Bottom_Wh) + ")\r");
context.Last_SoC_Min_Manual_Perc = SoC_Min_Manual_Perc;
}
if ( context.Last_SoC_Now_Perc != context.SoC_Now_Perc )
{
AddDebug("SoC_Perc_Now: ");
AddDebug(context.SoC_Now_Perc + "% (");
AddDebug(WhAskWhText(Batt_Now_Wh) + ")\r");
context.Last_SoC_Now_Perc = context.SoC_Now_Perc;
}
if ( Round(context.Last_Batt_Low_Value / 100)
!= Round(Batt_Low_Value / 100) )
{
AddDebug("Forecast_Low at ");
AddDebug(Batt_Low_Hour_Text);
AddDebug(WhAskWhText(Batt_Low_Value) + " (");
AddDebug(Round(Batt_Low_Value / Batt_Capacity_Wh * 100) + "%)" + "\r");
context.Last_Batt_Low_Value = Batt_Low_Value;
}
if ( Round(context.Last_Batt_High_Value / 100)
!= Round(Batt_High_Value / 100))
{
AddDebug("Forecast_High at ");
AddDebug(Batt_High_Hour_Text);
AddDebug(WhAskWhText(Batt_High_Value) + " (");
AddDebug(Round(Batt_High_Value / Batt_Capacity_Wh * 100) + "%)" + "\r");
context.Last_Batt_High_Value = Batt_High_Value;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - -
// Find Hour 'Horizon' that Battery can stay above
// Minimum without charging from Grid
// This is the last Hour that can be used to find
// High and Low Hours
for (let H = NowHour; H < Solar_Forecast_Modified.length; H++)
{
if (Batt_Forecast_Wh[H] < Batt_Min_Bottom_Wh)
{ break}
Forecast_Horizon_Hour = H;
}
if (context.Last_Forecast_Horizon_Hour != Forecast_Horizon_Hour)
{
AddDebug("Forecast_Horizon: ");
AddDebug(HourAsText(Forecast_Horizon_Hour));
AddDebug(" hr \r");
context.Last_Forecast_Horizon_Hour = Forecast_Horizon_Hour;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - -
} // BatteryCalculateLevels
// - - - - - - - - - - - - - - - - - - - - - - - - - -
function CalculateEnergyToFullSoC()
{
// What energy is needed from Grid to full SoC
// today at Sunset or possibly tomorrow
// this is usefull during daytime until sunset
if (TraceOn) node.warn("CalculateEnergyToFullSoC...");
let Energy_from_Grid_to_Full_at_Sunset_Today_Wh = 0;
let Energy_from_Grid_to_Full_at_Sunset_Tomorrow_Wh = 0;
let Energy_from_Grid_to_Stay_Above_Minimum_Wh = 0;
// See what charging is minimal needed to stay above minimum SoC
// This is probably usefull during the night, until sunrise.
let Batt_Level_Wh = Batt_Now_Wh;
Batt_Level_Wh =
Math.min(Batt_Level_Wh,
Round(Batt_Now_Wh
+ Solar_Forecast_Modified[NowHour] * MinutesRemainFactor()
- Consump_Forecast_Wh[NowHour] * MinutesRemainFactor()
)
);
let Next_Minimum_Hour = 0
for (Next_Minimum_Hour = NowHour+1;
Next_Minimum_Hour <= Forecast_Horizon_Hour;
Next_Minimum_Hour++)
{
Batt_Level_Wh =
Math.min(Batt_Level_Wh,
Round(Batt_Level_Wh
+ Solar_Forecast_Modified[Next_Minimum_Hour]
- Consump_Forecast_Wh[Next_Minimum_Hour]
)
);
if (Solar_Forecast_Modified[Next_Minimum_Hour]
> Consump_Forecast_Wh[Next_Minimum_Hour])
{
break;
}
}
Energy_from_Grid_to_Stay_Above_Minimum_Wh =
Math.max(0, Batt_Min_Manual_Wh - Batt_Level_Wh);
if ( Round(context.Last_Energy_from_Grid_to_Stay_Above_Minimum_Wh / 100)
!= Round(Energy_from_Grid_to_Stay_Above_Minimum_Wh / 100) )
{
AddDebug("Grid: Stay Above Minimum: "
+ WhAskWhText(Energy_from_Grid_to_Stay_Above_Minimum_Wh) + "\r");
context.Last_Energy_from_Grid_to_Stay_Above_Minimum_Wh
= Energy_from_Grid_to_Stay_Above_Minimum_Wh;
}
Energy_from_Grid_to_Full_at_Sunset_Today_Wh = Batt_Full_Wh;
for (let H = NowHour; H < Sunset_Hour; H++) {
Energy_from_Grid_to_Full_at_Sunset_Today_Wh =
Math.min(Energy_from_Grid_to_Full_at_Sunset_Today_Wh,
Batt_Full_Wh - Batt_Forecast_Wh[H]);
}
Energy_from_Grid_to_Full_at_Sunset_Today_Wh =
Math.max(0, Energy_from_Grid_to_Full_at_Sunset_Today_Wh);
if (context.Last_Energy_from_Grid_to_Full_at_Sunset_Today_Wh
!= Energy_from_Grid_to_Full_at_Sunset_Today_Wh)
{
AddDebug("Grid: Full at Sunset Today: "
+ WhAskWhText(Energy_from_Grid_to_Full_at_Sunset_Today_Wh) + "\r");
context.Last_Energy_from_Grid_to_Full_at_Sunset_Today_Wh =
Energy_from_Grid_to_Full_at_Sunset_Today_Wh;
}
Energy_from_Grid_to_Full_at_Sunset_Tomorrow_Wh = Batt_Full_Wh;
for (let H = NowHour; H < Sunset_Hour+24; H++) {
Energy_from_Grid_to_Full_at_Sunset_Tomorrow_Wh =
Math.min(Energy_from_Grid_to_Full_at_Sunset_Tomorrow_Wh,
Batt_Full_Wh - Batt_Forecast_Wh[H]);
}
Energy_from_Grid_to_Full_at_Sunset_Tomorrow_Wh =
Math.max(0, Energy_from_Grid_to_Full_at_Sunset_Tomorrow_Wh);
if (context.Last_Energy_from_Grid_to_Full_at_Sunset_Tomorrow_Wh
!= Energy_from_Grid_to_Full_at_Sunset_Tomorrow_Wh)
{
AddDebug("Grid: Full at Sunset Tomorrow: "
+ WhAskWhText(Energy_from_Grid_to_Full_at_Sunset_Tomorrow_Wh) + "\r");
context.Last_Energy_from_Grid_to_Full_at_Sunset_Tomorrow_Wh =
Energy_from_Grid_to_Full_at_Sunset_Tomorrow_Wh;
}
Energy_from_Grid_Needed_Wh =
Math.max(Energy_from_Grid_to_Stay_Above_Minimum_Wh,
Energy_from_Grid_to_Full_at_Sunset_Today_Wh,
Energy_from_Grid_to_Full_at_Sunset_Tomorrow_Wh);
} // CalculateEnergyToFullSoC
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function IsNegativePrice(p_Hour)
{
return ( IsInArrayOfPairs(context.NegativePriceArray, [p_Hour]) )
} // IsNegativePrice
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function IsZeroPrice(p_Hour)
{
return ( IsInArrayOfPairs(context.ZeroPriceArray, [p_Hour]) )
} // IsZeroPrice
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function IsVeryLowPrice(p_Hour)
{
return ( IsInArrayOfPairs(context.VeryLowPriceArray, [p_Hour]) )
} // IsVeryLowPrice
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function IsAnyLowPrice(p_Hour)
{
let Result = false;
if (IsVeryLowPrice(p_Hour) )
{ Result = true }
else if (IsZeroPrice(p_Hour) )
{ Result = true }
else if (IsNegativePrice(p_Hour) )
{ Result = true }
else if ( IsInArrayOfPairs(context.LowPriceArray, [p_Hour]) )
{ Result = true }
return Result;
} // IsAnyLowPrice
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function IsHighPrice(p_Hour)
{
return ( IsInArrayOfPairs(context.HighPriceArray, [p_Hour]) )
} // IsHighPrice
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function NegativePriceHourByIdx(p_Idx)
{
let Result = -1;
if (context.NegativePriceArray[p_Idx] != null)
{
Result = context.NegativePriceArray[p_Idx][0];
}
return Result;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function NegativePriceIdxByHour(p_Hour)
{
let Result = -1;
for (let H = 0; H < context.NegativePriceArray.length; H++)
{
if (context.NegativePriceArray[H][0] == p_Hour)
{
Result = H;
break;
}
}
return Result;
} // NegativePriceIdxByHour
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function ZeroPriceHourByIdx(p_Idx)
{
let Result = -1;
if (context.ZeroPriceArray[p_Idx] != null)
{
Result = context.ZeroPriceArray[p_Idx][0];
}
return Result;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function ZeroPriceIdxByHour(p_Hour)
{
let Result = -1;
for (let H = 0; H < context.ZeroPriceArray.length; H++)
{
if (context.ZeroPriceArray[H][0] == p_Hour)
{
Result = H;
break;
}
}
return Result;
} // ZeroPriceIdxByHour
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function LowPriceHourByIdx(p_Idx)
{
let Result = -1;
if (context.LowPriceArray[p_Idx] != null)
{
Result = context.LowPriceArray[p_Idx][0];
}
return Result;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function LowPriceIdxByHour(p_Hour)
{
let Result = -1;
for (let H = 0; H < context.LowPriceArray.length; H++)
{
if (context.LowPriceArray[H][0] == p_Hour)
{
Result = H;
break;
}
}
return Result;
} // LowPriceIdxByHour
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function VeryLowPriceHourByIdx(p_Idx)
{
let Result = -1;
if (context.VeryLowPriceArray[p_Idx] != null)
{
Result = context.VeryLowPriceArray[p_Idx][0];
}
return Result;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function VeryLowPriceIdxByHour(p_Hour)
{
let Result = -1;
for (let H = 0; H < context.VeryLowPriceArray.length; H++)
{
if (context.VeryLowPriceArray[H][0] == p_Hour)
{
Result = H;
break;
}
}
return Result;
} // VeryLowPriceIdxByHour
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function FindCheapChargeHour()
{
if (TraceOn) node.warn("FindCheapChargeHour...");
for (let H = NowHour; H <= Forecast_Horizon_Hour; H++)
{
if ( IsNegativePrice(H) )
{
Next_Cheap_Charge_Hour = H;
Next_Cheap_Charge_Price_Ct =
DayAhead_PriceArray_Ct[Next_Cheap_Charge_Hour];
break;
}
else if ( IsZeroPrice(H) )
{
Next_Cheap_Charge_Hour = H;
Next_Cheap_Charge_Price_Ct =
DayAhead_PriceArray_Ct[Next_Cheap_Charge_Hour];
break;
}
else if ( IsVeryLowPrice(H) )
{
Next_Cheap_Charge_Hour = H;
Next_Cheap_Charge_Price_Ct =
DayAhead_PriceArray_Ct[Next_Cheap_Charge_Hour];
break;
}
else if (DayAhead_PriceArray_Ct[H]
< Next_Cheap_Charge_Price_Ct)
{
Next_Cheap_Charge_Hour = H;
Next_Cheap_Charge_Price_Ct =
DayAhead_PriceArray_Ct[Next_Cheap_Charge_Hour];
}
} // try all hours
// See if two, resp. three or more hours are needed to charge
// possible start one hour earlier with charging
if ( (context.SoC_Now_Perc < 70)
&& (Next_Cheap_Charge_Hour > NowHour)
&& (context.NegativePriceArray.length <= 1)
&& (context.ZeroPriceArray.length <= 1)
&& (context.VeryLowPriceArray.length <= 1)
&& (IsAnyLowPrice(Next_Cheap_Charge_Hour-1) )
)
{
Next_Cheap_Charge_Hour -= 1; // one hour earlier
Next_Cheap_Charge_Price_Ct =
DayAhead_PriceArray_Ct[Next_Cheap_Charge_Hour];
}
if ( (context.SoC_Now_Perc < 40)
&& (Next_Cheap_Charge_Hour > NowHour)
&& (context.NegativePriceArray.length <= 2)
&& (context.ZeroPriceArray.length <= 2)
&& (context.VeryLowPriceArray.length <= 2)
&& (IsAnyLowPrice(Next_Cheap_Charge_Hour-1) )
)
{
Next_Cheap_Charge_Hour -= 1; // another hour earlier
Next_Cheap_Charge_Price_Ct =
DayAhead_PriceArray_Ct[Next_Cheap_Charge_Hour];
}
if (context.Last_Next_Cheap_Charge_Hour != Next_Cheap_Charge_Hour)
{
AddDebug("Next_Cheap_Charge_Hour: ");
AddDebug(HourAsText(Next_Cheap_Charge_Hour));
AddDebug(" = ");
AddDebug(Next_Cheap_Charge_Price_Ct + " ct");
AddDebug("\r");
context.Last_Next_Cheap_Charge_Hour = Next_Cheap_Charge_Hour;
}
} // FindCheapChargeHour
// - - - - - - - - - - - - - - - - - - - - - - - - - -
function FindHighSellHour()
{
if (TraceOn) node.warn("FindHighSellHour...");
for (let H = NowHour; H < Next_Cheap_Charge_Hour; H++)
{
if (DayAhead_PriceArray_Ct[H] * Conversion_Loss_Factor >
( Next_Cheap_Charge_Price_Ct / Conversion_Loss_Factor
+ Sell_Price_Limit_Ct)
)
{
if (DayAhead_PriceArray_Ct[H] > Next_High_Sell_Price_Ct)
{
Next_High_Sell_Hour = H;
Next_High_Sell_Price_Ct = DayAhead_PriceArray_Ct[H];
}
}
}
if (context.Last_Next_High_Sell_Hour != Next_High_Sell_Hour)
{
AddDebug("Next_High_Sell_Hour: ");
AddDebug(HourAsText(Next_High_Sell_Hour) + " hr = ");
AddDebug(Next_High_Sell_Price_Ct);
AddDebug(" ct\r");
context.Last_Next_High_Sell_Hour = Next_High_Sell_Hour;
}
} // FindHighSellHour
// - - - - - - - - - - - - - - - - - - - - - - - - - -
function FindEnergyToSell()
{
// best, discharge at high prices, but not to much
if (TraceOn) node.warn("FindEnergyToSell...");
// This is now available
Energy_to_Sell_Wh =
Batt_Capacity_Wh * context.SoC_Now_Perc / 100;
// a minimum should remain
Energy_to_Sell_Wh -=
Math.floor(Batt_Full_Wh * SoC_Min_Manual_Perc / 100);
// Minus Consumption plus Solar
if (Next_Cheap_Charge_Hour > NowHour)
{
// This hour proportionally
Energy_to_Sell_Wh -=
Consump_Forecast_Wh[NowHour] * MinutesRemainFactor()
// Changes in the next hours
for (let H = NowHour+1; H < Next_Cheap_Charge_Hour; H++)
{
Energy_to_Sell_Wh =
Math.min(Energy_to_Sell_Wh,
Energy_to_Sell_Wh
- Consump_Forecast_Wh[H]
+ Solar_Forecast_Modified[H]);
}
// before Solar is ready, consumption comes first
Energy_to_Sell_Wh -= Consump_Forecast_Wh[Next_Cheap_Charge_Hour];
// Value can't be negative
Energy_to_Sell_Wh =
Math.max(0, Energy_to_Sell_Wh);
// node.warn(Energy_to_Sell_Wh);
if (context.Last_Energy_to_Sell_Wh != Energy_to_Sell_Wh)
{
AddDebug("Energy Available to Sell: ");
AddDebug(WhAskWhText(Energy_to_Sell_Wh) + "\r");
context.Last_Energy_to_Sell_Wh = Energy_to_Sell_Wh;
}
} // if there comes a Low Charge Hour
} // FindEnergyToSell
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function NegativePriceComes()
{
if (TraceOn) node.warn("NegativePriceComes...");
let Result = false;
if ( IsNegativePrice(NowHour) )
{
Result = false;
}
else if (IsInArrayOfPairs(context.NegativePriceArray,
[ NowHour+1, NowHour+2, NowHour+3, NowHour+4,
NowHour+5, NowHour+6, NowHour+7, NowHour+8,
NowHour+9, NowHour+10, NowHour+11, NowHour+12] )
)
{
Result = true;
}
return Result
} // NegativePriceComes
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function ZeroPriceComes()
{
if (TraceOn) node.warn("ZeroPriceComes...");
let Result = false;
if ( IsZeroPrice(NowHour) )
{
Result = false;
}
else if (IsInArrayOfPairs(context.ZeroPriceArray,
[ NowHour+1, NowHour+2, NowHour+3, NowHour+4,
NowHour+5, NowHour+6, NowHour+7, NowHour+8,
NowHour+9, NowHour+10, NowHour+11, NowHour+12] )
)
{
Result = true;
}
return Result
} // ZeroPriceComes
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function VeryLowPriceComes()
{
if (TraceOn) node.warn("VeryLowPriceComes...");
let Result = false;
if ( IsVeryLowPrice(NowHour) )
{
Result = false;
}
else if (IsInArrayOfPairs(context.VeryLowPriceArray,
[ NowHour+1, NowHour+2, NowHour+3, NowHour+4,
NowHour+5, NowHour+6, NowHour+7, NowHour+8,
NowHour+9, NowHour+10, NowHour+11, NowHour+12] )
)
{
Result = true;
}
return Result
} // VeryLowPriceComes
// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
function SetSolarAvailable()
{
if (TraceOn) node.warn("SetSolarAvailable...");
let Result = false;
DayAheadRule = "\rSolar";
if (context.Energy_from_Solar_Now_W > 0)
{
Solar_To_Batt_Perc = Solar_Charge_Perc; // max perc to use for charging
if (Solar_Remain_Zero_Price_Wh
+ Solar_Remain_VeryLow_Price_Wh
+ Solar_Remain_Low_Price_Wh > Batt_Free_Wh)
{ // Low Price Solar is enough for charging
if ( Not(IsAnyLowPrice(NowHour)) )
{ // Now is NOT a low price
Solar_To_Batt_Perc = 2; // use only x% of Solar
}
} // if Solar Low Price is enough
DayAheadRule += ", use " + Solar_To_Batt_Perc + "%";
Result = true;
} // if Solar available
Batt_Max_Current_Solar_A =
Round( Solar_To_Batt_Perc / 100
* context.Energy_from_Solar_Now_W
/ context.Max_Charge_Voltage_V);
DayAheadRule += " Remain: " + WhAskWhText(Solar_Remain_Wh);
DayAheadRule += ", +:" + WhAskWhText(Solar_Tomorrow_Wh);
DayAheadRule += ".";
if (TraceOn) node.warn("SetSolarAvailable: " + Result);
return Result;
} // SetSolarAvailable
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function TryRule_DESS()
{
if (TraceOn) node.warn("TryRule_DESS...");
let Result = false;
if (context.DESS_Mode != 0)
{
DayAheadRule += " DESS Mode " + context.DESS_Mode + ".";
SoC_Min_Target_Perc = SoC_Min_Manual_Perc;
// Batt_Max_Current_Grid_A = -1;
// Batt_Max_Current_Solar_A = 0;
Result = true;
}
if (TraceOn) node.warn("TryRule_DESS: " + Result);
return Result;
} // TryRule_DESS
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function TryRule_FinishLowSoCCharging()
{
if (TraceOn) node.warn("TryRule_FinishLowSoCCharging...");
let Result = false;
if (NowInSeconds < context.Batt_Grid_End_Time)
{
DayAheadRule += " Finish Low SoC Charging.";
SoC_Min_Target_Perc = SoC_Min_Bottom_Perc;
Result = true;
} // Finish Low SoC Charging
if (TraceOn) node.warn("TryRule_FinishLowSoCCharging: " + Result);
return Result;
} // TryRule_FinishLowSoCCharging
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function TryRule_SoCBelowBottom()
{
if (TraceOn) node.warn("TryRule_SoCBelowBottom...");
let Result = false;
// SoC is below ultimate minimum
if (context.SoC_Now_Perc < SoC_Min_Bottom_Perc)
{
DayAheadRule += " SoC much to low ("
+ context.SoC_Now_Perc + "<" + SoC_Min_Bottom_Perc + ").";
SoC_Min_Target_Perc = SoC_Min_Bottom_Perc;
// Charge at least 5 minutes
context.Batt_Grid_End_Time = NowInSeconds + 300;
DayAheadRule += " Charge from Grid.";
Result = true;
} // SoC is below ultimate minimum
if (TraceOn) node.warn("TryRule_SoCBelowBottom: " + Result);
return Result;
} // TryRule_SoCBelowBottom
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function TryRule_SellHighPrice()
{
if (TraceOn) node.warn("TryRule_SellHighPrice...");
let Result = false;
// possible discharge for high price
// and charge later for low price
if ( (NowHour == Next_High_Sell_Hour)
&& (Energy_to_Sell_Wh > 0)
)
{
DayAheadRule += " High Sell Hour.";
// do not charge
Batt_Max_Current_Solar_A = 0;
Batt_Max_Current_Grid_A = 0;
Batt_Charger_Only_Target = false;
DayAheadRule += " Sell ";
DayAheadRule += WhAskWhText(Energy_to_Sell_Wh);
DayAheadRule += " at ";
DayAheadRule += Round(DayAhead_PriceArray_Ct[NowHour], 1);
DayAheadRule += " ct. ";
DayAheadRule += " Re-charge after ";
DayAheadRule += HourAsText(Next_Cheap_Charge_Hour);
DayAheadRule += " hr. ";
DayAheadRule += " at ";
DayAheadRule += Round(Next_Cheap_Charge_Price_Ct, 1);
DayAheadRule += " ct. ";
Grid_Setpoint_Unload_W =
-Math.max(200,
Math.min(Grid_Setpoint_W_Limit,
Round(Energy_to_Sell_Wh / MinutesRemainFactor() )
)
); // note the minus-sign: negative value
Result = true;
} // Discharge at High price for later Charge at Low price
if (TraceOn) node.warn("TryRule_SellHighPrice: " + Result);
return Result;
} // TryRule_SellHighPrice
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
[ Voor 220% gewijzigd door MJ de Bruijn op 14-05-2024 14:26 ]
Een mens leert van zijn fouten. Ik heb in mijn leven heel veel geleerd.
:no_upscale():strip_icc():fill(white):strip_exif()/f/image/sQJLwyb0ouZjqCTkJcNUnoxi.jpg?f=user_large)
:no_upscale():strip_icc():fill(white):strip_exif()/f/image/LnDDmR9xBaAgyKCelfmKNv9E.jpg?f=user_large)
/f/image/Ko9vIrhocStBmo17uKFVGHqW.png?f=fotoalbum_large)
:strip_exif()/f/image/05bw6pDQqmwoly4fqSYEwofe.jpg?f=fotoalbum_large)
:no_upscale():strip_icc():strip_exif()/f/image/5EXe0t38n6kk190jgz7uViU1.jpg?f=user_large)
:strip_exif()/f/image/hD2qhSCsDTNG70OxVEAwEond.jpg?f=fotoalbum_large)
:strip_exif()/f/image/TwYhrFRH6rB2tqckfqZOLl2A.jpg?f=fotoalbum_large)
/f/image/SjSKHtDASwheNOHkw2PZiMmn.png?f=fotoalbum_large)
:no_upscale():strip_icc():strip_exif()/f/image/xyXKYMgl7K6SmoYxvJ4HY6E1.jpg?f=user_large)
:no_upscale():strip_icc():strip_exif()/f/image/LVa9gHDSLzQ5yEysbeMGbrCk.jpg?f=user_large)
:strip_exif()/f/image/a6a7OroQ1039yVU6Onz3iY7f.jpg?f=fotoalbum_large)
:strip_exif()/f/image/VjlA6BB7yEImosuOrHlZVKIc.jpg?f=fotoalbum_large)
:strip_exif()/f/image/D9RHerghVVUWbkaIY6uHa37I.jpg?f=fotoalbum_large)
:strip_exif()/f/image/tgBmeQyYB0GE1fKIQxrYJBK1.jpg?f=fotoalbum_large)
:strip_exif()/f/image/pjkRGk4dJPK2PP1elrvek2XZ.jpg?f=fotoalbum_large)
:strip_exif()/f/image/IjiS2CJZnM4I0uV0jJ08uCPX.jpg?f=fotoalbum_large)
:strip_exif()/f/image/ZiZEFKgpI9g4sqthaUSK2v6u.jpg?f=fotoalbum_large)
:strip_exif()/f/image/ZvwLNCq2WvL8eZkeK1bZen0w.jpg?f=fotoalbum_large)
:strip_exif()/f/image/sVWNtBdHOmxRxIsEFNsNkdYB.jpg?f=fotoalbum_large)
:strip_exif()/f/image/kxUDQJeTKYTfxRdecsuv5T6X.jpg?f=fotoalbum_large)
:strip_exif()/f/image/ObLPUdwCUPsmAF5XtumCWofi.jpg?f=fotoalbum_large)
/f/image/iZAsxYJhraXJpAucsZ6qPApQ.png?f=fotoalbum_large)
/f/image/if2unDHNcArUtzYy1WcZ54EI.png?f=fotoalbum_large)
/f/image/NMfr0k7CbBbPqCeHDtKrOc4H.png?f=fotoalbum_large)
/f/image/p4gxIQYz73ow65HbsrdHswDq.png?f=fotoalbum_large)
/f/image/gjOXLXxYvNbsKK6kf1QvnYZZ.png?f=fotoalbum_large)
/f/image/680BJR1FKBmnRtcYy1jzKeIW.png?f=fotoalbum_large)
:strip_exif()/f/image/FEV1vz77F0ARwA9ppx6gZlhF.png?f=user_large)
/f/image/fdPZvwFWzEwuelx2Tje5oPqs.png?f=fotoalbum_large)
:strip_exif()/f/image/hLgRIoldrvCv0n15j2eLfgqH.png?f=user_large)
/f/image/mFCKdQkdyhqsDNz9AVTRHUSY.png?f=fotoalbum_large)
:strip_exif()/f/image/JWDqk7IrosyAahkgD9blQ9Ri.jpg?f=fotoalbum_large)
/f/image/VhbNbM2WGFd2w5FUyXIQ7kqb.png?f=fotoalbum_large)
/f/image/nRuyEtrNQbxrPO7vtXa0lsPq.png?f=fotoalbum_large)
/f/image/g63tEQXhORtyHKVhOdJclMPQ.png?f=fotoalbum_large)