Adding sdm_meter ability to report total power (#2959)

This commit is contained in:
MiKuBB 2022-01-10 00:23:01 +01:00 committed by GitHub
parent a4431abea8
commit 9e8b701dea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 2 deletions

View File

@ -57,15 +57,19 @@ void SDMMeter::on_modbus_data(const std::vector<uint8_t> &data) {
phase.phase_angle_sensor_->publish_state(phase_angle);
}
float total_power = sdm_meter_get_float(SDM_TOTAL_SYSTEM_POWER * 2);
float frequency = sdm_meter_get_float(SDM_FREQUENCY * 2);
float import_active_energy = sdm_meter_get_float(SDM_IMPORT_ACTIVE_ENERGY * 2);
float export_active_energy = sdm_meter_get_float(SDM_EXPORT_ACTIVE_ENERGY * 2);
float import_reactive_energy = sdm_meter_get_float(SDM_IMPORT_REACTIVE_ENERGY * 2);
float export_reactive_energy = sdm_meter_get_float(SDM_EXPORT_REACTIVE_ENERGY * 2);
ESP_LOGD(TAG, "SDMMeter: F=%.3f Hz, Im.A.E=%.3f Wh, Ex.A.E=%.3f Wh, Im.R.E=%.3f VARh, Ex.R.E=%.3f VARh", frequency,
import_active_energy, export_active_energy, import_reactive_energy, export_reactive_energy);
ESP_LOGD(TAG, "SDMMeter: F=%.3f Hz, Im.A.E=%.3f Wh, Ex.A.E=%.3f Wh, Im.R.E=%.3f VARh, Ex.R.E=%.3f VARh, T.P=%.3f W",
frequency, import_active_energy, export_active_energy, import_reactive_energy, export_reactive_energy,
total_power);
if (this->total_power_sensor_ != nullptr)
this->total_power_sensor_->publish_state(total_power);
if (this->frequency_sensor_ != nullptr)
this->frequency_sensor_->publish_state(frequency);
if (this->import_active_energy_sensor_ != nullptr)
@ -95,6 +99,7 @@ void SDMMeter::dump_config() {
LOG_SENSOR(" ", "Power Factor", phase.power_factor_sensor_);
LOG_SENSOR(" ", "Phase Angle", phase.phase_angle_sensor_);
}
LOG_SENSOR(" ", "Total Power", this->total_power_sensor_);
LOG_SENSOR(" ", "Frequency", this->frequency_sensor_);
LOG_SENSOR(" ", "Import Active Energy", this->import_active_energy_sensor_);
LOG_SENSOR(" ", "Export Active Energy", this->export_active_energy_sensor_);

View File

@ -37,6 +37,7 @@ class SDMMeter : public PollingComponent, public modbus::ModbusDevice {
this->phases_[phase].setup = true;
this->phases_[phase].phase_angle_sensor_ = phase_angle_sensor;
}
void set_total_power_sensor(sensor::Sensor *total_power_sensor) { this->total_power_sensor_ = total_power_sensor; }
void set_frequency_sensor(sensor::Sensor *frequency_sensor) { this->frequency_sensor_ = frequency_sensor; }
void set_import_active_energy_sensor(sensor::Sensor *import_active_energy_sensor) {
this->import_active_energy_sensor_ = import_active_energy_sensor;
@ -69,6 +70,7 @@ class SDMMeter : public PollingComponent, public modbus::ModbusDevice {
sensor::Sensor *phase_angle_sensor_{nullptr};
} phases_[3];
sensor::Sensor *frequency_sensor_{nullptr};
sensor::Sensor *total_power_sensor_{nullptr};
sensor::Sensor *import_active_energy_sensor_{nullptr};
sensor::Sensor *export_active_energy_sensor_{nullptr};
sensor::Sensor *import_reactive_energy_sensor_{nullptr};

View File

@ -8,6 +8,7 @@ from esphome.const import (
CONF_CURRENT,
CONF_EXPORT_ACTIVE_ENERGY,
CONF_EXPORT_REACTIVE_ENERGY,
CONF_TOTAL_POWER,
CONF_FREQUENCY,
CONF_ID,
CONF_IMPORT_ACTIVE_ENERGY,
@ -98,6 +99,12 @@ CONFIG_SCHEMA = (
accuracy_decimals=3,
state_class=STATE_CLASS_MEASUREMENT,
),
cv.Optional(CONF_TOTAL_POWER): sensor.sensor_schema(
unit_of_measurement=UNIT_WATT,
accuracy_decimals=2,
device_class=DEVICE_CLASS_POWER,
state_class=STATE_CLASS_MEASUREMENT,
),
cv.Optional(CONF_IMPORT_ACTIVE_ENERGY): sensor.sensor_schema(
unit_of_measurement=UNIT_KILOWATT_HOURS,
accuracy_decimals=2,
@ -132,6 +139,10 @@ async def to_code(config):
await cg.register_component(var, config)
await modbus.register_modbus_device(var, config)
if CONF_TOTAL_POWER in config:
sens = await sensor.new_sensor(config[CONF_TOTAL_POWER])
cg.add(var.set_total_power_sensor(sens))
if CONF_FREQUENCY in config:
sens = await sensor.new_sensor(config[CONF_FREQUENCY])
cg.add(var.set_frequency_sensor(sens))

View File

@ -685,6 +685,7 @@ CONF_TOLERANCE = "tolerance"
CONF_TOPIC = "topic"
CONF_TOPIC_PREFIX = "topic_prefix"
CONF_TOTAL = "total"
CONF_TOTAL_POWER = "total_power"
CONF_TRACES = "traces"
CONF_TRANSITION_LENGTH = "transition_length"
CONF_TRIGGER_ID = "trigger_id"