mirror of
https://github.com/esphome/esphome.git
synced 2024-11-10 10:00:36 +01:00
pzemac total energy support (#933)
* add energy support in pzemac sensor Co-authored-by: Sergio Mayoral Martinez <sergiomayoralmartinez@gmail.com> Co-authored-by: t151602 <sergio.mayoralmartinez@telefonica.com> Co-authored-by: Otto Winter <otto@otto-winter.com>
This commit is contained in:
parent
11b727fdf7
commit
c60989a7be
@ -19,6 +19,7 @@ void PZEMAC::on_modbus_data(const std::vector<uint8_t> &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
|
// 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
|
// 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--
|
// 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 {
|
auto pzem_get_16bit = [&](size_t i) -> uint16_t {
|
||||||
return (uint16_t(data[i + 0]) << 8) | (uint16_t(data[i + 1]) << 0);
|
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<uint8_t> &data) {
|
|||||||
uint32_t raw_active_power = pzem_get_32bit(6);
|
uint32_t raw_active_power = pzem_get_32bit(6);
|
||||||
float active_power = raw_active_power / 10.0f; // max 429496729.5 W
|
float active_power = raw_active_power / 10.0f; // max 429496729.5 W
|
||||||
|
|
||||||
|
float active_energy = static_cast<float>(pzem_get_32bit(10));
|
||||||
|
|
||||||
uint16_t raw_frequency = pzem_get_16bit(14);
|
uint16_t raw_frequency = pzem_get_16bit(14);
|
||||||
float frequency = raw_frequency / 10.0f;
|
float frequency = raw_frequency / 10.0f;
|
||||||
|
|
||||||
uint16_t raw_power_factor = pzem_get_16bit(16);
|
uint16_t raw_power_factor = pzem_get_16bit(16);
|
||||||
float power_factor = raw_power_factor / 100.0f;
|
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,
|
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,
|
||||||
power_factor);
|
active_energy, frequency, power_factor);
|
||||||
if (this->voltage_sensor_ != nullptr)
|
if (this->voltage_sensor_ != nullptr)
|
||||||
this->voltage_sensor_->publish_state(voltage);
|
this->voltage_sensor_->publish_state(voltage);
|
||||||
if (this->current_sensor_ != nullptr)
|
if (this->current_sensor_ != nullptr)
|
||||||
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(active_power);
|
this->power_sensor_->publish_state(active_power);
|
||||||
|
if (this->energy_sensor_ != nullptr)
|
||||||
|
this->energy_sensor_->publish_state(active_energy);
|
||||||
if (this->frequency_sensor_ != nullptr)
|
if (this->frequency_sensor_ != nullptr)
|
||||||
this->frequency_sensor_->publish_state(frequency);
|
this->frequency_sensor_->publish_state(frequency);
|
||||||
if (this->power_factor_sensor_ != nullptr)
|
if (this->power_factor_sensor_ != nullptr)
|
||||||
@ -63,6 +68,7 @@ void PZEMAC::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_);
|
||||||
LOG_SENSOR("", "Frequency", this->frequency_sensor_);
|
LOG_SENSOR("", "Frequency", this->frequency_sensor_);
|
||||||
LOG_SENSOR("", "Power Factor", this->power_factor_sensor_);
|
LOG_SENSOR("", "Power Factor", this->power_factor_sensor_);
|
||||||
}
|
}
|
||||||
|
@ -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_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_power_factor_sensor(sensor::Sensor *power_factor_sensor) { power_factor_sensor_ = power_factor_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 *voltage_sensor_;
|
||||||
sensor::Sensor *current_sensor_;
|
sensor::Sensor *current_sensor_;
|
||||||
sensor::Sensor *power_sensor_;
|
sensor::Sensor *power_sensor_;
|
||||||
|
sensor::Sensor *energy_sensor_;
|
||||||
sensor::Sensor *frequency_sensor_;
|
sensor::Sensor *frequency_sensor_;
|
||||||
sensor::Sensor *power_factor_sensor_;
|
sensor::Sensor *power_factor_sensor_;
|
||||||
};
|
};
|
||||||
|
@ -3,7 +3,8 @@ import esphome.config_validation as cv
|
|||||||
from esphome.components import sensor, modbus
|
from esphome.components import sensor, modbus
|
||||||
from esphome.const import CONF_CURRENT, CONF_ID, CONF_POWER, CONF_VOLTAGE, \
|
from esphome.const import CONF_CURRENT, CONF_ID, CONF_POWER, CONF_VOLTAGE, \
|
||||||
CONF_FREQUENCY, UNIT_VOLT, ICON_FLASH, UNIT_AMPERE, UNIT_WATT, UNIT_EMPTY, \
|
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']
|
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_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_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_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_FREQUENCY): sensor.sensor_schema(UNIT_HERTZ, ICON_CURRENT_AC, 1),
|
||||||
cv.Optional(CONF_POWER_FACTOR): sensor.sensor_schema(UNIT_EMPTY, ICON_FLASH, 2),
|
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))
|
}).extend(cv.polling_component_schema('60s')).extend(modbus.modbus_device_schema(0x01))
|
||||||
@ -37,6 +39,10 @@ def to_code(config):
|
|||||||
conf = config[CONF_POWER]
|
conf = config[CONF_POWER]
|
||||||
sens = yield sensor.new_sensor(conf)
|
sens = yield 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 = yield sensor.new_sensor(conf)
|
||||||
|
cg.add(var.set_energy_sensor(sens))
|
||||||
if CONF_FREQUENCY in config:
|
if CONF_FREQUENCY in config:
|
||||||
conf = config[CONF_FREQUENCY]
|
conf = config[CONF_FREQUENCY]
|
||||||
sens = yield sensor.new_sensor(conf)
|
sens = yield sensor.new_sensor(conf)
|
||||||
|
@ -145,6 +145,7 @@ CONF_EFFECTS = 'effects'
|
|||||||
CONF_ELSE = 'else'
|
CONF_ELSE = 'else'
|
||||||
CONF_ENABLE_PIN = 'enable_pin'
|
CONF_ENABLE_PIN = 'enable_pin'
|
||||||
CONF_ENABLE_TIME = 'enable_time'
|
CONF_ENABLE_TIME = 'enable_time'
|
||||||
|
CONF_ENERGY = 'energy'
|
||||||
CONF_ENTITY_ID = 'entity_id'
|
CONF_ENTITY_ID = 'entity_id'
|
||||||
CONF_ESP8266_RESTORE_FROM_FLASH = 'esp8266_restore_from_flash'
|
CONF_ESP8266_RESTORE_FROM_FLASH = 'esp8266_restore_from_flash'
|
||||||
CONF_ESPHOME = 'esphome'
|
CONF_ESPHOME = 'esphome'
|
||||||
@ -580,6 +581,7 @@ UNIT_VOLT = 'V'
|
|||||||
UNIT_VOLT_AMPS = 'VA'
|
UNIT_VOLT_AMPS = 'VA'
|
||||||
UNIT_VOLT_AMPS_REACTIVE = 'VAR'
|
UNIT_VOLT_AMPS_REACTIVE = 'VAR'
|
||||||
UNIT_WATT = 'W'
|
UNIT_WATT = 'W'
|
||||||
|
UNIT_WATT_HOURS = 'Wh'
|
||||||
|
|
||||||
DEVICE_CLASS_CONNECTIVITY = 'connectivity'
|
DEVICE_CLASS_CONNECTIVITY = 'connectivity'
|
||||||
DEVICE_CLASS_MOVING = 'moving'
|
DEVICE_CLASS_MOVING = 'moving'
|
||||||
|
@ -335,6 +335,8 @@ sensor:
|
|||||||
name: "PZEMAC Current"
|
name: "PZEMAC Current"
|
||||||
power:
|
power:
|
||||||
name: "PZEMAC Power"
|
name: "PZEMAC Power"
|
||||||
|
energy:
|
||||||
|
name: "PZEMAC Energy"
|
||||||
frequency:
|
frequency:
|
||||||
name: "PZEMAC Frequency"
|
name: "PZEMAC Frequency"
|
||||||
power_factor:
|
power_factor:
|
||||||
|
Loading…
Reference in New Issue
Block a user