Skip to content

Commit

Permalink
solar_forecast, front_panel, heating: Fix Wh vs kWh mixup
Browse files Browse the repository at this point in the history
The forecast.solar api returns Wh, but the internal API assumed kWh
  • Loading branch information
borg42 committed Sep 20, 2024
1 parent 9f446f4 commit f9fad3a
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 37 deletions.
48 changes: 24 additions & 24 deletions software/src/modules/front_panel/front_panel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -438,11 +438,11 @@ int FrontPanel::update_front_page_solar_forecast(const uint8_t index, const Tile
switch (param) {
case SFType::ForecastToday:
str1 = "Heute";
kwh = solar_forecast.get_kwh_today();
kwh = solar_forecast.get_wh_today();
break;
case SFType::ForecastTomorrow:
str1 = "Morgen";
kwh = solar_forecast.get_kwh_tomorrow();
kwh = solar_forecast.get_wh_tomorrow();
break;
}

Expand Down Expand Up @@ -607,39 +607,39 @@ void FrontPanel::update()
update_led();
}

String FrontPanel::watt_value_to_display_string(const int32_t watt)
String FrontPanel::watt_value_to_display_string(const int32_t w)
{
if (watt < 10000) {
return String(watt) + " W";
} else if (watt < (1000*1000)) {
uint32_t kw = watt / 1000;
if (w < 10000) {
return String(w) + " W";
} else if (w < (1000*1000)) {
uint32_t kw = w / 1000;
return String(kw) + " kW";
} else if (watt < (1000*1000*1000)) {
uint32_t mw = watt / (1000*1000);
} else if (w < (1000*1000*1000)) {
uint32_t mw = w / (1000*1000);
return String(mw) + " MW";
} else {
return String(">1GW");
}
}

String FrontPanel::watt_hour_value_to_display_string(const uint32_t kilo_watt_hour)
String FrontPanel::watt_hour_value_to_display_string(const uint32_t wh)
{
if (kilo_watt_hour < 1000) {
return String(kilo_watt_hour) + "kWh";
} else if (kilo_watt_hour < (1000*10)) {
uint32_t mwh = kilo_watt_hour / 1000;
return String(mwh/10) + "." + String(mwh%10) + "MWh";
} else if (kilo_watt_hour < (1000*1000)) {
uint32_t mwh = kilo_watt_hour / 1000;
if (wh < 1000) {
return String(wh) + "Wh";
} else if (wh < (1000*10)) {
uint32_t wh100 = wh / 100;
return String(wh100/10) + "." + String(wh100%10) + "kWh";
} else if (wh < (1000*1000)) {
uint32_t kwh = wh / 1000;
return String(kwh) + "kWh";
} else if(wh < (1000*1000*10)) {
uint32_t kwh100 = wh / (1000*100);
return String(kwh100/10) + "." + String(kwh100%10) + "MWh";
} else if (wh < (1000*1000*1000)) {
uint32_t mwh = wh / (1000*1000);
return String(mwh) + "MWh";
} else if(kilo_watt_hour < (1000*1000*10)) {
uint32_t gwh = kilo_watt_hour / (1000*1000);
return String(gwh/10) + "." + String(gwh%10) + "GWh";
} else if (kilo_watt_hour < (1000*1000*1000)) {
uint32_t gwh = kilo_watt_hour / (1000*1000);
return String(gwh) + "GWh";
} else {
return String(">1 TWh"); // damn
return String(">1 GWh"); // damn
}
}

Expand Down
4 changes: 2 additions & 2 deletions software/src/modules/front_panel/front_panel.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ class FrontPanel : public DeviceModule<TF_WARPFrontPanel,

void check_flash_metadata();

String watt_value_to_display_string(const int32_t watt);
String watt_hour_value_to_display_string(const uint32_t kilo_watt_hour);
String watt_value_to_display_string(const int32_t w);
String watt_hour_value_to_display_string(const uint32_t wh);
String price_value_to_display_string(const int32_t price);

ConfigRoot config;
Expand Down
14 changes: 7 additions & 7 deletions software/src/modules/heating/heating.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,23 +264,23 @@ void Heating::update()
// we check the expected px excess and unblock if it is below the threshold.
if (blocked && summer_yield_forecast_active) {
extended_logging("We are in block time and yield forecast is active.");
DataReturn<uint32_t> kwh_expected = {false, 0};
DataReturn<uint32_t> wh_expected = {false, 0};
if (is_morning) {
kwh_expected = solar_forecast.get_kwh_today();
wh_expected = solar_forecast.get_wh_today();
} else if (is_evening) {
kwh_expected = solar_forecast.get_kwh_tomorrow();
wh_expected = solar_forecast.get_wh_tomorrow();
} else {
extended_logging("We are in block time but not in morning or evening. Ignoring yield forecast.");
}

if(!kwh_expected.data_available) {
if(!wh_expected.data_available) {
extended_logging("Expected PV yield not available. Ignoring yield forecast.");
} else {
if (kwh_expected.data < summer_yield_forecast_threshold) {
extended_logging("Expected PV yield %dkWh is below threshold of %dkWh.", kwh_expected.data, summer_yield_forecast_threshold);
if (wh_expected.data/1000 < summer_yield_forecast_threshold) {
extended_logging("Expected PV yield %dkWh is below threshold of %dkWh.", wh_expected.data/1000, summer_yield_forecast_threshold);
blocked = false;
} else {
extended_logging("Expected PV yield %dkWh is above or equal to threshold of %dkWh.", kwh_expected.data, summer_yield_forecast_threshold);
extended_logging("Expected PV yield %dkWh is above or equal to threshold of %dkWh.", wh_expected.data/1000, summer_yield_forecast_threshold);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions software/src/modules/solar_forecast/solar_forecast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ bool SolarForecast::forecast_time_between(const uint32_t first_date, const uint3
return (forecast_time >= start) && (forecast_time <= end);
}

DataReturn<uint32_t> SolarForecast::get_kwh_today()
DataReturn<uint32_t> SolarForecast::get_wh_today()
{
const uint32_t start = get_localtime_today_midnight_in_utc() / 60;
const uint32_t end = start + 60*24 - 1;
Expand All @@ -589,7 +589,7 @@ DataReturn<uint32_t> SolarForecast::get_kwh_today()
return {count != 0, wh};
}

DataReturn<uint32_t> SolarForecast::get_kwh_tomorrow()
DataReturn<uint32_t> SolarForecast::get_wh_tomorrow()
{
const uint32_t start = get_localtime_today_midnight_in_utc() / 60 + 60*24;
const uint32_t end = start + 60*24 - 1;
Expand Down
4 changes: 2 additions & 2 deletions software/src/modules/solar_forecast/solar_forecast.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ class SolarForecast final : public IModule
esp_err_t update_event_handler_impl(esp_http_client_event_t *event);
void next_update();

DataReturn<uint32_t> get_kwh_today();
DataReturn<uint32_t> get_kwh_tomorrow();
DataReturn<uint32_t> get_wh_today();
DataReturn<uint32_t> get_wh_tomorrow();

ConfigRoot config;
ConfigRoot state;
Expand Down

0 comments on commit f9fad3a

Please sign in to comment.