mirror of
https://github.com/esphome/esphome.git
synced 2024-11-28 17:54:13 +01:00
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:
parent
6704b2cedf
commit
4d674392e8
5 changed files with 24 additions and 1 deletions
|
@ -37,6 +37,9 @@ void PZEMDC::on_modbus_data(const std::vector<uint8_t> &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<uint8_t> &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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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))
|
||||
|
|
|
@ -583,6 +583,8 @@ sensor:
|
|||
name: PZEMDC Current
|
||||
power:
|
||||
name: PZEMDC Power
|
||||
energy:
|
||||
name: PZEMDC Energy
|
||||
- platform: tmp102
|
||||
name: TMP102 Temperature
|
||||
- platform: hm3301
|
||||
|
|
|
@ -477,7 +477,6 @@ sensor:
|
|||
acceleration_mode: low
|
||||
store_baseline: true
|
||||
address: 0x69
|
||||
|
||||
- platform: mcp9600
|
||||
thermocouple_type: K
|
||||
hot_junction:
|
||||
|
|
Loading…
Reference in a new issue