Show real time production

This commit is contained in:
Gunnar Skjold 2022-08-10 10:53:16 +02:00
parent 5d2e320b07
commit 3a4fc707b0
13 changed files with 137 additions and 33 deletions

View File

@ -67,7 +67,7 @@ bool AmsDataStorage::update(AmsData* data) {
}
tmElements_t last;
breakTime(day.lastMeterReadTime, last);
for(int i = last.Hour; i < utc.Hour; i++) {
for(int i = last.Hour; i <= utc.Hour; i++) {
if(debugger->isActive(RemoteDebug::VERBOSE)) {
debugger->printf("(AmsDataStorage) Clearing hour: %d\n", i);
}
@ -90,7 +90,7 @@ bool AmsDataStorage::update(AmsData* data) {
}
tmElements_t last;
breakTime(tz->toLocal(month.lastMeterReadTime), last);
for(int i = last.Day; i < ltz.Day; i++) {
for(int i = last.Day; i <= ltz.Day; i++) {
if(debugger->isActive(RemoteDebug::VERBOSE)) {
debugger->printf("(AmsDataStorage) Clearing day: %d\n", i);
}

View File

@ -80,6 +80,7 @@ bool EnergyAccounting::update(AmsData* amsData) {
}
use = 0;
produce = 0;
costHour = 0;
currentHour = local.Hour;
@ -106,19 +107,24 @@ bool EnergyAccounting::update(AmsData* amsData) {
}
unsigned long ms = this->lastUpdateMillis > amsData->getLastUpdateMillis() ? 0 : amsData->getLastUpdateMillis() - this->lastUpdateMillis;
float kwh = (amsData->getActiveImportPower() * (((float) ms) / 3600000.0)) / 1000.0;
float kwhi = (amsData->getActiveImportPower() * (((float) ms) / 3600000.0)) / 1000.0;
float kwhe = (amsData->getActiveExportPower() * (((float) ms) / 3600000.0)) / 1000.0;
lastUpdateMillis = amsData->getLastUpdateMillis();
if(kwh > 0) {
if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf("(EnergyAccounting) Adding %.4f kWh\n", kwh);
use += kwh;
if(kwhi > 0) {
if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf("(EnergyAccounting) Adding %.4f kWh import\n", kwhi);
use += kwhi;
if(eapi != NULL && eapi->getValueForHour(0) != ENTSOE_NO_VALUE) {
float price = eapi->getValueForHour(0);
float cost = price * kwh;
float cost = price * kwhi;
if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf("(EnergyAccounting) and %.4f %s\n", cost / 100.0, eapi->getCurrency());
costHour += cost;
costDay += cost;
}
}
if(kwhe > 0) {
if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf("(EnergyAccounting) Adding %.4f kWh export\n", kwhe);
produce += kwhe;
}
if(config != NULL) {
if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf("(EnergyAccounting) calculating threshold, currently at %d\n", currentThresholdIdx);
@ -151,10 +157,6 @@ double EnergyAccounting::getUseThisHour() {
return use;
}
double EnergyAccounting::getCostThisHour() {
return costHour;
}
double EnergyAccounting::getUseToday() {
float ret = 0.0;
time_t now = time(nullptr);
@ -168,14 +170,6 @@ double EnergyAccounting::getUseToday() {
return ret + getUseThisHour();
}
double EnergyAccounting::getCostToday() {
return costDay;
}
double EnergyAccounting::getCostYesterday() {
return data.costYesterday / 10.0;
}
double EnergyAccounting::getUseThisMonth() {
time_t now = time(nullptr);
if(now < BUILD_EPOCH) return 0;
@ -191,6 +185,51 @@ double EnergyAccounting::getUseThisMonth() {
return ret + getUseToday();
}
double EnergyAccounting::getProducedThisHour() {
return produce;
}
double EnergyAccounting::getProducedToday() {
float ret = 0.0;
time_t now = time(nullptr);
if(now < BUILD_EPOCH) return 0;
tmElements_t local, utc;
breakTime(tz->toLocal(now), local);
for(int i = 0; i < local.Hour; i++) {
breakTime(now - ((local.Hour - i) * 3600), utc);
ret += ds->getHourExport(utc.Hour) / 1000.0;
}
return ret + getProducedThisHour();
}
double EnergyAccounting::getProducedThisMonth() {
time_t now = time(nullptr);
if(now < BUILD_EPOCH) return 0;
tmElements_t tm;
if(tz != NULL)
breakTime(tz->toLocal(now), tm);
else
breakTime(now, tm);
float ret = 0;
for(int i = 0; i < tm.Day; i++) {
ret += ds->getDayExport(i) / 1000.0;
}
return ret + getProducedToday();
}
double EnergyAccounting::getCostThisHour() {
return costHour;
}
double EnergyAccounting::getCostToday() {
return costDay;
}
double EnergyAccounting::getCostYesterday() {
return data.costYesterday / 10.0;
}
double EnergyAccounting::getCostThisMonth() {
return data.costThisMonth + getCostToday();
}

View File

@ -42,11 +42,16 @@ public:
bool save();
double getUseThisHour();
double getCostThisHour();
double getUseToday();
double getUseThisMonth();
double getProducedThisHour();
double getProducedToday();
double getProducedThisMonth();
double getCostThisHour();
double getCostToday();
double getCostYesterday();
double getUseThisMonth();
double getCostThisMonth();
uint16_t getCostLastMonth();
@ -66,6 +71,7 @@ private:
Timezone *tz = NULL;
uint8_t currentHour = 0, currentDay = 0, currentThresholdIdx = 0;
double use, costHour, costDay;
double produce;
EnergyAccountingData data = { 0, 0, 0, 0, 0, 0 };
void calcDayCost();

View File

@ -27,7 +27,9 @@ bool JsonMqttHandler::publish(AmsData* data, AmsData* previousState, EnergyAccou
ea->getUseThisHour(),
ea->getUseToday(),
ea->getCurrentThreshold(),
ea->getMonthMax()
ea->getMonthMax(),
ea->getProducedThisHour(),
ea->getProducedToday()
);
return mqtt->publish(topic, json);
} else if(data->getListType() == 2) {
@ -55,7 +57,9 @@ bool JsonMqttHandler::publish(AmsData* data, AmsData* previousState, EnergyAccou
ea->getUseThisHour(),
ea->getUseToday(),
ea->getCurrentThreshold(),
ea->getMonthMax()
ea->getMonthMax(),
ea->getProducedThisHour(),
ea->getProducedToday()
);
return mqtt->publish(topic, json);
} else if(data->getListType() == 3) {
@ -88,7 +92,9 @@ bool JsonMqttHandler::publish(AmsData* data, AmsData* previousState, EnergyAccou
ea->getUseThisHour(),
ea->getUseToday(),
ea->getCurrentThreshold(),
ea->getMonthMax()
ea->getMonthMax(),
ea->getProducedThisHour(),
ea->getProducedToday()
);
return mqtt->publish(topic, json);
} else if(data->getListType() == 4) {
@ -125,7 +131,9 @@ bool JsonMqttHandler::publish(AmsData* data, AmsData* previousState, EnergyAccou
ea->getUseThisHour(),
ea->getUseToday(),
ea->getCurrentThreshold(),
ea->getMonthMax()
ea->getMonthMax(),
ea->getProducedThisHour(),
ea->getProducedToday()
);
return mqtt->publish(topic, json);
}

View File

@ -76,6 +76,8 @@ bool RawMqttHandler::publish(AmsData* data, AmsData* meterState, EnergyAccountin
mqtt->publish(topic + "/realtime/import/day", String(ea->getUseToday(), 2));
mqtt->publish(topic + "/realtime/import/threshold", String(ea->getCurrentThreshold(), 10), true, 0);
mqtt->publish(topic + "/realtime/import/monthmax", String(ea->getMonthMax(), 3), true, 0);
mqtt->publish(topic + "/realtime/export/hour", String(ea->getProducedThisHour(), 3));
mqtt->publish(topic + "/realtime/export/day", String(ea->getProducedToday(), 2));
return true;
}

View File

@ -747,10 +747,13 @@ void AmsWebServer::dataJson() {
ea->getCurrentThreshold(),
ea->getUseThisHour(),
ea->getCostThisHour(),
ea->getProducedThisHour(),
ea->getUseToday(),
ea->getCostToday(),
ea->getProducedToday(),
ea->getUseThisMonth(),
ea->getCostThisMonth(),
ea->getProducedThisMonth(),
(uint32_t) time(nullptr)
);

View File

@ -790,6 +790,12 @@ var fetch = function() {
if(currency) {
$('.sp').show();
}
if(om > 0) {
$('.se').removeClass('d-none');
$('#eache').html(json.ea.h.p.toFixed(2));
$('#eacde').html(json.ea.d.p.toFixed(1));
$('#eacme').html(json.ea.m.p.toFixed(0));
}
}
if(json.me) {

View File

@ -38,15 +38,18 @@
"t" : %d,
"h" : {
"u" : %.2f,
"c" : %.2f
"c" : %.2f,
"p" : %.2f
},
"d" : {
"u" : %.2f,
"c" : %.2f
"c" : %.2f,
"p" : %.2f
},
"m" : {
"u" : %.2f,
"c" : %.2f
"c" : %.2f,
"p" : %.2f
}
},
"c" : %lu

View File

@ -113,7 +113,7 @@
<div class="col-xl-12 mb-3">
<div class="bg-white rounded shadow pt-3 pb-3" style="font-size: 14px;">
<strong class="mr-3 ml-3">Real time calculation</strong><br/>
<strong class="mr-3 ml-3">Real time consumption</strong><br/>
<div class="row">
<div class="col-lg-3 col-sm-6">
<div class="mr-3 ml-3 d-flex">
@ -151,6 +151,35 @@
</div>
</div>
</div>
<strong class="mr-3 ml-3 se d-none">Real time production</strong><br/>
<div class="row se d-none">
<div class="col-lg-3 col-sm-6">
<div class="mr-3 ml-3 d-flex">
<div>Hour</div>
<div class="flex-fill text-right">
<span id="eache"></span> kWh
</div>
</div>
</div>
<div class="col-lg-3 col-sm-6">
<div class="mr-3 ml-3 d-flex">
<div>Day</div>
<div class="flex-fill text-right">
<span id="eacde"></span> kWh
</div>
</div>
</div>
<div class="col-lg-3 col-sm-6">
<div class="mr-3 ml-3 d-flex">
<div>Month</div>
<div class="flex-fill text-right">
<span id="eacme"></span> kWh
</div>
</div>
</div>
<div class="col-lg-3 col-sm-6"></div>
</div>
</div>
</div>

View File

@ -13,6 +13,8 @@
"h" : %.2f,
"d" : %.1f,
"t" : %d,
"x" : %.2f
"x" : %.2f,
"he" : %.2f,
"de" : %.1f
}
}

View File

@ -25,6 +25,8 @@
"h" : %.2f,
"d" : %.1f,
"t" : %d,
"x" : %.2f
"x" : %.2f,
"he" : %.2f,
"de" : %.1f
}
}

View File

@ -30,6 +30,8 @@
"h" : %.2f,
"d" : %.1f,
"t" : %d,
"x" : %.2f
"x" : %.2f,
"he" : %.2f,
"de" : %.1f
}
}

View File

@ -34,6 +34,8 @@
"h" : %.2f,
"d" : %.1f,
"t" : %d,
"x" : %.2f
"x" : %.2f,
"he" : %.2f,
"de" : %.1f
}
}