diff --git a/esphome/components/pzemdc/pzemdc.cpp b/esphome/components/pzemdc/pzemdc.cpp index 6a31a723a1..c914edefc8 100644 --- a/esphome/components/pzemdc/pzemdc.cpp +++ b/esphome/components/pzemdc/pzemdc.cpp @@ -37,6 +37,9 @@ void PZEMDC::on_modbus_data(const std::vector &data) { uint32_t raw_power = pzem_get_32bit(4); float power = raw_power / 10.0f; // max 429496729.5 W + uint32_t raw_energy = pzem_get_32bit(8); + float energy = raw_energy / 1000.0f; // max 4294967.295 kWh + ESP_LOGD(TAG, "PZEM DC: V=%.1f V, I=%.3f A, P=%.1f W", voltage, current, power); if (this->voltage_sensor_ != nullptr) this->voltage_sensor_->publish_state(voltage); @@ -44,6 +47,8 @@ void PZEMDC::on_modbus_data(const std::vector &data) { this->current_sensor_->publish_state(current); if (this->power_sensor_ != nullptr) this->power_sensor_->publish_state(power); + if (this->energy_sensor_ != nullptr) + this->energy_sensor_->publish_state(energy); } void PZEMDC::update() { this->send(PZEM_CMD_READ_IN_REGISTERS, 0, 8); } @@ -53,6 +58,7 @@ void PZEMDC::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_); } } // namespace pzemdc diff --git a/esphome/components/pzemdc/pzemdc.h b/esphome/components/pzemdc/pzemdc.h index dff904476b..54df6c484c 100644 --- a/esphome/components/pzemdc/pzemdc.h +++ b/esphome/components/pzemdc/pzemdc.h @@ -14,6 +14,7 @@ class PZEMDC : 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_powerfactor_sensor(sensor::Sensor *powerfactor_sensor) { power_factor_sensor_ = powerfactor_sensor; } @@ -29,6 +30,7 @@ class PZEMDC : public PollingComponent, public modbus::ModbusDevice { sensor::Sensor *power_sensor_{nullptr}; sensor::Sensor *frequency_sensor_{nullptr}; sensor::Sensor *power_factor_sensor_{nullptr}; + sensor::Sensor *energy_sensor_{nullptr}; }; } // namespace pzemdc diff --git a/esphome/components/pzemdc/sensor.py b/esphome/components/pzemdc/sensor.py index 08ec688afb..4234ce19ad 100644 --- a/esphome/components/pzemdc/sensor.py +++ b/esphome/components/pzemdc/sensor.py @@ -5,14 +5,18 @@ from esphome.const import ( CONF_CURRENT, CONF_ID, CONF_POWER, + CONF_ENERGY, CONF_VOLTAGE, DEVICE_CLASS_CURRENT, + DEVICE_CLASS_ENERGY, DEVICE_CLASS_POWER, DEVICE_CLASS_VOLTAGE, STATE_CLASS_MEASUREMENT, + STATE_CLASS_TOTAL_INCREASING, UNIT_VOLT, UNIT_AMPERE, UNIT_WATT, + UNIT_KILOWATT_HOURS, ) AUTO_LOAD = ["modbus"] @@ -42,6 +46,12 @@ CONFIG_SCHEMA = ( device_class=DEVICE_CLASS_POWER, state_class=STATE_CLASS_MEASUREMENT, ), + cv.Optional(CONF_ENERGY): sensor.sensor_schema( + unit_of_measurement=UNIT_KILOWATT_HOURS, + accuracy_decimals=3, + device_class=DEVICE_CLASS_ENERGY, + state_class=STATE_CLASS_TOTAL_INCREASING, + ), } ) .extend(cv.polling_component_schema("60s")) @@ -66,3 +76,7 @@ async def to_code(config): conf = config[CONF_POWER] sens = await sensor.new_sensor(conf) cg.add(var.set_power_sensor(sens)) + if CONF_ENERGY in config: + conf = config[CONF_ENERGY] + sens = await sensor.new_sensor(conf) + cg.add(var.set_energy_sensor(sens)) diff --git a/tests/test3.yaml b/tests/test3.yaml index 16489335af..5776e1fc10 100644 --- a/tests/test3.yaml +++ b/tests/test3.yaml @@ -583,6 +583,8 @@ sensor: name: PZEMDC Current power: name: PZEMDC Power + energy: + name: PZEMDC Energy - platform: tmp102 name: TMP102 Temperature - platform: hm3301 diff --git a/tests/test5.yaml b/tests/test5.yaml index 5708f1f093..ec3fef5e19 100644 --- a/tests/test5.yaml +++ b/tests/test5.yaml @@ -477,7 +477,6 @@ sensor: acceleration_mode: low store_baseline: true address: 0x69 - - platform: mcp9600 thermocouple_type: K hot_junction: