Skip to content

Commit

Permalink
Merge branch 'helgeerbe:development' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert-Schimanek authored Jun 20, 2024
2 parents 8f0067b + 83ac154 commit e8c76a5
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 9 deletions.
1 change: 1 addition & 0 deletions include/Configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ struct CONFIG_T {
uint32_t Interval;
bool IsInverterBehindPowerMeter;
bool IsInverterSolarPowered;
bool UseOverscalingToCompensateShading;
uint64_t InverterId;
uint8_t InverterChannelId;
int32_t TargetPowerConsumption;
Expand Down
3 changes: 2 additions & 1 deletion include/MqttHandlePowerLimiter.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ class MqttHandlePowerLimiterClass {
VoltageStopThreshold,
FullSolarPassThroughStartVoltage,
FullSolarPassThroughStopVoltage,
UpperPowerLimit
UpperPowerLimit,
TargetPowerConsumption
};

void onMqttCmd(MqttPowerLimiterCommand command, const espMqttClientTypes::MessageProperties& properties, const char* topic, const uint8_t* payload, size_t len, size_t index, size_t total);
Expand Down
1 change: 1 addition & 0 deletions include/defaults.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@
#define POWERLIMITER_INTERVAL 10
#define POWERLIMITER_IS_INVERTER_BEHIND_POWER_METER true
#define POWERLIMITER_IS_INVERTER_SOLAR_POWERED false
#define POWERLIMITER_USE_OVERSCALING_TO_COMPENSATE_SHADING false
#define POWERLIMITER_INVERTER_ID 0ULL
#define POWERLIMITER_INVERTER_CHANNEL_ID 0
#define POWERLIMITER_TARGET_POWER_CONSUMPTION 0
Expand Down
2 changes: 2 additions & 0 deletions src/Configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ bool ConfigurationClass::write()
powerlimiter["interval"] = config.PowerLimiter.Interval;
powerlimiter["is_inverter_behind_powermeter"] = config.PowerLimiter.IsInverterBehindPowerMeter;
powerlimiter["is_inverter_solar_powered"] = config.PowerLimiter.IsInverterSolarPowered;
powerlimiter["use_overscaling_to_compensate_shading"] = config.PowerLimiter.UseOverscalingToCompensateShading;
powerlimiter["inverter_id"] = config.PowerLimiter.InverterId;
powerlimiter["inverter_channel_id"] = config.PowerLimiter.InverterChannelId;
powerlimiter["target_power_consumption"] = config.PowerLimiter.TargetPowerConsumption;
Expand Down Expand Up @@ -455,6 +456,7 @@ bool ConfigurationClass::read()
config.PowerLimiter.Interval = powerlimiter["interval"] | POWERLIMITER_INTERVAL;
config.PowerLimiter.IsInverterBehindPowerMeter = powerlimiter["is_inverter_behind_powermeter"] | POWERLIMITER_IS_INVERTER_BEHIND_POWER_METER;
config.PowerLimiter.IsInverterSolarPowered = powerlimiter["is_inverter_solar_powered"] | POWERLIMITER_IS_INVERTER_SOLAR_POWERED;
config.PowerLimiter.UseOverscalingToCompensateShading = powerlimiter["use_overscaling_to_compensate_shading"] | POWERLIMITER_USE_OVERSCALING_TO_COMPENSATE_SHADING;
config.PowerLimiter.InverterId = powerlimiter["inverter_id"] | POWERLIMITER_INVERTER_ID;
config.PowerLimiter.InverterChannelId = powerlimiter["inverter_channel_id"] | POWERLIMITER_INVERTER_CHANNEL_ID;
config.PowerLimiter.TargetPowerConsumption = powerlimiter["target_power_consumption"] | POWERLIMITER_TARGET_POWER_CONSUMPTION;
Expand Down
8 changes: 8 additions & 0 deletions src/MqttHandlePowerLimiter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ void MqttHandlePowerLimiterClass::init(Scheduler& scheduler)
subscribe("threshold/voltage/full_solar_passthrough_stop", MqttPowerLimiterCommand::FullSolarPassThroughStopVoltage);
subscribe("mode", MqttPowerLimiterCommand::Mode);
subscribe("upper_power_limit", MqttPowerLimiterCommand::UpperPowerLimit);
subscribe("target_power_consumption", MqttPowerLimiterCommand::TargetPowerConsumption);

_lastPublish = millis();
}
Expand Down Expand Up @@ -79,6 +80,8 @@ void MqttHandlePowerLimiterClass::loop()

