-
-
Save luuuis/17398da94f9be8c024c3905eb37a296e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function calculateDivert ({ | |
now, // current Date | |
cycle_surplus_Wh, // accumulated surplus (feed in - consumption) in this cycle | |
cycle_feed_in_Wh, // accumulated feed-in in this cycle | |
cycle_minutes, // minutes in a cycle | |
load_kW, // load consumption | |
load_status, // load on/off status | |
max_demand_kW, // max provisioned demand in kW | |
grid_consumption_kW // current consumption from grid | |
}) { | |
const cycle_seconds = cycle_minutes * 60 | |
const remaining_seconds = cycle_seconds - (now.getMinutes() * 60 + now.getSeconds()) % cycle_seconds | |
const surplus_kWh = cycle_surplus_Wh / 1000 | |
const feed_in_kWh = cycle_feed_in_Wh / 1000 | |
const load_per_second_kWh = load_kW / 60 / 60 | |
const elapsed_seconds = cycle_seconds - remaining_seconds | |
const feed_in_per_second_kWh = feed_in_kWh / elapsed_seconds | |
return { | |
debug: { | |
now, | |
cycle_surplus_Wh, | |
cycle_feed_in_Wh, | |
cycle_minutes, | |
load_kW, | |
heater_status: load_status, | |
grid_consumption_kW, | |
cycle_seconds, | |
remaining_seconds, | |
surplus_kWh, | |
feed_in_kWh, | |
load_per_second_kWh, | |
elapsed_seconds, | |
feed_in_per_second_kWh, | |
}, | |
enabled: (() => { | |
if (load_status === 'on') { | |
// keep on until feed-in has been drained | |
// @todo predict if surplus would go negative before end of cycle | |
return grid_consumption_kW < max_demand_kW && cycle_surplus_Wh > 0 | |
} else if (remaining_seconds < 30) { | |
// don't bother cycling the relay | |
return false | |
} else { | |
// turn on as late as possible within a cycle | |
return (grid_consumption_kW + load_kW) < max_demand_kW && | |
surplus_kWh >= remaining_seconds * (load_per_second_kWh - feed_in_per_second_kWh) | |
} | |
})() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment