Skip to content

Commit

Permalink
Fixed accuracy of energy reported: no more floats but Wh instead.
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieucarbou committed Jan 28, 2025
1 parent a706cf9 commit 71e2c0e
Show file tree
Hide file tree
Showing 15 changed files with 43 additions and 49 deletions.
4 changes: 2 additions & 2 deletions include/i18n/en.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
#define YASOLR_LBL_013 "Firmware: Build Hash"
#define YASOLR_LBL_014 "Firmware: Build Timestamp"
#define YASOLR_LBL_015 "Firmware: Filename"
#define YASOLR_LBL_016 "Grid: Energy (kWh)"
#define YASOLR_LBL_017 "Grid: Energy Returned (kWh)"
#define YASOLR_LBL_016 "Grid: Energy (Wh)"
#define YASOLR_LBL_017 "Grid: Energy Returned (Wh)"
#define YASOLR_LBL_018 "Grid: Frequency (Hz)"
#define YASOLR_LBL_019 "Network : Hostname"
#define YASOLR_LBL_020 "Network : Interface"
Expand Down
4 changes: 2 additions & 2 deletions include/i18n/fr.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
#define YASOLR_LBL_013 "Micro-logiciel: Hachage de construction"
#define YASOLR_LBL_014 "Micro-logiciel: Date de construction"
#define YASOLR_LBL_015 "Micro-logiciel: Nom de fichier"
#define YASOLR_LBL_016 "Réseau électrique: Énergie (kWh)"
#define YASOLR_LBL_017 "Réseau électrique: Énergie retournée (kWh)"
#define YASOLR_LBL_016 "Réseau électrique: Énergie (Wh)"
#define YASOLR_LBL_017 "Réseau électrique: Énergie retournée (Wh)"
#define YASOLR_LBL_018 "Réseau électrique: Fréquence (Hz)"
#define YASOLR_LBL_019 "Réseau: Nom d'hôte"
#define YASOLR_LBL_020 "Réseau: Interface"
Expand Down
2 changes: 1 addition & 1 deletion lib/ESPDASHPro
10 changes: 4 additions & 6 deletions lib/MycilaDimmer/MycilaDimmer.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,11 @@ namespace Mycila {
uint16_t getFiringDelay() const { return _delay > _semiPeriod ? _semiPeriod : _delay; }

/**
* @brief Get the phase angle in radians of the dimmer in the range [0, PI]
* At 0% power, the phase angle is equal to PI
* @brief Get the phase angle in degrees (°) of the dimmer in the range [0, 180]
* At 0% power, the phase angle is equal to 180
* At 100% power, the phase angle is equal to 0
*/
float getPhaseAngle() const { return _delay >= _semiPeriod ? PI : PI * _delay / _semiPeriod; }
float getPhaseAngle() const { return _delay >= _semiPeriod ? 180 : 180 * _delay / _semiPeriod; }

#ifdef MYCILA_JSON_SUPPORT
/**
Expand All @@ -166,7 +166,6 @@ namespace Mycila {
* @param root: the JSON object to serialize to
*/
void toJson(const JsonObject& root) const {
const float angle = getPhaseAngle();
root["type"] = type();
root["enabled"] = isEnabled();
root["online"] = isOnline();
Expand All @@ -177,8 +176,7 @@ namespace Mycila {
root["duty_cycle_max"] = getDutyCycleMax();
root["semi_period"] = getSemiPeriod();
root["delay"] = getFiringDelay();
root["angle_d"] = angle * RAD_TO_DEG;
root["angle"] = angle;
root["angle"] = getPhaseAngle();
}
#endif

Expand Down
6 changes: 2 additions & 4 deletions lib/MycilaRouter/MycilaGrid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,8 @@ void Mycila::Grid::toJson(const JsonObject& dest, const Metrics& metrics) {
dest["apparent_power"] = metrics.apparentPower;
if (!std::isnan(metrics.current))
dest["current"] = metrics.current;
if (!std::isnan(metrics.energy))
dest["energy"] = metrics.energy;
if (!std::isnan(metrics.energyReturned))
dest["energy_returned"] = metrics.energyReturned;
dest["energy"] = metrics.energy;
dest["energy_returned"] = metrics.energyReturned;
if (!std::isnan(metrics.frequency))
dest["frequency"] = metrics.frequency;
if (!std::isnan(metrics.power))
Expand Down
4 changes: 2 additions & 2 deletions lib/MycilaRouter/MycilaGrid.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ namespace Mycila {
typedef struct {
float apparentPower = NAN;
float current = NAN;
float energy = NAN;
float energyReturned = NAN;
uint32_t energy = 0;
uint32_t energyReturned = 0;
float frequency = NAN;
float power = NAN;
float powerFactor = NAN;
Expand Down
3 changes: 1 addition & 2 deletions lib/MycilaRouter/MycilaRouter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ void Mycila::Router::toJson(const JsonObject& dest, const Metrics& metrics) {
dest["apparent_power"] = metrics.apparentPower;
if (!std::isnan(metrics.current))
dest["current"] = metrics.current;
if (!std::isnan(metrics.energy))
dest["energy"] = metrics.energy;
dest["energy"] = metrics.energy;
if (!std::isnan(metrics.power))
dest["power"] = metrics.power;
if (!std::isnan(metrics.powerFactor))
Expand Down
2 changes: 1 addition & 1 deletion lib/MycilaRouter/MycilaRouter.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace Mycila {
typedef struct {
float apparentPower = 0;
float current = 0;
float energy = 0;
uint32_t energy = 0;
float power = 0;
float powerFactor = NAN;
float resistance = NAN;
Expand Down
3 changes: 1 addition & 2 deletions lib/MycilaRouter/MycilaRouterOutput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ void Mycila::RouterOutput::toJson(const JsonObject& dest, const Metrics& metrics
dest["apparent_power"] = metrics.apparentPower;
if (!std::isnan(metrics.current))
dest["current"] = metrics.current;
if (!std::isnan(metrics.dimmedVoltage))
dest["energy"] = metrics.energy;
dest["energy"] = metrics.energy;
if (!std::isnan(metrics.power))
dest["power"] = metrics.power;
if (!std::isnan(metrics.powerFactor))
Expand Down
2 changes: 1 addition & 1 deletion lib/MycilaRouter/MycilaRouterOutput.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ namespace Mycila {
float apparentPower = 0;
float current = 0;
float dimmedVoltage = 0;
float energy = 0;
uint32_t energy = 0;
float power = 0;
float powerFactor = NAN;
float resistance = NAN;
Expand Down
4 changes: 2 additions & 2 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ lib_deps =
mathieucarbou/MycilaESPConnect @ 7.1.1
mathieucarbou/MycilaEasyDisplay @ 3.1.0
mathieucarbou/MycilaHADiscovery @ 6.0.1
mathieucarbou/MycilaJSY @ 13.1.1
mathieucarbou/MycilaJSY @ 14.0.1
mathieucarbou/MycilaLogger @ 3.2.1
mathieucarbou/MycilaMQTT @ 6.0.5
mathieucarbou/MycilaNTP @ 6.0.5
mathieucarbou/MycilaPulseAnalyzer @ 3.0.0
mathieucarbou/MycilaPZEM004Tv3 @ 5.0.1
mathieucarbou/MycilaPZEM004Tv3 @ 6.0.0
mathieucarbou/MycilaRelay @ 4.0.1
mathieucarbou/MycilaSystem @ 4.0.1
mathieucarbou/MycilaTaskManager @ 3.2.1
Expand Down
20 changes: 10 additions & 10 deletions src/yasolr_dashboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ static dash::StatisticValue<const char*> _deviceModel(dashboard, YASOLR_LBL_011)
static dash::StatisticValue<const char*> _firmwareBuildHash(dashboard, YASOLR_LBL_013);
static dash::StatisticValue<const char*> _firmwareBuildTimestamp(dashboard, YASOLR_LBL_014);
static dash::StatisticValue<const char*> _firmwareFilename(dashboard, YASOLR_LBL_015);
static dash::StatisticValue<float, 3> _gridEnergy(dashboard, YASOLR_LBL_016);
static dash::StatisticValue<float, 3> _gridEnergyReturned(dashboard, YASOLR_LBL_017);
static dash::StatisticValue<float, 0> _gridFrequency(dashboard, YASOLR_LBL_018);
static dash::StatisticValue<uint32_t> _gridEnergy(dashboard, YASOLR_LBL_016);
static dash::StatisticValue<uint32_t> _gridEnergyReturned(dashboard, YASOLR_LBL_017);
static dash::StatisticValue<float, 1> _gridFrequency(dashboard, YASOLR_LBL_018);
static dash::StatisticValue<float, 2> _udpMessageRateBuffer(dashboard, YASOLR_LBL_157);
static dash::StatisticValue<const char*> _networkHostname(dashboard, YASOLR_LBL_019);
static dash::StatisticValue<const char*> _networkInterface(dashboard, YASOLR_LBL_020);
Expand Down Expand Up @@ -91,7 +91,7 @@ static dash::EnergyCard<float, 2> _routerTHDi(dashboard, YASOLR_LBL_039, "%");
static dash::EnergyCard<float, 0> _routerVoltage(dashboard, YASOLR_LBL_040, "V");
static dash::EnergyCard<float, 2> _routerCurrent(dashboard, YASOLR_LBL_041, "A");
static dash::EnergyCard<float, 2> _routerResistance(dashboard, YASOLR_LBL_042, "Ω");
static dash::EnergyCard<float, 3> _routerEnergy(dashboard, YASOLR_LBL_043, "kWh");
static dash::EnergyCard<uint32_t> _routerEnergy(dashboard, YASOLR_LBL_043, "Wh");
static dash::EnergyCard<float, 0> _gridPower(dashboard, YASOLR_LBL_044, "W");
static dash::TemperatureCard<float, 2> _routerDS18State(dashboard, YASOLR_LBL_045);

Expand Down Expand Up @@ -137,7 +137,7 @@ static dash::EnergyCard<float, 2> _output1THDi(dashboard, YASOLR_LBL_055, "%");
static dash::EnergyCard<float, 0> _output1Voltage(dashboard, YASOLR_LBL_056, "V");
static dash::EnergyCard<float, 2> _output1Current(dashboard, YASOLR_LBL_057, "A");
static dash::EnergyCard<float, 2> _output1Resistance(dashboard, YASOLR_LBL_058, "Ω");
static dash::EnergyCard<float, 3> _output1Energy(dashboard, YASOLR_LBL_059, "kWh");
static dash::EnergyCard<uint32_t> _output1Energy(dashboard, YASOLR_LBL_059, "Wh");
static dash::SwitchCard _output1BypassAuto(dashboard, YASOLR_LBL_064);
static dash::SwitchCard _output1Bypass(dashboard, YASOLR_LBL_051);
static dash::FeedbackCard<const char*> _output1BypassRO(dashboard, YASOLR_LBL_051);
Expand All @@ -156,7 +156,7 @@ static dash::EnergyCard<float, 2> _output2THDi(dashboard, YASOLR_LBL_055, "%");
static dash::EnergyCard<float, 0> _output2Voltage(dashboard, YASOLR_LBL_056, "V");
static dash::EnergyCard<float, 2> _output2Current(dashboard, YASOLR_LBL_057, "A");
static dash::EnergyCard<float, 2> _output2Resistance(dashboard, YASOLR_LBL_058, "Ω");
static dash::EnergyCard<float, 3> _output2Energy(dashboard, YASOLR_LBL_059, "kWh");
static dash::EnergyCard<uint32_t> _output2Energy(dashboard, YASOLR_LBL_059, "Wh");
static dash::SwitchCard _output2BypassAuto(dashboard, YASOLR_LBL_064);
static dash::SwitchCard _output2Bypass(dashboard, YASOLR_LBL_051);
static dash::FeedbackCard<const char*> _output2BypassRO(dashboard, YASOLR_LBL_051);
Expand Down Expand Up @@ -1409,7 +1409,7 @@ void YaSolR::Website::updateCards() {
_routerPower.setValue(routerMetrics.power);
_routerApparentPower.setValue(routerMetrics.apparentPower);
_routerPowerFactor.setValue(routerMetrics.powerFactor);
_routerTHDi.setValue(routerMetrics.thdi * 100.0f);
_routerTHDi.setValue(routerMetrics.thdi);
_routerVoltage.setValue(gridMetrics.voltage);
_routerCurrent.setValue(routerMetrics.current);
_routerResistance.setValue(routerMetrics.resistance);
Expand Down Expand Up @@ -1480,7 +1480,7 @@ void YaSolR::Website::updateCards() {
_output1Power.setValue(output1Measurements.power);
_output1ApparentPower.setValue(output1Measurements.apparentPower);
_output1PowerFactor.setValue(output1Measurements.powerFactor);
_output1THDi.setValue(output1Measurements.thdi * 100.0f);
_output1THDi.setValue(output1Measurements.thdi);
_output1Voltage.setValue(output1Measurements.dimmedVoltage);
_output1Current.setValue(output1Measurements.current);
_output1Resistance.setValue(output1Measurements.resistance);
Expand All @@ -1498,7 +1498,7 @@ void YaSolR::Website::updateCards() {
_output2Power.setValue(output2Measurements.power);
_output2ApparentPower.setValue(output2Measurements.apparentPower);
_output2PowerFactor.setValue(output2Measurements.powerFactor);
_output2THDi.setValue(output2Measurements.thdi * 100.0f);
_output2THDi.setValue(output2Measurements.thdi);
_output2Voltage.setValue(output2Measurements.dimmedVoltage);
_output2Current.setValue(output2Measurements.current);
_output2Resistance.setValue(output2Measurements.resistance);
Expand Down Expand Up @@ -1543,7 +1543,7 @@ void YaSolR::Website::updateCharts() {
// set new value
_gridPowerHistoryY[YASOLR_GRAPH_POINTS - 1] = std::round(grid.getPower().orElse(0));
_routedPowerHistoryY[YASOLR_GRAPH_POINTS - 1] = std::round(routerMetrics.power);
_routerTHDiHistoryY[YASOLR_GRAPH_POINTS - 1] = std::isnan(routerMetrics.thdi) ? 0 : std::round(routerMetrics.thdi * 100.0f);
_routerTHDiHistoryY[YASOLR_GRAPH_POINTS - 1] = std::isnan(routerMetrics.thdi) ? 0 : std::round(routerMetrics.thdi);

// update charts
_gridPowerHistory.setY(_gridPowerHistoryY, YASOLR_GRAPH_POINTS);
Expand Down
14 changes: 7 additions & 7 deletions src/yasolr_jsy_remote.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ void onData(AsyncUDPPacket packet) {
grid.remoteMetrics().update({
.apparentPower = doc["apparent_power"] | NAN,
.current = doc["current"] | NAN,
.energy = doc["active_energy_imported"] | NAN,
.energyReturned = doc["active_energy_returned"] | NAN,
.energy = doc["active_energy_imported"] | static_cast<uint32_t>(0),
.energyReturned = doc["active_energy_returned"] | static_cast<uint32_t>(0),
.power = doc["active_power"] | NAN,
.powerFactor = doc["power_factor"] | NAN,
.voltage = doc["voltage"] | NAN,
Expand All @@ -64,8 +64,8 @@ void onData(AsyncUDPPacket packet) {
grid.remoteMetrics().update({
.apparentPower = doc["channel2"]["apparent_power"] | NAN,
.current = doc["channel2"]["current"] | NAN,
.energy = doc["channel2"]["active_energy_imported"] | NAN,
.energyReturned = doc["channel2"]["active_energy_returned"] | NAN,
.energy = doc["channel2"]["active_energy_imported"] | static_cast<uint32_t>(0),
.energyReturned = doc["channel2"]["active_energy_returned"] | static_cast<uint32_t>(0),
.frequency = doc["channel2"]["frequency"] | NAN,
.power = doc["channel2"]["active_power"] | NAN,
.powerFactor = doc["channel2"]["power_factor"] | NAN,
Expand All @@ -74,7 +74,7 @@ void onData(AsyncUDPPacket packet) {
router.remoteMetrics().update({
.apparentPower = doc["channel1"]["apparent_power"] | NAN,
.current = doc["channel1"]["current"] | NAN,
.energy = doc["channel1"]["active_energy"] | NAN,
.energy = doc["channel1"]["active_energy"] | static_cast<uint32_t>(0),
.power = doc["channel1"]["active_power"] | NAN,
.powerFactor = doc["channel1"]["power_factor"] | NAN,
.resistance = doc["channel1"]["resistance"] | NAN,
Expand All @@ -88,8 +88,8 @@ void onData(AsyncUDPPacket packet) {
grid.remoteMetrics().update({
.apparentPower = aggregate["apparent_power"] | NAN,
.current = aggregate["current"] | NAN,
.energy = aggregate["active_energy_imported"] | NAN,
.energyReturned = aggregate["active_energy_returned"] | NAN,
.energy = aggregate["active_energy_imported"] | static_cast<uint32_t>(0),
.energyReturned = aggregate["active_energy_returned"] | static_cast<uint32_t>(0),
.frequency = aggregate["frequency"] | NAN,
.power = aggregate["active_power"] | NAN,
.powerFactor = aggregate["power_factor"] | NAN,
Expand Down
6 changes: 3 additions & 3 deletions src/yasolr_mqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,15 +369,15 @@ static void haDiscovery() {
// SENSORS

haDiscovery.publish(Mycila::HA::State("grid", "Grid Electricity", "/grid/online", YASOLR_TRUE, YASOLR_FALSE, "power"));
haDiscovery.publish(Mycila::HA::Counter("grid_energy", "Grid Energy", "/grid/energy", "energy", nullptr, "kWh"));
haDiscovery.publish(Mycila::HA::Counter("grid_energy_returned", "Grid Energy Returned", "/grid/energy_returned", "energy", nullptr, "kWh"));
haDiscovery.publish(Mycila::HA::Counter("grid_energy", "Grid Energy", "/grid/energy", "energy", nullptr, "Wh"));
haDiscovery.publish(Mycila::HA::Counter("grid_energy_returned", "Grid Energy Returned", "/grid/energy_returned", "energy", nullptr, "Wh"));
haDiscovery.publish(Mycila::HA::Counter("grid_frequency", "Grid Frequency", "/grid/frequency", "frequency", nullptr, "Hz"));
haDiscovery.publish(Mycila::HA::Gauge("grid_power", "Grid Power", "/grid/power", "power", nullptr, "W"));
haDiscovery.publish(Mycila::HA::Gauge("grid_power_virtual", "Grid Power Without Routing", "/router/virtual_grid_power", "power", nullptr, "W"));
haDiscovery.publish(Mycila::HA::Gauge("grid_power_factor", "Grid Power Factor", "/grid/power_factor", "power_factor"));
haDiscovery.publish(Mycila::HA::Gauge("grid_voltage", "Grid Voltage", "/grid/voltage", "voltage", nullptr, "V"));

haDiscovery.publish(Mycila::HA::Counter("routed_energy", "Routed Energy", "/router/energy", "energy", nullptr, "kWh"));
haDiscovery.publish(Mycila::HA::Counter("routed_energy", "Routed Energy", "/router/energy", "energy", nullptr, "Wh"));
haDiscovery.publish(Mycila::HA::Gauge("routed_power", "Routed Power", "/router/power", "power", nullptr, "W"));
haDiscovery.publish(Mycila::HA::Gauge("router_power_factor", "Router Power Factor", "/router/power_factor", "power_factor"));
haDiscovery.publish(Mycila::HA::Gauge("router_temperature", "Router Temperature", "/router/temperature", "temperature", "mdi:thermometer", "°C"));
Expand Down
8 changes: 4 additions & 4 deletions src/yasolr_pzem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ void yasolr_init_pzem() {
grid.pzemMetrics().update({
.apparentPower = NAN,
.current = NAN,
.energy = NAN,
.energyReturned = NAN,
.energy = 0,
.energyReturned = 0,
.frequency = pzemO1->data.frequency,
.power = NAN,
.powerFactor = NAN,
Expand Down Expand Up @@ -104,8 +104,8 @@ void yasolr_init_pzem() {
grid.pzemMetrics().update({
.apparentPower = NAN,
.current = NAN,
.energy = NAN,
.energyReturned = NAN,
.energy = 0,
.energyReturned = 0,
.frequency = pzemO2->data.frequency,
.power = NAN,
.powerFactor = NAN,
Expand Down

0 comments on commit 71e2c0e

Please sign in to comment.