ESPHome 2026.10.0-dev
Loading...
Searching...
No Matches
selec_meter.cpp
Go to the documentation of this file.
1#include "selec_meter.h"
3#include "esphome/core/log.h"
4
6
7namespace helpers = modbus::helpers;
8
9static const char *const TAG = "selec_meter";
10
11static const uint8_t MODBUS_REGISTER_COUNT = 34; // 34 x 16-bit registers
12
13void SelecMeter::on_read_input_registers(uint16_t start_address, std::span<const uint16_t> registers,
16 return; // the hub already logs exception responses
17
18 // Publish a sensor if both of its registers are in this response; skipping absent registers keeps
19 // this correct for any read range, so the poll may be split into multiple requests.
20 // Values are 32-bit floats, low word first.
21 auto publish = [&](sensor::Sensor *sensor, uint16_t reg, float unit) -> void {
22 if (sensor == nullptr)
23 return;
25 sensor->publish_state(*value * unit);
26 };
27
28 publish(this->total_active_energy_sensor_, SELEC_TOTAL_ACTIVE_ENERGY, NO_DEC_UNIT);
29 publish(this->import_active_energy_sensor_, SELEC_IMPORT_ACTIVE_ENERGY, NO_DEC_UNIT);
30 publish(this->export_active_energy_sensor_, SELEC_EXPORT_ACTIVE_ENERGY, NO_DEC_UNIT);
31 publish(this->total_reactive_energy_sensor_, SELEC_TOTAL_REACTIVE_ENERGY, NO_DEC_UNIT);
32 publish(this->import_reactive_energy_sensor_, SELEC_IMPORT_REACTIVE_ENERGY, NO_DEC_UNIT);
33 publish(this->export_reactive_energy_sensor_, SELEC_EXPORT_REACTIVE_ENERGY, NO_DEC_UNIT);
34 publish(this->apparent_energy_sensor_, SELEC_APPARENT_ENERGY, NO_DEC_UNIT);
35 publish(this->active_power_sensor_, SELEC_ACTIVE_POWER, MULTIPLY_THOUSAND_UNIT);
36 publish(this->reactive_power_sensor_, SELEC_REACTIVE_POWER, MULTIPLY_THOUSAND_UNIT);
37 publish(this->apparent_power_sensor_, SELEC_APPARENT_POWER, MULTIPLY_THOUSAND_UNIT);
38 publish(this->voltage_sensor_, SELEC_VOLTAGE, NO_DEC_UNIT);
39 publish(this->current_sensor_, SELEC_CURRENT, NO_DEC_UNIT);
40 publish(this->power_factor_sensor_, SELEC_POWER_FACTOR, NO_DEC_UNIT);
41 publish(this->frequency_sensor_, SELEC_FREQUENCY, NO_DEC_UNIT);
42 publish(this->maximum_demand_active_power_sensor_, SELEC_MAXIMUM_DEMAND_ACTIVE_POWER, MULTIPLY_THOUSAND_UNIT);
43 publish(this->maximum_demand_reactive_power_sensor_, SELEC_MAXIMUM_DEMAND_REACTIVE_POWER, MULTIPLY_THOUSAND_UNIT);
44 publish(this->maximum_demand_apparent_power_sensor_, SELEC_MAXIMUM_DEMAND_APPARENT_POWER, MULTIPLY_THOUSAND_UNIT);
45}
46
47void SelecMeter::update() { this->read_input_registers(0, MODBUS_REGISTER_COUNT); }
49 ESP_LOGCONFIG(TAG,
50 "SELEC Meter:\n"
51 " Address: 0x%02X",
52 this->address_);
53 LOG_SENSOR(" ", "Total Active Energy", this->total_active_energy_sensor_);
54 LOG_SENSOR(" ", "Import Active Energy", this->import_active_energy_sensor_);
55 LOG_SENSOR(" ", "Export Active Energy", this->export_active_energy_sensor_);
56 LOG_SENSOR(" ", "Total Reactive Energy", this->total_reactive_energy_sensor_);
57 LOG_SENSOR(" ", "Import Reactive Energy", this->import_reactive_energy_sensor_);
58 LOG_SENSOR(" ", "Export Reactive Energy", this->export_reactive_energy_sensor_);
59 LOG_SENSOR(" ", "Apparent Energy", this->apparent_energy_sensor_);
60 LOG_SENSOR(" ", "Active Power", this->active_power_sensor_);
61 LOG_SENSOR(" ", "Reactive Power", this->reactive_power_sensor_);
62 LOG_SENSOR(" ", "Apparent Power", this->apparent_power_sensor_);
63 LOG_SENSOR(" ", "Voltage", this->voltage_sensor_);
64 LOG_SENSOR(" ", "Current", this->current_sensor_);
65 LOG_SENSOR(" ", "Power Factor", this->power_factor_sensor_);
66 LOG_SENSOR(" ", "Frequency", this->frequency_sensor_);
67 LOG_SENSOR(" ", "Maximum Demand Active Power", this->maximum_demand_active_power_sensor_);
68 LOG_SENSOR(" ", "Maximum Demand Reactive Power", this->maximum_demand_reactive_power_sensor_);
69 LOG_SENSOR(" ", "Maximum Demand Apparent Power", this->maximum_demand_apparent_power_sensor_);
70}
71
72} // namespace esphome::selec_meter
uint8_t status
Definition bl0942.h:8
virtual void update()=0
void on_read_input_registers(uint16_t start_address, std::span< const uint16_t > registers, modbus::ResponseStatus status) override
Base-class for all sensors.
Definition sensor.h:47
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
constexpr std::optional< RegisterValueType< VALUE_TYPE > > value_at(std::span< const uint16_t > registers, uint16_t start_address, uint16_t address)
The value stored at an absolute register address, or nullopt when it is not wholly inside this respon...
std::optional< ExceptionCode > ResponseStatus
Definition modbus.h:306
bool succeeded(ResponseStatus status)
True when a transaction carried no exception.
Definition modbus.h:309