Add energy to pzemdc (#3626)

* added energy to pzemdc

* fixed calculation

* added test

* moved tests

---------

Co-authored-by: Andreas Hergert <andreas.hergert@otrs.com>
This commit is contained in:
Andreas Hergert 2023-02-23 18:38:34 +01:00 committed by GitHub
parent 6704b2cedf
commit 4d674392e8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 24 additions and 1 deletions

View file

@ -37,6 +37,9 @@ void PZEMDC::on_modbus_data(const std::vector<uint8_t> &data) {
uint32_t raw_power = pzem_get_32bit(4); uint32_t raw_power = pzem_get_32bit(4);
float power = raw_power / 10.0f; // max 429496729.5 W 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); ESP_LOGD(TAG, "PZEM DC: V=%.1f V, I=%.3f A, P=%.1f W", voltage, current, power);
if (this->voltage_sensor_ != nullptr) if (this->voltage_sensor_ != nullptr)
this->voltage_sensor_->publish_state(voltage); this->voltage_sensor_->publish_state(voltage);
@ -44,6 +47,8 @@ void PZEMDC::on_modbus_data(const std::vector<uint8_t> &data) {
this->current_sensor_->publish_state(current); this->current_sensor_->publish_state(current);
if (this->power_sensor_ != nullptr) if (this->power_sensor_ != nullptr)
this->power_sensor_->publish_state(power); 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); } 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("", "Voltage", this->voltage_sensor_);
LOG_SENSOR("", "Current", this->current_sensor_); LOG_SENSOR("", "Current", this->current_sensor_);
LOG_SENSOR("", "Power", this->power_sensor_); LOG_SENSOR("", "Power", this->power_sensor_);
LOG_SENSOR("", "Energy", this->energy_sensor_);
} }
} // namespace pzemdc } // namespace pzemdc

View file

@ -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_voltage_sensor(sensor::Sensor *voltage_sensor) { voltage_sensor_ = voltage_sensor; }
void set_current_sensor(sensor::Sensor *current_sensor) { current_sensor_ = current_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_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_frequency_sensor(sensor::Sensor *frequency_sensor) { frequency_sensor_ = frequency_sensor; }
void set_powerfactor_sensor(sensor::Sensor *powerfactor_sensor) { power_factor_sensor_ = powerfactor_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 *power_sensor_{nullptr};
sensor::Sensor *frequency_sensor_{nullptr}; sensor::Sensor *frequency_sensor_{nullptr};
sensor::Sensor *power_factor_sensor_{nullptr}; sensor::Sensor *power_factor_sensor_{nullptr};
sensor::Sensor *energy_sensor_{nullptr};
}; };
} // namespace pzemdc } // namespace pzemdc

View file

@ -5,14 +5,18 @@ from esphome.const import (
CONF_CURRENT, CONF_CURRENT,
CONF_ID, CONF_ID,
CONF_POWER, CONF_POWER,
CONF_ENERGY,
CONF_VOLTAGE, CONF_VOLTAGE,
DEVICE_CLASS_CURRENT, DEVICE_CLASS_CURRENT,
DEVICE_CLASS_ENERGY,
DEVICE_CLASS_POWER, DEVICE_CLASS_POWER,
DEVICE_CLASS_VOLTAGE, DEVICE_CLASS_VOLTAGE,
STATE_CLASS_MEASUREMENT, STATE_CLASS_MEASUREMENT,
STATE_CLASS_TOTAL_INCREASING,
UNIT_VOLT, UNIT_VOLT,
UNIT_AMPERE, UNIT_AMPERE,
UNIT_WATT, UNIT_WATT,
UNIT_KILOWATT_HOURS,
) )
AUTO_LOAD = ["modbus"] AUTO_LOAD = ["modbus"]
@ -42,6 +46,12 @@ CONFIG_SCHEMA = (
device_class=DEVICE_CLASS_POWER, device_class=DEVICE_CLASS_POWER,
state_class=STATE_CLASS_MEASUREMENT, 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")) .extend(cv.polling_component_schema("60s"))
@ -66,3 +76,7 @@ async def to_code(config):
conf = config[CONF_POWER] conf = config[CONF_POWER]
sens = await sensor.new_sensor(conf) sens = await sensor.new_sensor(conf)
cg.add(var.set_power_sensor(sens)) 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))

View file

@ -583,6 +583,8 @@ sensor:
name: PZEMDC Current name: PZEMDC Current
power: power:
name: PZEMDC Power name: PZEMDC Power
energy:
name: PZEMDC Energy
- platform: tmp102 - platform: tmp102
name: TMP102 Temperature name: TMP102 Temperature
- platform: hm3301 - platform: hm3301

View file

@ -477,7 +477,6 @@ sensor:
acceleration_mode: low acceleration_mode: low
store_baseline: true store_baseline: true
address: 0x69 address: 0x69
- platform: mcp9600 - platform: mcp9600
thermocouple_type: K thermocouple_type: K
hot_junction: hot_junction: