RGBMatrixDisplay/src/mode/Energy/Energy.h

98 lines
2.9 KiB
C++

#ifndef MODE_ENERGY_H
#define MODE_ENERGY_H
#include <patrix/core/mqtt.h>
#include "mode/Mode.h"
#define PHOTOVOLTAIC_ENERGY_KWH "openDTU/pv/ac/yieldtotal"
#define GRID_IMPORT_WH "electricity/grid/energy/import/wh"
#define GRID_EXPORT_WH "electricity/grid/energy/export/wh"
#define POWER_PHOTOVOLTAIC_photovoltaicEnergyKWh_BEFORE_METER_CHANGE 287.995
#define PV_COST_TOTAL_EURO 576.52
#define GRID_KWH_EURO 0.33
#define PV_COST_AMORTISATION_KWH ( PV_COST_TOTAL_EURO / GRID_KWH_EURO )
class Energy final : public Mode {
double photovoltaicEnergyKWh = NAN;
unsigned long photovoltaicEnergyKWhLast = 0;
double gridImportKWh = NAN;
unsigned long gridImportKWhLast = 0;
double gridExportKWh = NAN;
unsigned long gridExportKWhLast = 0;
int page = 0;
public:
explicit Energy(Display& display) : Mode(display) {
timer(0, 7000);
}
const char *getName() override {
return "Energy";
}
void start() override {
mqttSubscribe(PHOTOVOLTAIC_ENERGY_KWH);
mqttSubscribe(GRID_IMPORT_WH);
mqttSubscribe(GRID_EXPORT_WH);
}
void stop() override {
mqttUnsubscribe(PHOTOVOLTAIC_ENERGY_KWH);
mqttUnsubscribe(GRID_IMPORT_WH);
mqttUnsubscribe(GRID_EXPORT_WH);
}
protected:
void tick(uint8_t index, microseconds_t microseconds) override {
page = (page + 1) % 3;
}
void step(microseconds_t microseconds) override {
if (realtimeChanged) {
markDirty();
}
}
void draw(Display& display) override {
const auto photovoltaicEnergyKWhAfterMeterChange = photovoltaicEnergyKWh - POWER_PHOTOVOLTAIC_photovoltaicEnergyKWh_BEFORE_METER_CHANGE;
const auto selfAfterMeterChange = photovoltaicEnergyKWhAfterMeterChange - gridExportKWh;
const auto selfRatio = selfAfterMeterChange / photovoltaicEnergyKWhAfterMeterChange;
const auto selfConsumedKWh = selfRatio * photovoltaicEnergyKWh;
const auto costSaved = selfConsumedKWh * GRID_KWH_EURO;
const auto amortisationPercent = selfConsumedKWh / PV_COST_AMORTISATION_KWH * 100;
display.clear();
display.cursorX = 0;
display.cursorY = 1;
if (page == 0) {
display.foreground = Green;
display.printf("%3.0f€", costSaved);
display.foreground = White;
display.printf(" %3.0f%%", amortisationPercent);
} else if (page == 1) {
display.foreground = Blue;
display.printf("%3.0f", photovoltaicEnergyKWh);
display.foreground = Green;
display.printf(" %3.0f", selfConsumedKWh);
} else {
display.foreground = Yellow;
display.printf("%4.0f", gridImportKWh);
display.foreground = Magenta;
display.printf(" %3.0f", gridExportKWh);
}
}
};
#endif