MqttSettings.publish("powerlimiter/status/upper_power_limit", String(config.PowerLimiter.UpperPowerLimit));

MqttSettings.publish("powerlimiter/status/target_power_consumption", String(config.PowerLimiter.TargetPowerConsumption));

MqttSettings.publish("powerlimiter/status/inverter_update_timeouts", String(PowerLimiter.getInverterUpdateTimeouts()));

// no thresholds are relevant for setups without a battery
Expand Down Expand Up @@ -182,6 +185,11 @@ void MqttHandlePowerLimiterClass::onMqttCmd(MqttPowerLimiterCommand command, con
MessageOutput.printf("Setting upper power limit to: %d W\r\n", intValue);
config.PowerLimiter.UpperPowerLimit = intValue;
break;
case MqttPowerLimiterCommand::TargetPowerConsumption:
if (config.PowerLimiter.TargetPowerConsumption == intValue) { return; }
MessageOutput.printf("Setting target power consumption to: %d W\r\n", intValue);
config.PowerLimiter.TargetPowerConsumption = intValue;
break;
}

// not reached if the value did not change
Expand Down
68 changes: 60 additions & 8 deletions src/PowerLimiter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,16 @@ float PowerLimiterClass::getBatteryVoltage(bool log) {
return res;
}

static float getInverterEfficiency(std::shared_ptr<InverterAbstract> inverter)
{
float inverterEfficiencyPercent = inverter->Statistics()->getChannelFieldValue(
TYPE_INV, CH0, FLD_EFF);

// fall back to hoymiles peak efficiency as per datasheet if inverter
// is currently not producing (efficiency is zero in that case)
return (inverterEfficiencyPercent > 0) ? inverterEfficiencyPercent/100 : 0.967;
}

