From c60989a7be75989e55af166d8496cb5ff27b4080 Mon Sep 17 00:00:00 2001 From: Pavel <205196+yekm@users.noreply.github.com> Date: Thu, 12 Mar 2020 23:37:57 +0300 Subject: [PATCH] pzemac total energy support (#933) * add energy support in pzemac sensor Co-authored-by: Sergio Mayoral Martinez Co-authored-by: t151602 Co-authored-by: Otto Winter --- esphome/components/pzemac/pzemac.cpp | 10 ++++++++-- esphome/components/pzemac/pzemac.h | 2 ++ esphome/components/pzemac/sensor.py | 8 +++++++- esphome/const.py | 2 ++ tests/test3.yaml | 2 ++ 5 files changed, 21 insertions(+), 3 deletions(-) diff --git a/esphome/components/pzemac/pzemac.cpp b/esphome/components/pzemac/pzemac.cpp index f05ce15711..c79508d22f 100644 --- a/esphome/components/pzemac/pzemac.cpp +++ b/esphome/components/pzemac/pzemac.cpp @@ -19,6 +19,7 @@ void PZEMAC::on_modbus_data(const std::vector &data) { // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 // 01 04 14 08 D1 00 6C 00 00 00 F4 00 00 00 26 00 00 01 F4 00 64 00 00 51 34 // Id Cc Sz Volt- Current---- Power------ Energy----- Frequ PFact Alarm Crc-- + // 0 2 6 10 14 16 auto pzem_get_16bit = [&](size_t i) -> uint16_t { return (uint16_t(data[i + 0]) << 8) | (uint16_t(data[i + 1]) << 0); @@ -36,20 +37,24 @@ void PZEMAC::on_modbus_data(const std::vector &data) { uint32_t raw_active_power = pzem_get_32bit(6); float active_power = raw_active_power / 10.0f; // max 429496729.5 W + float active_energy = static_cast(pzem_get_32bit(10)); + uint16_t raw_frequency = pzem_get_16bit(14); float frequency = raw_frequency / 10.0f; uint16_t raw_power_factor = pzem_get_16bit(16); float power_factor = raw_power_factor / 100.0f; - ESP_LOGD(TAG, "PZEM AC: V=%.1f V, I=%.3f A, P=%.1f W, F=%.1f Hz, PF=%.2f", voltage, current, active_power, frequency, - power_factor); + ESP_LOGD(TAG, "PZEM AC: V=%.1f V, I=%.3f A, P=%.1f W, E=%.1f Wh, F=%.1f Hz, PF=%.2f", voltage, current, active_power, + active_energy, frequency, power_factor); if (this->voltage_sensor_ != nullptr) this->voltage_sensor_->publish_state(voltage); if (this->current_sensor_ != nullptr) this->current_sensor_->publish_state(current); if (this->power_sensor_ != nullptr) this->power_sensor_->publish_state(active_power); + if (this->energy_sensor_ != nullptr) + this->energy_sensor_->publish_state(active_energy); if (this->frequency_sensor_ != nullptr) this->frequency_sensor_->publish_state(frequency); if (this->power_factor_sensor_ != nullptr) @@ -63,6 +68,7 @@ void PZEMAC::dump_config() { LOG_SENSOR("", "Voltage", this->voltage_sensor_); LOG_SENSOR("", "Current", this->current_sensor_); LOG_SENSOR("", "Power", this->power_sensor_); + LOG_SENSOR("", "Energy", this->energy_sensor_); LOG_SENSOR("", "Frequency", this->frequency_sensor_); LOG_SENSOR("", "Power Factor", this->power_factor_sensor_); } diff --git a/esphome/components/pzemac/pzemac.h b/esphome/components/pzemac/pzemac.h index d396b7cddf..07f661535f 100644 --- a/esphome/components/pzemac/pzemac.h +++ b/esphome/components/pzemac/pzemac.h @@ -12,6 +12,7 @@ class PZEMAC : public PollingComponent, public modbus::ModbusDevice { void set_voltage_sensor(sensor::Sensor *voltage_sensor) { voltage_sensor_ = voltage_sensor; } void set_current_sensor(sensor::Sensor *current_sensor) { current_sensor_ = current_sensor; } void set_power_sensor(sensor::Sensor *power_sensor) { power_sensor_ = power_sensor; } + void set_energy_sensor(sensor::Sensor *energy_sensor) { energy_sensor_ = energy_sensor; } void set_frequency_sensor(sensor::Sensor *frequency_sensor) { frequency_sensor_ = frequency_sensor; } void set_power_factor_sensor(sensor::Sensor *power_factor_sensor) { power_factor_sensor_ = power_factor_sensor; } @@ -25,6 +26,7 @@ class PZEMAC : public PollingComponent, public modbus::ModbusDevice { sensor::Sensor *voltage_sensor_; sensor::Sensor *current_sensor_; sensor::Sensor *power_sensor_; + sensor::Sensor *energy_sensor_; sensor::Sensor *frequency_sensor_; sensor::Sensor *power_factor_sensor_; }; diff --git a/esphome/components/pzemac/sensor.py b/esphome/components/pzemac/sensor.py index 54eb01e085..e3d2b90742 100644 --- a/esphome/components/pzemac/sensor.py +++ b/esphome/components/pzemac/sensor.py @@ -3,7 +3,8 @@ import esphome.config_validation as cv from esphome.components import sensor, modbus from esphome.const import CONF_CURRENT, CONF_ID, CONF_POWER, CONF_VOLTAGE, \ CONF_FREQUENCY, UNIT_VOLT, ICON_FLASH, UNIT_AMPERE, UNIT_WATT, UNIT_EMPTY, \ - ICON_POWER, CONF_POWER_FACTOR, ICON_CURRENT_AC, UNIT_HERTZ + ICON_POWER, CONF_POWER_FACTOR, ICON_CURRENT_AC, UNIT_HERTZ, \ + CONF_ENERGY, UNIT_WATT_HOURS, ICON_COUNTER AUTO_LOAD = ['modbus'] @@ -15,6 +16,7 @@ CONFIG_SCHEMA = cv.Schema({ cv.Optional(CONF_VOLTAGE): sensor.sensor_schema(UNIT_VOLT, ICON_FLASH, 1), cv.Optional(CONF_CURRENT): sensor.sensor_schema(UNIT_AMPERE, ICON_CURRENT_AC, 3), cv.Optional(CONF_POWER): sensor.sensor_schema(UNIT_WATT, ICON_POWER, 1), + cv.Optional(CONF_ENERGY): sensor.sensor_schema(UNIT_WATT_HOURS, ICON_COUNTER, 0), cv.Optional(CONF_FREQUENCY): sensor.sensor_schema(UNIT_HERTZ, ICON_CURRENT_AC, 1), cv.Optional(CONF_POWER_FACTOR): sensor.sensor_schema(UNIT_EMPTY, ICON_FLASH, 2), }).extend(cv.polling_component_schema('60s')).extend(modbus.modbus_device_schema(0x01)) @@ -37,6 +39,10 @@ def to_code(config): conf = config[CONF_POWER] sens = yield sensor.new_sensor(conf) cg.add(var.set_power_sensor(sens)) + if CONF_ENERGY in config: + conf = config[CONF_ENERGY] + sens = yield sensor.new_sensor(conf) + cg.add(var.set_energy_sensor(sens)) if CONF_FREQUENCY in config: conf = config[CONF_FREQUENCY] sens = yield sensor.new_sensor(conf) diff --git a/esphome/const.py b/esphome/const.py index e747371710..36ffbed254 100644 --- a/esphome/const.py +++ b/esphome/const.py @@ -145,6 +145,7 @@ CONF_EFFECTS = 'effects' CONF_ELSE = 'else' CONF_ENABLE_PIN = 'enable_pin' CONF_ENABLE_TIME = 'enable_time' +CONF_ENERGY = 'energy' CONF_ENTITY_ID = 'entity_id' CONF_ESP8266_RESTORE_FROM_FLASH = 'esp8266_restore_from_flash' CONF_ESPHOME = 'esphome' @@ -580,6 +581,7 @@ UNIT_VOLT = 'V' UNIT_VOLT_AMPS = 'VA' UNIT_VOLT_AMPS_REACTIVE = 'VAR' UNIT_WATT = 'W' +UNIT_WATT_HOURS = 'Wh' DEVICE_CLASS_CONNECTIVITY = 'connectivity' DEVICE_CLASS_MOVING = 'moving' diff --git a/tests/test3.yaml b/tests/test3.yaml index 9c66242b4e..2e1dd3f078 100644 --- a/tests/test3.yaml +++ b/tests/test3.yaml @@ -335,6 +335,8 @@ sensor: name: "PZEMAC Current" power: name: "PZEMAC Power" + energy: + name: "PZEMAC Energy" frequency: name: "PZEMAC Frequency" power_factor: