From 9995c1172ee1f4e338107b9ca3d590523aa47160 Mon Sep 17 00:00:00 2001 From: Bernhard Kirchen Date: Thu, 22 Jun 2023 21:32:20 +0200 Subject: [PATCH] VE.Direct live view enhancements (#269) * add calculated values to VE.Direct data solar current, battery output power, and the charger's efficiency can be calculated from the values reported by the charger. the efficiency must be taken with a grain of salt. it seems that the solar power value and the battery output voltage/current are not always in sync. for that reason a moving average is used to smooth out the calculated efficiency value. * show calculated VE.Direct values in web live view order the values and translations similarly for the input and output, starting with power at the top, then voltage, then current as the last of these three. * VE.Direct live view: use 'd' as unit for days 'd' is the SI unit symbol for days and does not need translation, which is desirable as units are not translated throughout the project. * refactor VE.Direct live view * move Dynamic Power Limiter data into its own type. * split VE.Direct data into three types: "device", "input", and "output". hence all input and output values are now ValueObject, which allows to iterate over them using a loop without typing issues. * generate the tables with input and output values using a loop, rather than defining each row individually. * localize numbers using $n (vue method), which fixes switching the number format (dot vs. comma) when switching the language. * use no decimal point for power values (they are integers), three decimal points for kWh values (charger only reports two decimal places, but three are easier to read since the unit is *kilo* Wh), one decimal point for the efficiency, and two for voltage and current. * update language tokens to avoid mapping JSON keys to language keys (use the JSON keys to access the language tokens). * re-structure language tokes so the brief keys took over from VeDirectFrameHandler always make sense (nest into "input" and "output"). * order values similarly from top to bottom: power, then voltage, then current. this is following the order of the inverters' details. * group values by type/unit (yield and max. power) and order them "newest" to "oldest" from top to bottom. * increase the DynamicJsonDocument as it was too small to hold the newly added data. * update webapp_dist to include VE.Direct live view refactoring --- .../VeDirectFrameHandler.cpp | 13 +++ .../VeDirectFrameHandler.h | 38 ++++++- src/WebApi_ws_vedirect_live.cpp | 87 +++++++++------- webapp/src/components/VedirectView.vue | 93 ++++++++---------- webapp/src/locales/de.json | 29 +++--- webapp/src/locales/en.json | 29 +++--- webapp/src/locales/fr.json | 32 +++--- webapp/src/types/VedirectLiveDataStatus.ts | 23 +++-- webapp_dist/index.html.gz | Bin 329 -> 329 bytes webapp_dist/js/app.js.gz | Bin 186528 -> 186515 bytes webapp_dist/zones.json.gz | Bin 4100 -> 4100 bytes 11 files changed, 214 insertions(+), 130 deletions(-) diff --git a/lib/VeDirectFrameHandler/VeDirectFrameHandler.cpp b/lib/VeDirectFrameHandler/VeDirectFrameHandler.cpp index c53bf540..fa80dd8c 100644 --- a/lib/VeDirectFrameHandler/VeDirectFrameHandler.cpp +++ b/lib/VeDirectFrameHandler/VeDirectFrameHandler.cpp @@ -259,6 +259,19 @@ void VeDirectFrameHandler::textRxEvent(char * name, char * value) { */ void VeDirectFrameHandler::frameEndEvent(bool valid) { if ( valid ) { + _tmpFrame.P = _tmpFrame.V * _tmpFrame.I; + + _tmpFrame.IPV = 0; + if ( _tmpFrame.VPV > 0) { + _tmpFrame.IPV = _tmpFrame.PPV / _tmpFrame.VPV; + } + + _tmpFrame.E = 0; + if ( _tmpFrame.PPV > 0) { + _efficiency.addNumber(static_cast(_tmpFrame.P * 100) / _tmpFrame.PPV); + _tmpFrame.E = _efficiency.getAverage(); + } + veFrame = _tmpFrame; setLastUpdate(); } diff --git a/lib/VeDirectFrameHandler/VeDirectFrameHandler.h b/lib/VeDirectFrameHandler/VeDirectFrameHandler.h index d02024a9..13b51eaa 100644 --- a/lib/VeDirectFrameHandler/VeDirectFrameHandler.h +++ b/lib/VeDirectFrameHandler/VeDirectFrameHandler.h @@ -36,10 +36,13 @@ typedef struct { uint32_t OR; // off reason uint8_t MPPT; // state of MPP tracker uint32_t HSDS; // day sequence number 1...365 + int32_t P; // battery output power in W (calculated) double V; // battery voltage in V double I; // battery current in A - double VPV; // panel voltage in V + double E; // efficiency in percent (calculated, moving average) int32_t PPV; // panel power in W + double VPV; // panel voltage in V + double IPV; // panel current in A (calculated) double H19; // yield total kWh double H20; // yield today kWh int32_t H21; // maximum power today W @@ -47,6 +50,38 @@ typedef struct { int32_t H23; // maximum power yesterday W } veStruct; +template +class MovingAverage { +public: + MovingAverage() + : _sum(0) + , _index(0) + , _count(0) { } + + void addNumber(T num) { + if (_count < WINDOW_SIZE) { + _count++; + } else { + _sum -= _window[_index]; + } + + _window[_index] = num; + _sum += num; + _index = (_index + 1) % WINDOW_SIZE; + } + + double getAverage() const { + if (_count == 0) { return 0.0; } + return static_cast(_sum) / _count; + } + +private: + std::array _window; + T _sum; + size_t _index; + size_t _count; +}; + class VeDirectFrameHandler { public: @@ -82,6 +117,7 @@ private: char _name[VE_MAX_VALUE_LEN]; // buffer for the field name char _value[VE_MAX_VALUE_LEN]; // buffer for the field value veStruct _tmpFrame{}; // private struct for received name and value pairs + MovingAverage _efficiency; unsigned long _pollInterval; unsigned long _lastPoll; }; diff --git a/src/WebApi_ws_vedirect_live.cpp b/src/WebApi_ws_vedirect_live.cpp index 68f8d925..539917a7 100644 --- a/src/WebApi_ws_vedirect_live.cpp +++ b/src/WebApi_ws_vedirect_live.cpp @@ -61,7 +61,7 @@ void WebApiWsVedirectLiveClass::loop() String buffer; // free JsonDocument as soon as possible { - DynamicJsonDocument root(1024); + DynamicJsonDocument root(2048); JsonVariant var = root; generateJsonResponse(var); serializeJson(root, buffer); @@ -88,47 +88,64 @@ void WebApiWsVedirectLiveClass::loop() void WebApiWsVedirectLiveClass::generateJsonResponse(JsonVariant& root) { // device info - root["data_age"] = (millis() - VeDirect.getLastUpdate() ) / 1000; - root["age_critical"] = !VeDirect.isDataValid(); - root["PID"] = VeDirect.getPidAsString(VeDirect.veFrame.PID); - root["SER"] = VeDirect.veFrame.SER; - root["FW"] = VeDirect.veFrame.FW; - root["LOAD"] = VeDirect.veFrame.LOAD == true ? "ON" : "OFF"; - root["CS"] = VeDirect.getCsAsString(VeDirect.veFrame.CS); - root["ERR"] = VeDirect.getErrAsString(VeDirect.veFrame.ERR); - root["OR"] = VeDirect.getOrAsString(VeDirect.veFrame.OR); - root["MPPT"] = VeDirect.getMpptAsString(VeDirect.veFrame.MPPT); - root["HSDS"]["v"] = VeDirect.veFrame.HSDS; - root["HSDS"]["u"] = "Days"; + root["device"]["data_age"] = (millis() - VeDirect.getLastUpdate() ) / 1000; + root["device"]["age_critical"] = !VeDirect.isDataValid(); + root["device"]["PID"] = VeDirect.getPidAsString(VeDirect.veFrame.PID); + root["device"]["SER"] = VeDirect.veFrame.SER; + root["device"]["FW"] = VeDirect.veFrame.FW; + root["device"]["LOAD"] = VeDirect.veFrame.LOAD == true ? "ON" : "OFF"; + root["device"]["CS"] = VeDirect.getCsAsString(VeDirect.veFrame.CS); + root["device"]["ERR"] = VeDirect.getErrAsString(VeDirect.veFrame.ERR); + root["device"]["OR"] = VeDirect.getOrAsString(VeDirect.veFrame.OR); + root["device"]["MPPT"] = VeDirect.getMpptAsString(VeDirect.veFrame.MPPT); + root["device"]["HSDS"]["v"] = VeDirect.veFrame.HSDS; + root["device"]["HSDS"]["u"] = "d"; // battery info - root["V"]["v"] = VeDirect.veFrame.V; - root["V"]["u"] = "V"; - root["I"]["v"] = VeDirect.veFrame.I; - root["I"]["u"] = "A"; + root["output"]["P"]["v"] = VeDirect.veFrame.P; + root["output"]["P"]["u"] = "W"; + root["output"]["P"]["d"] = 0; + root["output"]["V"]["v"] = VeDirect.veFrame.V; + root["output"]["V"]["u"] = "V"; + root["output"]["V"]["d"] = 2; + root["output"]["I"]["v"] = VeDirect.veFrame.I; + root["output"]["I"]["u"] = "A"; + root["output"]["I"]["d"] = 2; + root["output"]["E"]["v"] = VeDirect.veFrame.E; + root["output"]["E"]["u"] = "%"; + root["output"]["E"]["d"] = 1; // panel info - root["VPV"]["v"] = VeDirect.veFrame.VPV; - root["VPV"]["u"] = "V"; - root["PPV"]["v"] = VeDirect.veFrame.PPV; - root["PPV"]["u"] = "W"; - root["H19"]["v"] = VeDirect.veFrame.H19; - root["H19"]["u"] = "kWh"; - root["H20"]["v"] = VeDirect.veFrame.H20; - root["H20"]["u"] = "kWh"; - root["H21"]["v"] = VeDirect.veFrame.H21; - root["H21"]["u"] = "W"; - root["H22"]["v"] = VeDirect.veFrame.H22; - root["H22"]["u"] = "kWh"; - root["H23"]["v"] = VeDirect.veFrame.H23; - root["H23"]["u"] = "W"; + root["input"]["PPV"]["v"] = VeDirect.veFrame.PPV; + root["input"]["PPV"]["u"] = "W"; + root["input"]["PPV"]["d"] = 0; + root["input"]["VPV"]["v"] = VeDirect.veFrame.VPV; + root["input"]["VPV"]["u"] = "V"; + root["input"]["VPV"]["d"] = 2; + root["input"]["IPV"]["v"] = VeDirect.veFrame.IPV; + root["input"]["IPV"]["u"] = "A"; + root["input"]["IPV"]["d"] = 2; + root["input"]["YieldToday"]["v"] = VeDirect.veFrame.H20; + root["input"]["YieldToday"]["u"] = "kWh"; + root["input"]["YieldToday"]["d"] = 3; + root["input"]["YieldYesterday"]["v"] = VeDirect.veFrame.H22; + root["input"]["YieldYesterday"]["u"] = "kWh"; + root["input"]["YieldYesterday"]["d"] = 3; + root["input"]["YieldTotal"]["v"] = VeDirect.veFrame.H19; + root["input"]["YieldTotal"]["u"] = "kWh"; + root["input"]["YieldTotal"]["d"] = 3; + root["input"]["MaximumPowerToday"]["v"] = VeDirect.veFrame.H21; + root["input"]["MaximumPowerToday"]["u"] = "W"; + root["input"]["MaximumPowerToday"]["d"] = 0; + root["input"]["MaximumPowerYesterday"]["v"] = VeDirect.veFrame.H23; + root["input"]["MaximumPowerYesterday"]["u"] = "W"; + root["input"]["MaximumPowerYesterday"]["d"] = 0; // power limiter state + root["dpl"]["PLSTATE"] = -1; if (Configuration.get().PowerLimiter_Enabled) - root["PLSTATE"] = PowerLimiter.getPowerLimiterState(); - else - root["PLSTATE"] = -1; - root["PLLIMIT"] = PowerLimiter.getLastRequestedPowewrLimit(); + root["dpl"]["PLSTATE"] = PowerLimiter.getPowerLimiterState(); + root["dpl"]["PLLIMIT"] = PowerLimiter.getLastRequestedPowewrLimit(); if (VeDirect.getLastUpdate() > _newestVedirectTimestamp) { _newestVedirectTimestamp = VeDirect.getLastUpdate(); diff --git a/webapp/src/components/VedirectView.vue b/webapp/src/components/VedirectView.vue index a4a4f48b..43df79dd 100644 --- a/webapp/src/components/VedirectView.vue +++ b/webapp/src/components/VedirectView.vue @@ -34,21 +34,21 @@
@@ -119,15 +119,15 @@ - - {{ $t('vedirecthome.BatteryVoltage') }} - {{formatNumber(vedirectData.V.v)}} - {{vedirectData.V.u}} - - - {{ $t('vedirecthome.BatteryCurrent') }} - {{formatNumber(vedirectData.I.v)}} - {{vedirectData.I.u}} + + {{ $t('vedirecthome.output.' + key) }} + + {{ $n(prop.v, 'decimal', { + minimumFractionDigits: prop.d, + maximumFractionDigits: prop.d}) + }} + + {{prop.u}} @@ -149,40 +149,15 @@ - - {{ $t('vedirecthome.PanelVoltage') }} - {{formatNumber(vedirectData.VPV.v)}} - {{vedirectData.VPV.u}} - - - {{ $t('vedirecthome.PanelPower') }} - {{formatNumber(vedirectData.PPV.v)}} - {{vedirectData.PPV.u}} - - - {{ $t('vedirecthome.YieldTotal') }} - {{formatNumber(vedirectData.H19.v)}} - {{vedirectData.H19.u}} - - - {{ $t('vedirecthome.YieldToday') }} - {{formatNumber(vedirectData.H20.v)}} - {{vedirectData.H20.u}} - - - {{ $t('vedirecthome.MaximumPowerToday') }} - {{formatNumber(vedirectData.H21.v)}} - {{vedirectData.H21.u}} - - - {{ $t('vedirecthome.YieldYesterday') }} - {{formatNumber(vedirectData.H22.v)}} - {{vedirectData.H22.u}} - - - {{ $t('vedirecthome.MaximumPowerYesterday') }} - {{formatNumber(vedirectData.H23.v)}} - {{vedirectData.H23.u}} + + {{ $t('vedirecthome.input.' + key) }} + + {{ $n(prop.v, 'decimal', { + minimumFractionDigits: prop.d, + maximumFractionDigits: prop.d}) + }} + + {{prop.u}} @@ -203,7 +178,7 @@