/**
* calculate the AC output power (limit) to set, such that the inverter uses
* the given power on its DC side, i.e., adjust the power for the inverter's
Expand All @@ -351,12 +361,7 @@ int32_t PowerLimiterClass::inverterPowerDcToAc(std::shared_ptr<InverterAbstract>
{
CONFIG_T& config = Configuration.get();

float inverterEfficiencyPercent = inverter->Statistics()->getChannelFieldValue(
TYPE_INV, CH0, FLD_EFF);

// fall back to hoymiles peak efficiency as per datasheet if inverter
// is currently not producing (efficiency is zero in that case)
float inverterEfficiencyFactor = (inverterEfficiencyPercent > 0) ? inverterEfficiencyPercent/100 : 0.967;
float inverterEfficiencyFactor = getInverterEfficiency(inverter);

// account for losses between solar charger and inverter (cables, junctions...)
float lossesFactor = 1.00 - static_cast<float>(config.PowerLimiter.SolarPassThroughLosses)/100;
Expand Down Expand Up @@ -703,8 +708,7 @@ bool PowerLimiterClass::updateInverter()
*
* TODO(schlimmchen): the current implementation is broken and is in need of
* refactoring. currently it only works for inverters that provide one MPPT for
* each input. it also does not work as expected if any input produces *some*
* energy, but is limited by its respective solar input.
* each input.
*/
static int32_t scalePowerLimit(std::shared_ptr<InverterAbstract> inverter, int32_t newLimit, int32_t currentLimitWatts)
{
Expand All @@ -729,6 +733,54 @@ static int32_t scalePowerLimit(std::shared_ptr<InverterAbstract> inverter, int32
// producing very little due to the very low limit.
if (currentLimitWatts < dcTotalChnls * 10) { return newLimit; }

auto const& config = Configuration.get();
auto allowOverscaling = config.PowerLimiter.UseOverscalingToCompensateShading;
auto isInverterSolarPowered = config.PowerLimiter.IsInverterSolarPowered;

// overscalling allows us to compensate for shaded panels by increasing the
// total power limit, if the inverter is solar powered.
if (allowOverscaling && isInverterSolarPowered) {
auto inverterOutputAC = inverter->Statistics()->getChannelFieldValue(TYPE_AC, CH0, FLD_PAC);
float inverterEfficiencyFactor = getInverterEfficiency(inverter);

// 98% of the expected power is good enough
auto expectedAcPowerPerChannel = (currentLimitWatts / dcTotalChnls) * 0.98;

size_t dcShadedChnls = 0;
auto shadedChannelACPowerSum = 0.0;

for (auto& c : dcChnls) {
auto channelPowerAC = inverter->Statistics()->getChannelFieldValue(TYPE_DC, c, FLD_PDC) * inverterEfficiencyFactor;

if (channelPowerAC < expectedAcPowerPerChannel) {
dcShadedChnls++;
shadedChannelACPowerSum += channelPowerAC;
}
}

// no shading or the shaded channels provide more power than what
// we currently need.
if (dcShadedChnls == 0 || shadedChannelACPowerSum >= newLimit) { return newLimit; }

// keep the currentLimit when all channels are shaded and we get the
// expected AC power or less.
if (dcShadedChnls == dcTotalChnls && inverterOutputAC <= newLimit) {
MessageOutput.printf("[DPL::scalePowerLimit] all channels are shaded, "
"keeping the current limit of %d W\r\n", currentLimitWatts);

return currentLimitWatts;
}

size_t dcNonShadedChnls = dcTotalChnls - dcShadedChnls;
auto overScaledLimit = static_cast<int32_t>((newLimit - shadedChannelACPowerSum) / dcNonShadedChnls * dcTotalChnls);

if (overScaledLimit <= newLimit) { return newLimit; }

MessageOutput.printf("[DPL::scalePowerLimit] %d/%d channels are shaded, "
"scaling %d W\r\n", dcShadedChnls, dcTotalChnls, overScaledLimit);
return overScaledLimit;
}

size_t dcProdChnls = 0;
for (auto& c : dcChnls) {
if (inverter->Statistics()->getChannelFieldValue(TYPE_DC, c, FLD_PDC) > 2.0) {
Expand Down
2 changes: 2 additions & 0 deletions src/WebApi_powerlimiter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ void WebApiPowerLimiterClass::onStatus(AsyncWebServerRequest* request)
root["battery_always_use_at_night"] = config.PowerLimiter.BatteryAlwaysUseAtNight;
root["is_inverter_behind_powermeter"] = config.PowerLimiter.IsInverterBehindPowerMeter;
root["is_inverter_solar_powered"] = config.PowerLimiter.IsInverterSolarPowered;
root["use_overscaling_to_compensate_shading"] = config.PowerLimiter.UseOverscalingToCompensateShading;
root["inverter_serial"] = String(config.PowerLimiter.InverterId);
root["inverter_channel_id"] = config.PowerLimiter.InverterChannelId;
root["target_power_consumption"] = config.PowerLimiter.TargetPowerConsumption;
Expand Down Expand Up @@ -164,6 +165,7 @@ void WebApiPowerLimiterClass::onAdminPost(AsyncWebServerRequest* request)

config.PowerLimiter.IsInverterBehindPowerMeter = root["is_inverter_behind_powermeter"].as<bool>();
config.PowerLimiter.IsInverterSolarPowered = root["is_inverter_solar_powered"].as<bool>();
config.PowerLimiter.UseOverscalingToCompensateShading = root["use_overscaling_to_compensate_shading"].as<bool>();
config.PowerLimiter.InverterId = root["inverter_serial"].as<uint64_t>();
config.PowerLimiter.InverterChannelId = root["inverter_channel_id"].as<uint8_t>();
config.PowerLimiter.TargetPowerConsumption = root["target_power_consumption"].as<int32_t>();
Expand Down
2 changes: 2 additions & 0 deletions webapp/src/locales/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,8 @@
"InverterIsBehindPowerMeter": "Stromzählermessung beinhaltet Wechselrichter",
"InverterIsBehindPowerMeterHint": "Aktivieren falls sich der Stromzähler-Messwert um die Ausgangsleistung des Wechselrichters verringert, wenn dieser Strom produziert. Normalerweise ist das zutreffend.",
"InverterIsSolarPowered": "Wechselrichter wird von Solarmodulen gespeist",
"UseOverscalingToCompensateShading": "Verschattung durch Überskalierung ausgleichen",
"UseOverscalingToCompensateShadingHint": "Erlaubt das Überskalieren des Wechselrichter-Limits, um Verschattung eines oder mehrerer Eingänge auszugleichen",
"VoltageThresholds": "Batterie Spannungs-Schwellwerte ",
"VoltageLoadCorrectionInfo": "<b>Hinweis:</b> Wenn Leistung von der Batterie abgegeben wird, bricht ihre Spannung etwas ein. Der Spannungseinbruch skaliert mit dem Entladestrom. Damit nicht vorzeitig der Wechselrichter ausgeschaltet wird sobald der Stop-Schwellenwert unterschritten wurde, wird der hier angegebene Korrekturfaktor mit einberechnet um die Spannung zu errechnen die der Akku in Ruhe hätte. Korrigierte Spannung = DC Spannung + (Aktuelle Leistung (W) * Korrekturfaktor).",
"InverterRestartHour": "Uhrzeit für geplanten Neustart",
Expand Down
2 changes: 2 additions & 0 deletions webapp/src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,8 @@
"InverterIsBehindPowerMeter": "PowerMeter reading includes inverter output",
"InverterIsBehindPowerMeterHint": "Enable this option if the power meter reading is reduced by the inverter's output when it produces power. This is typically true.",
"InverterIsSolarPowered": "Inverter is powered by solar modules",
"UseOverscalingToCompensateShading": "Compensate for shading",
"UseOverscalingToCompensateShadingHint": "Allow to overscale the inverter limit to compensate for shading of one or multiple inputs",
"VoltageThresholds": "Battery Voltage Thresholds",
"VoltageLoadCorrectionInfo": "<b>Hint:</b> When the battery is discharged, its voltage drops. The voltage drop scales with the discharge current. In order to not stop the inverter too early (stop threshold), this load correction factor can be specified to calculate the battery voltage if it was idle. Corrected voltage = DC Voltage + (Current power * correction factor).",
"InverterRestartHour": "Automatic Restart Time",
Expand Down
1 change: 1 addition & 0 deletions webapp/src/types/PowerLimiterConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export interface PowerLimiterConfig {
battery_always_use_at_night: boolean;
is_inverter_behind_powermeter: boolean;
is_inverter_solar_powered: boolean;
use_overscaling_to_compensate_shading: boolean;
inverter_serial: string;
inverter_channel_id: number;
target_power_consumption: number;
Expand Down
10 changes: 10 additions & 0 deletions webapp/src/views/PowerLimiterAdminView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@
v-model="powerLimiterConfigList.is_inverter_solar_powered"
type="checkbox" wide/>

<InputElement v-show="canUseOverscaling()"
:label="$t('powerlimiteradmin.UseOverscalingToCompensateShading')"
:tooltip="$t('powerlimiteradmin.UseOverscalingToCompensateShadingHint')"
v-model="powerLimiterConfigList.use_overscaling_to_compensate_shading"
type="checkbox" wide/>

<div class="row mb-3" v-if="needsChannelSelection()">
<label for="inverter_channel" class="col-sm-4 col-form-label">
{{ $t('powerlimiteradmin.InverterChannelId') }}
Expand Down Expand Up @@ -329,6 +335,10 @@ export default defineComponent({
hasPowerMeter() {
return this.powerLimiterMetaData.power_meter_enabled;
},
canUseOverscaling() {
const cfg = this.powerLimiterConfigList;
return cfg.is_inverter_solar_powered;
},
canUseSolarPassthrough() {
const cfg = this.powerLimiterConfigList;
const meta = this.powerLimiterMetaData;
Expand Down

0 comments on commit e8c76a5

Please sign in to comment.