Update energy usage calculation to fix remaining tests

pull/25233/head
Petar Petrov 2025-04-30 08:44:04 +03:00
parent 92353ebed5
commit eefbfd98b3
2 changed files with 23 additions and 15 deletions

View File

@ -1031,15 +1031,21 @@ export const computeConsumptionSingle = (data: {
let solar_to_grid = 0; let solar_to_grid = 0;
let used_battery = 0; let used_battery = 0;
let used_grid = 0; let used_grid = 0;
if ((to_grid != null || to_battery != null) && solar != null) { if (solar == null) {
if (to_battery != null) {
grid_to_battery = to_battery;
}
if (to_grid != null) {
battery_to_grid = to_grid;
}
} else if (to_grid != null || to_battery != null) {
used_solar = (solar || 0) - (to_grid || 0) - (to_battery || 0); used_solar = (solar || 0) - (to_grid || 0) - (to_battery || 0);
if (used_solar < 0) { if (used_solar < 0) {
if (to_battery != null) { if (to_battery != null) {
grid_to_battery = used_solar * -1; grid_to_battery = Math.min(used_solar * -1, from_grid || 0, to_battery);
if (grid_to_battery > (from_grid || 0)) {
battery_to_grid = grid_to_battery - (from_grid || 0);
grid_to_battery = from_grid || 0;
} }
if (to_grid != null) {
battery_to_grid = Math.min(to_grid - solar, from_battery || 0, to_grid);
} }
used_solar = 0; used_solar = 0;
} }

View File

@ -220,7 +220,6 @@ describe("Energy Usage Calculation Tests", () => {
} }
); );
}); });
/* Fails
it("Charging and discharging the battery to/from the grid in the same interval.", () => { it("Charging and discharging the battery to/from the grid in the same interval.", () => {
assert.deepEqual( assert.deepEqual(
computeConsumptionSingle({ computeConsumptionSingle({
@ -234,12 +233,13 @@ describe("Energy Usage Calculation Tests", () => {
grid_to_battery: 3, grid_to_battery: 3,
battery_to_grid: 1, battery_to_grid: 1,
used_solar: 0, used_solar: 0,
used_grid: 1, used_grid: 2,
used_battery: 0, used_battery: 0,
solar_to_battery: 0,
solar_to_grid: 0,
} }
); );
}); */ });
/* Test does not pass, battery is not really correct when solar is not present
it("Charging the battery with no solar sensor.", () => { it("Charging the battery with no solar sensor.", () => {
assert.deepEqual( assert.deepEqual(
computeConsumptionSingle({ computeConsumptionSingle({
@ -255,10 +255,11 @@ describe("Energy Usage Calculation Tests", () => {
used_solar: 0, used_solar: 0,
used_grid: 2, used_grid: 2,
used_battery: 0, used_battery: 0,
solar_to_battery: 0,
solar_to_grid: 0,
} }
); );
}); */ });
/* Test does not pass
it("Discharging battery to grid while also consuming from grid.", () => { it("Discharging battery to grid while also consuming from grid.", () => {
assert.deepEqual( assert.deepEqual(
computeConsumptionSingle({ computeConsumptionSingle({
@ -274,10 +275,11 @@ describe("Energy Usage Calculation Tests", () => {
used_solar: 0, used_solar: 0,
used_grid: 5, used_grid: 5,
used_battery: 0, used_battery: 0,
solar_to_battery: 0,
solar_to_grid: 0,
} }
); );
}); });
*/
it("Grid, solar, and battery", () => { it("Grid, solar, and battery", () => {
assert.deepEqual( assert.deepEqual(
@ -352,7 +354,6 @@ describe("Energy Usage Calculation Tests", () => {
solar_to_grid: 7, solar_to_grid: 7,
} }
); );
/* Test does not pass
assert.deepEqual( assert.deepEqual(
computeConsumptionSingle({ computeConsumptionSingle({
from_grid: 5, from_grid: 5,
@ -367,8 +368,9 @@ describe("Energy Usage Calculation Tests", () => {
used_solar: 0, used_solar: 0,
used_grid: 5, used_grid: 5,
used_battery: 0, used_battery: 0,
solar_to_battery: 0,
solar_to_grid: 1,
} }
); );
*/
}); });
}); });