Add balanced energy values

Add balanced energy values (#16346)
This commit is contained in:
Theo Arends 2022-08-26 14:35:52 +02:00
parent 4923755afc
commit 91875f20a5
2 changed files with 18 additions and 2 deletions

View File

@ -187,6 +187,8 @@
#define D_JSON_TEMPERATURE_UNIT "TempUnit"
#define D_JSON_TIME "Time"
#define D_JSON_TODAY "Today"
#define D_JSON_TODAY_SUM_EXPORT "TodaySumExport"
#define D_JSON_TODAY_SUM_IMPORT "TodaySumImport"
#define D_JSON_TOTAL "Total"
#define D_JSON_TOTAL_USAGE "TotalUsage"
#define D_JSON_TOTAL_REACTIVE "TotalReactive"

View File

@ -87,6 +87,8 @@ struct ENERGY {
float daily_sum; // 123.123 kWh
float total_sum; // 12345.12345 kWh total energy
float yesterday_sum; // 123.123 kWh
float daily_sum_import_balanced; // 123.123 kWh
float daily_sum_export_balanced; // 123.123 kWh
int32_t kWhtoday_delta[ENERGY_MAX_PHASES]; // 1212312345 Wh 10^-5 (deca micro Watt hours) - Overflows to Energy.kWhtoday (HLW and CSE only)
int32_t kWhtoday_offset[ENERGY_MAX_PHASES]; // 12312312 Wh * 10^-2 (deca milli Watt hours) - 5764 = 0.05764 kWh = 0.058 kWh = Energy.daily
@ -238,10 +240,12 @@ void EnergyUpdateToday(void) {
Energy.total_sum = 0.0f;
Energy.yesterday_sum = 0.0f;
Energy.daily_sum = 0.0f;
int32_t delta_sum_balanced = 0;
for (uint32_t i = 0; i < Energy.phase_count; i++) {
if (abs(Energy.kWhtoday_delta[i]) > 1000) {
int32_t delta = Energy.kWhtoday_delta[i] / 1000;
delta_sum_balanced += delta;
Energy.kWhtoday_delta[i] -= (delta * 1000);
Energy.kWhtoday[i] += delta;
if (delta < 0) { // Export energy
@ -261,6 +265,12 @@ void EnergyUpdateToday(void) {
Energy.daily_sum += Energy.daily[i];
}
if (delta_sum_balanced > 0) {
Energy.daily_sum_import_balanced += (float) delta_sum_balanced / 100000;
} else {
Energy.daily_sum_export_balanced += (float) abs(delta_sum_balanced) / 100000;
}
if (RtcTime.valid){ // We calc the difference only if we have a valid RTC time.
uint32_t energy_diff = (uint32_t)(Energy.total_sum * 100000) - RtcSettings.energy_usage.last_usage_kWhtotal;
@ -353,6 +363,8 @@ void Energy200ms(void)
RtcSettings.energy_kWhtoday_ph[i] = 0;
Energy.start_energy[i] = 0;
// Energy.kWhtoday_delta = 0; // dont zero this, we need to carry the remainder over to tomorrow
Energy.daily_sum_import_balanced = 0.0;
Energy.daily_sum_export_balanced = 0.0;
}
EnergyUpdateToday();
#if defined(USE_ENERGY_MARGIN_DETECTION) && defined(USE_ENERGY_POWER_LIMIT)
@ -1185,8 +1197,10 @@ void EnergyShow(bool json) {
*/
if (!isnan(Energy.export_active[0])) {
ResponseAppend_P(PSTR(",\"" D_JSON_EXPORT_ACTIVE "\":%s"),
EnergyFormat(value_chr, Energy.export_active, Settings->flag2.energy_resolution));
ResponseAppend_P(PSTR(",\"" D_JSON_TODAY_SUM_IMPORT "\":%s,\"" D_JSON_TODAY_SUM_EXPORT "\":%s,\"" D_JSON_EXPORT_ACTIVE "\":%s"),
EnergyFormat(value_chr, &Energy.daily_sum_import_balanced, Settings->flag2.energy_resolution, 1),
EnergyFormat(value2_chr, &Energy.daily_sum_export_balanced, Settings->flag2.energy_resolution, 1),
EnergyFormat(value3_chr, Energy.export_active, Settings->flag2.energy_resolution));
if (energy_tariff) {
ResponseAppend_P(PSTR(",\"" D_JSON_EXPORT D_CMND_TARIFF "\":%s"),
EnergyFormat(value_chr, energy_return, Settings->flag2.energy_resolution, 6));