From 8621eb3c423f8984ec3deb3480c1614a36bd1d8f Mon Sep 17 00:00:00 2001 From: Mathieu Carbou Date: Mon, 25 Mar 2024 23:54:28 +0100 Subject: [PATCH] Fix #18: Add support for Grid Power, Voltage, Power Factor, Energy and Returned Energ from Website, HA, MQTT and REST API --- include/YaSolRWebsite.h | 3 +++ lib/MycilaGrid/MycilaGrid.cpp | 15 +++++++++++++++ lib/MycilaGrid/MycilaGrid.h | 3 +++ src/YaSolR_HADiscovery.cpp | 5 +++++ src/YaSolR_MQTT_Publish.cpp | 16 ++++++++++------ src/YaSolR_Website_Update.cpp | 3 +++ 6 files changed, 39 insertions(+), 6 deletions(-) diff --git a/include/YaSolRWebsite.h b/include/YaSolRWebsite.h index fd3be9ac..9591b255 100644 --- a/include/YaSolRWebsite.h +++ b/include/YaSolRWebsite.h @@ -53,6 +53,9 @@ namespace YaSolR { Statistic _gridVoltStat = Statistic(&dashboard, "Grid Voltage"); Statistic _gridFreqStat = Statistic(&dashboard, "Grid Frequency"); + Statistic _gridPowerFactorStat = Statistic(&dashboard, "Grid Power Factor"); + Statistic _gridEnergyStat = Statistic(&dashboard, "Grid Energy"); + Statistic _gridEnergyReturnedStat = Statistic(&dashboard, "Grid Energy Returned"); Statistic _output1SwitchCountStat = Statistic(&dashboard, "Output 1 Switch Count"); Statistic _output2SwitchCountStat = Statistic(&dashboard, "Output 2 Switch Count"); diff --git a/lib/MycilaGrid/MycilaGrid.cpp b/lib/MycilaGrid/MycilaGrid.cpp index 934ff8a9..f35a4ec8 100644 --- a/lib/MycilaGrid/MycilaGrid.cpp +++ b/lib/MycilaGrid/MycilaGrid.cpp @@ -74,6 +74,18 @@ float Mycila::GridClass::getActivePower() const { return 0; } +float Mycila::GridClass::getActiveEnergy() const { + return _jsy != nullptr ? _jsy->getEnergy2() : 0; +} + +float Mycila::GridClass::getActiveEnergyReturned() const { + return _jsy != nullptr ? _jsy->getEnergyReturned2() : 0; +} + +float Mycila::GridClass::getPowerFactor() const { + return _jsy != nullptr ? _jsy->getPowerFactor2() : 0; +} + float Mycila::GridClass::getVoltage() const { // 1. MQTT if (!_mqttGridVoltageTopic.isEmpty() && !isMQTTGridDataExpired()) @@ -87,9 +99,12 @@ float Mycila::GridClass::getVoltage() const { } void Mycila::GridClass::toJson(const JsonObject& root) const { + root["energy_returned"] = getActiveEnergyReturned(); + root["energy"] = getActiveEnergy(); root["frequency"] = getFrequency(); root["online"] = isConnected(); root["power"] = getActivePower(); + root["power_factor"] = getPowerFactor(); root["voltage"] = getVoltage(); if (_jsy != nullptr) _jsy->toJson(root["jsy"].to()); diff --git a/lib/MycilaGrid/MycilaGrid.h b/lib/MycilaGrid/MycilaGrid.h index 74fe4bf8..aba6ec1d 100644 --- a/lib/MycilaGrid/MycilaGrid.h +++ b/lib/MycilaGrid/MycilaGrid.h @@ -27,6 +27,9 @@ namespace Mycila { float getFrequency() const; float getActivePower() const; + float getActiveEnergy() const; + float getActiveEnergyReturned() const; + float getPowerFactor() const; float getVoltage() const; bool isConnected() const { return getVoltage() > 0; } diff --git a/src/YaSolR_HADiscovery.cpp b/src/YaSolR_HADiscovery.cpp index 46bea1df..4cb7c16e 100644 --- a/src/YaSolR_HADiscovery.cpp +++ b/src/YaSolR_HADiscovery.cpp @@ -100,6 +100,11 @@ void YaSolR::YaSolRClass::publishHADiscovery() { // SENSORS Mycila::HADiscovery.publish(Mycila::HAState("grid", "Grid Electricity", "/grid/online", YASOLR_TRUE, YASOLR_FALSE, "connectivity")); + Mycila::HADiscovery.publish(Mycila::HAGauge("grid_energy", "Grid Energy", "/grid/energy", "energy", nullptr, "kWh")); + Mycila::HADiscovery.publish(Mycila::HAGauge("grid_energy_returned", "Grid Energy Returned", "/grid/energy_returned", "energy", nullptr, "kWh")); + Mycila::HADiscovery.publish(Mycila::HAGauge("grid_power", "Grid Power", "/grid/power", "power", nullptr, "W")); + Mycila::HADiscovery.publish(Mycila::HAGauge("grid_power_factor", "Grid Power Factor", "/grid/power_factor", "power_factor")); + Mycila::HADiscovery.publish(Mycila::HAGauge("grid_voltage", "Grid Voltage", "/grid/voltage", "voltage")); Mycila::HADiscovery.publish(Mycila::HAGauge("router_energy", "Routed Energy", "/router/energy", "energy", nullptr, "kWh")); Mycila::HADiscovery.publish(Mycila::HAGauge("router_power", "Total Routed Power", "/router/power", "power", nullptr, "W")); diff --git a/src/YaSolR_MQTT_Publish.cpp b/src/YaSolR_MQTT_Publish.cpp index 4766734b..ca7741b7 100644 --- a/src/YaSolR_MQTT_Publish.cpp +++ b/src/YaSolR_MQTT_Publish.cpp @@ -35,8 +35,12 @@ void YaSolR::YaSolRClass::publishMQTT() { // grid { - Mycila::MQTT.publish(baseTopic + "/grid/power", String(Mycila::Grid.getActivePower())); + Mycila::MQTT.publish(baseTopic + "/grid/power", String(Mycila::Grid.getActivePower(), 3)); + Mycila::MQTT.publish(baseTopic + "/grid/energy", String(Mycila::Grid.getActiveEnergy(), 3)); + Mycila::MQTT.publish(baseTopic + "/grid/energy_returned", String(Mycila::Grid.getActiveEnergyReturned(), 3)); + Mycila::MQTT.publish(baseTopic + "/grid/power_factor", String(Mycila::Grid.getPowerFactor(), 3)); Mycila::MQTT.publish(baseTopic + "/grid/online", YASOLR_BOOL(Mycila::Grid.isConnected())); + Mycila::MQTT.publish(baseTopic + "/grid/voltage", String(Mycila::Grid.getVoltage(), 3)); } yield(); @@ -78,11 +82,11 @@ void YaSolR::YaSolRClass::publishMQTT() { // router { - Mycila::MQTT.publish(baseTopic + "/router/energy", String(Mycila::Router.getTotalRoutedEnergy())); - Mycila::MQTT.publish(baseTopic + "/router/power_factor", String(Mycila::Router.getTotalPowerFactor())); - Mycila::MQTT.publish(baseTopic + "/router/power", String(Mycila::Router.getTotalRoutedPower())); - Mycila::MQTT.publish(baseTopic + "/router/thdi", String(Mycila::Router.getTotalTHDi())); - Mycila::MQTT.publish(baseTopic + "/router/virtual_grid_power", String(Mycila::Router.getVirtualGridPower())); + Mycila::MQTT.publish(baseTopic + "/router/energy", String(Mycila::Router.getTotalRoutedEnergy(), 3)); + Mycila::MQTT.publish(baseTopic + "/router/power_factor", String(Mycila::Router.getTotalPowerFactor(), 3)); + Mycila::MQTT.publish(baseTopic + "/router/power", String(Mycila::Router.getTotalRoutedPower(), 3)); + Mycila::MQTT.publish(baseTopic + "/router/thdi", String(Mycila::Router.getTotalTHDi(), 3)); + Mycila::MQTT.publish(baseTopic + "/router/virtual_grid_power", String(Mycila::Router.getVirtualGridPower(), 3)); } yield(); diff --git a/src/YaSolR_Website_Update.cpp b/src/YaSolR_Website_Update.cpp index 25e3455b..c398b0d9 100644 --- a/src/YaSolR_Website_Update.cpp +++ b/src/YaSolR_Website_Update.cpp @@ -21,6 +21,9 @@ void YaSolR::WebsiteClass::_update(bool skipWebSocketPush) { _ethMACStat.set(ESPConnect.getMACAddress(ESPConnectMode::ETH).c_str()); _gridFreqStat.set((String(Mycila::Grid.getFrequency()) + " Hz").c_str()); _gridVoltStat.set((String(Mycila::Grid.getVoltage()) + " V").c_str()); + _gridPowerFactorStat.set(String(Mycila::Grid.getPowerFactor()).c_str()); + _gridEnergyStat.set((String(Mycila::Grid.getActiveEnergy(), 3) + " kWh").c_str()); + _gridEnergyReturnedStat.set((String(Mycila::Grid.getActiveEnergyReturned(), 3) + " kWh").c_str()); _heapMemoryUsageStat.set((String(memory.usage) + " %").c_str()); _heapMemoryUsedStat.set((String(memory.used) + " bytes").c_str()); _netModeStat.set(mode == ESPConnectMode::AP ? "AP" : (mode == ESPConnectMode::STA ? "WiFi" : (mode == ESPConnectMode::ETH ? "Ethernet" : "")));