ESPHome 2025.6.3
Loading...
Searching...
No Matches
selec_meter.cpp
Go to the documentation of this file.
1#include "selec_meter.h"
4#include "esphome/core/log.h"
5
6namespace esphome {
7namespace selec_meter {
8
9static const char *const TAG = "selec_meter";
10
11static const uint8_t MODBUS_CMD_READ_IN_REGISTERS = 0x04;
12static const uint8_t MODBUS_REGISTER_COUNT = 34; // 34 x 16-bit registers
13
14void SelecMeter::on_modbus_data(const std::vector<uint8_t> &data) {
15 if (data.size() < MODBUS_REGISTER_COUNT * 2) {
16 ESP_LOGW(TAG, "Invalid size for SelecMeter!");
17 return;
18 }
19
20 auto selec_meter_get_float = [&](size_t i, float unit) -> float {
21 uint32_t temp = encode_uint32(data[i + 2], data[i + 3], data[i], data[i + 1]);
22
23 float f;
24 memcpy(&f, &temp, sizeof(f));
25 return (f * unit);
26 };
27
28 float total_active_energy = selec_meter_get_float(SELEC_TOTAL_ACTIVE_ENERGY * 2, NO_DEC_UNIT);
29 float import_active_energy = selec_meter_get_float(SELEC_IMPORT_ACTIVE_ENERGY * 2, NO_DEC_UNIT);
30 float export_active_energy = selec_meter_get_float(SELEC_EXPORT_ACTIVE_ENERGY * 2, NO_DEC_UNIT);
31 float total_reactive_energy = selec_meter_get_float(SELEC_TOTAL_REACTIVE_ENERGY * 2, NO_DEC_UNIT);
32 float import_reactive_energy = selec_meter_get_float(SELEC_IMPORT_REACTIVE_ENERGY * 2, NO_DEC_UNIT);
33 float export_reactive_energy = selec_meter_get_float(SELEC_EXPORT_REACTIVE_ENERGY * 2, NO_DEC_UNIT);
34 float apparent_energy = selec_meter_get_float(SELEC_APPARENT_ENERGY * 2, NO_DEC_UNIT);
35 float active_power = selec_meter_get_float(SELEC_ACTIVE_POWER * 2, MULTIPLY_THOUSAND_UNIT);
36 float reactive_power = selec_meter_get_float(SELEC_REACTIVE_POWER * 2, MULTIPLY_THOUSAND_UNIT);
37 float apparent_power = selec_meter_get_float(SELEC_APPARENT_POWER * 2, MULTIPLY_THOUSAND_UNIT);
38 float voltage = selec_meter_get_float(SELEC_VOLTAGE * 2, NO_DEC_UNIT);
39 float current = selec_meter_get_float(SELEC_CURRENT * 2, NO_DEC_UNIT);
40 float power_factor = selec_meter_get_float(SELEC_POWER_FACTOR * 2, NO_DEC_UNIT);
41 float frequency = selec_meter_get_float(SELEC_FREQUENCY * 2, NO_DEC_UNIT);
42 float maximum_demand_active_power =
43 selec_meter_get_float(SELEC_MAXIMUM_DEMAND_ACTIVE_POWER * 2, MULTIPLY_THOUSAND_UNIT);
44 float maximum_demand_reactive_power =
45 selec_meter_get_float(SELEC_MAXIMUM_DEMAND_REACTIVE_POWER * 2, MULTIPLY_THOUSAND_UNIT);
46 float maximum_demand_apparent_power =
47 selec_meter_get_float(SELEC_MAXIMUM_DEMAND_APPARENT_POWER * 2, MULTIPLY_THOUSAND_UNIT);
48
49 if (this->total_active_energy_sensor_ != nullptr)
50 this->total_active_energy_sensor_->publish_state(total_active_energy);
51 if (this->import_active_energy_sensor_ != nullptr)
52 this->import_active_energy_sensor_->publish_state(import_active_energy);
53 if (this->export_active_energy_sensor_ != nullptr)
54 this->export_active_energy_sensor_->publish_state(export_active_energy);
55 if (this->total_reactive_energy_sensor_ != nullptr)
56 this->total_reactive_energy_sensor_->publish_state(total_reactive_energy);
57 if (this->import_reactive_energy_sensor_ != nullptr)
58 this->import_reactive_energy_sensor_->publish_state(import_reactive_energy);
59 if (this->export_reactive_energy_sensor_ != nullptr)
60 this->export_reactive_energy_sensor_->publish_state(export_reactive_energy);
61 if (this->apparent_energy_sensor_ != nullptr)
62 this->apparent_energy_sensor_->publish_state(apparent_energy);
63 if (this->active_power_sensor_ != nullptr)
64 this->active_power_sensor_->publish_state(active_power);
65 if (this->reactive_power_sensor_ != nullptr)
66 this->reactive_power_sensor_->publish_state(reactive_power);
67 if (this->apparent_power_sensor_ != nullptr)
68 this->apparent_power_sensor_->publish_state(apparent_power);
69 if (this->voltage_sensor_ != nullptr)
70 this->voltage_sensor_->publish_state(voltage);
71 if (this->current_sensor_ != nullptr)
72 this->current_sensor_->publish_state(current);
73 if (this->power_factor_sensor_ != nullptr)
74 this->power_factor_sensor_->publish_state(power_factor);
75 if (this->frequency_sensor_ != nullptr)
76 this->frequency_sensor_->publish_state(frequency);
77 if (this->maximum_demand_active_power_sensor_ != nullptr)
78 this->maximum_demand_active_power_sensor_->publish_state(maximum_demand_active_power);
79 if (this->maximum_demand_reactive_power_sensor_ != nullptr)
80 this->maximum_demand_reactive_power_sensor_->publish_state(maximum_demand_reactive_power);
81 if (this->maximum_demand_apparent_power_sensor_ != nullptr)
82 this->maximum_demand_apparent_power_sensor_->publish_state(maximum_demand_apparent_power);
83}
84
85void SelecMeter::update() { this->send(MODBUS_CMD_READ_IN_REGISTERS, 0, MODBUS_REGISTER_COUNT); }
87 ESP_LOGCONFIG(TAG,
88 "SELEC Meter:\n"
89 " Address: 0x%02X",
90 this->address_);
91 LOG_SENSOR(" ", "Total Active Energy", this->total_active_energy_sensor_);
92 LOG_SENSOR(" ", "Import Active Energy", this->import_active_energy_sensor_);
93 LOG_SENSOR(" ", "Export Active Energy", this->export_active_energy_sensor_);
94 LOG_SENSOR(" ", "Total Reactive Energy", this->total_reactive_energy_sensor_);
95 LOG_SENSOR(" ", "Import Reactive Energy", this->import_reactive_energy_sensor_);
96 LOG_SENSOR(" ", "Export Reactive Energy", this->export_reactive_energy_sensor_);
97 LOG_SENSOR(" ", "Apparent Energy", this->apparent_energy_sensor_);
98 LOG_SENSOR(" ", "Active Power", this->active_power_sensor_);
99 LOG_SENSOR(" ", "Reactive Power", this->reactive_power_sensor_);
100 LOG_SENSOR(" ", "Apparent Power", this->apparent_power_sensor_);
101 LOG_SENSOR(" ", "Voltage", this->voltage_sensor_);
102 LOG_SENSOR(" ", "Current", this->current_sensor_);
103 LOG_SENSOR(" ", "Power Factor", this->power_factor_sensor_);
104 LOG_SENSOR(" ", "Frequency", this->frequency_sensor_);
105 LOG_SENSOR(" ", "Maximum Demand Active Power", this->maximum_demand_active_power_sensor_);
106 LOG_SENSOR(" ", "Maximum Demand Reactive Power", this->maximum_demand_reactive_power_sensor_);
107 LOG_SENSOR(" ", "Maximum Demand Apparent Power", this->maximum_demand_apparent_power_sensor_);
108}
109
110} // namespace selec_meter
111} // namespace esphome
uint16_le_t frequency
Definition bl0942.h:6
virtual void update()=0
void send(uint8_t function, uint16_t start_address, uint16_t number_of_entities, uint8_t payload_len=0, const uint8_t *payload=nullptr)
Definition modbus.h:62
void on_modbus_data(const std::vector< uint8_t > &data) override
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
constexpr uint32_t encode_uint32(uint8_t byte1, uint8_t byte2, uint8_t byte3, uint8_t byte4)
Encode a 32-bit value given four bytes in most to least significant byte order.
Definition helpers.h:200