ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
selec_meter.h
Go to the documentation of this file.
1#pragma once
2
4#include "esphome/core/log.h"
7#ifdef USE_BINARY_SENSOR
9#endif
10#ifdef USE_TEXT_SENSOR
12#endif
13
14#include <span>
15
16namespace esphome::selec_meter {
17
18#define SELEC_METER_SENSOR(name) \
19 protected: \
20 sensor::Sensor *name##_sensor_{nullptr}; \
21\
22 public: \
23 void set_##name##_sensor(sensor::Sensor *(name)) { this->name##_sensor_ = name; }
24
25// Per-phase sensor in the Phase struct, setter indexed by phase (0 = L1/A, 1 = L2/B, 2 = L3/C)
26#define SELEC_METER_PHASE_SENSOR(name) \
27 void set_##name##_sensor(uint8_t phase, sensor::Sensor *(name)) { this->phases_[phase].name##_sensor_ = name; }
28
29enum class Model : uint8_t {
32};
33
34// Read cycle steps in the order they run. The extra reads (serial number, DG sensing) sit far outside
35// the main block and get their own transaction.
42
44#if defined(USE_SELEC_METER_EM4M) && defined(USE_TEXT_SENSOR)
45 SUB_TEXT_SENSOR(serial_number)
46#endif
47#if defined(USE_SELEC_METER_EM4M) && defined(USE_BINARY_SENSOR)
48 SUB_BINARY_SENSOR(dg_sensing)
49#endif
50
51 public:
52 SELEC_METER_SENSOR(total_active_energy)
53 SELEC_METER_SENSOR(import_active_energy)
54 SELEC_METER_SENSOR(export_active_energy)
55 SELEC_METER_SENSOR(total_reactive_energy)
56 SELEC_METER_SENSOR(import_reactive_energy)
57 SELEC_METER_SENSOR(export_reactive_energy)
58 SELEC_METER_SENSOR(apparent_energy)
59 SELEC_METER_SENSOR(active_power)
60 SELEC_METER_SENSOR(reactive_power)
61 SELEC_METER_SENSOR(apparent_power)
62 SELEC_METER_SENSOR(voltage)
63 SELEC_METER_SENSOR(current)
64 SELEC_METER_SENSOR(power_factor)
65 SELEC_METER_SENSOR(frequency)
66 SELEC_METER_SENSOR(maximum_demand_active_power)
67 SELEC_METER_SENSOR(maximum_demand_reactive_power)
68 SELEC_METER_SENSOR(maximum_demand_apparent_power)
69
70#ifdef USE_SELEC_METER_EM4M
71 // EM4M per-phase sensors, indexed by phase (see Phase below)
72 SELEC_METER_PHASE_SENSOR(voltage)
73 SELEC_METER_PHASE_SENSOR(current)
74 SELEC_METER_PHASE_SENSOR(active_power)
75 SELEC_METER_PHASE_SENSOR(reactive_power)
76 SELEC_METER_PHASE_SENSOR(apparent_power)
77 SELEC_METER_PHASE_SENSOR(power_factor)
78 SELEC_METER_PHASE_SENSOR(import_active_energy)
79 SELEC_METER_PHASE_SENSOR(export_active_energy)
80 SELEC_METER_PHASE_SENSOR(import_reactive_energy)
81 SELEC_METER_PHASE_SENSOR(export_reactive_energy)
82 SELEC_METER_PHASE_SENSOR(apparent_energy)
83
84 // Line-to-line and aggregate sensors, not tied to one phase
85 SELEC_METER_SENSOR(voltage_l12)
86 SELEC_METER_SENSOR(voltage_l23)
87 SELEC_METER_SENSOR(voltage_l31)
88 SELEC_METER_SENSOR(average_voltage_ll)
89 SELEC_METER_SENSOR(net_active_energy_mains)
90 SELEC_METER_SENSOR(net_reactive_energy_mains)
91 SELEC_METER_SENSOR(net_apparent_energy_mains)
92 SELEC_METER_SENSOR(net_active_energy_dg)
93 SELEC_METER_SENSOR(net_reactive_energy_dg)
94 SELEC_METER_SENSOR(net_apparent_energy_dg)
95#endif
96
97 void set_model(Model model) { this->model_ = model; }
98 // true: registers are word-swapped (LSRF / Mid Little Endian, CDAB byte order)
99 // false: registers are in the meter's straightforward order (MSRF / Big Endian, ABCD byte order)
100 void set_word_swap(bool word_swap) { this->word_swap_ = word_swap; }
101
102 void update() override;
103
104 void on_read_input_registers(uint16_t start_address, std::span<const uint16_t> registers,
106 bool on_no_response(std::span<const uint8_t> request_pdu) override;
107 void on_not_sent(std::span<const uint8_t> request_pdu) override;
108
109 void dump_config() override;
110
111 protected:
112 // One float field of the main block: target sensor, register and whether it is published x1000
113 struct FloatField {
115 uint16_t reg;
116 bool kilo;
117 };
118
119 // Publishes each configured field in `fields` (a PROGMEM table); false on a payload shorter than
120 // `min_count`, and the caller sets or clears the warning from the result
121 bool publish_fields_(std::span<const uint16_t> registers, std::span<const FloatField> fields, uint8_t min_count);
122 bool decode_em2m_(std::span<const uint16_t> registers);
123#ifdef USE_SELEC_METER_EM4M
124 bool decode_em4m_(std::span<const uint16_t> registers);
125#ifdef USE_TEXT_SENSOR
126 void decode_serial_number_(std::span<const uint16_t> registers);
127#endif
128#ifdef USE_BINARY_SENSOR
129 void decode_dg_sensing_(std::span<const uint16_t> registers);
130#endif
131#endif
132 // First register and register count of the read for `state`
133 struct ReadRange {
134 uint16_t start;
135 uint8_t count;
136 };
138 // Whether the read cycle should run `state` on this pass
139 bool wanted_(ReadState state) const;
140 // Next wanted step after `current`, so one failed range does not cancel the other reads
142 // Sends the read for `state`; safe from a callback per the modbus callback contract
144 // Clears the pending read and advances the chain. Only a MAIN_BLOCK failure sets the warning and
145 // only a `durable` refusal counts toward MAX_OPTIONAL_READ_FAILURES; `reason` nullptr means already logged
146 void fail_current_read_(const LogString *reason, bool durable = false);
147
148#ifdef USE_SELEC_METER_EM4M
149 // phases_[0..2] are L1/L2/L3 (A/B/C), EM4M_PHASE_STRIDE registers apart
164#endif
165
167 bool word_swap_{false};
169
170 // An optional read stops once its counter reaches MAX_OPTIONAL_READ_FAILURES
171#if defined(USE_SELEC_METER_EM4M) && defined(USE_TEXT_SENSOR)
172 // Also set to the limit once the serial number is published, since it never changes
174#endif
175#if defined(USE_SELEC_METER_EM4M) && defined(USE_BINARY_SENSOR)
177#endif
178};
179
180} // namespace esphome::selec_meter
uint8_t status
Definition bl0942.h:8
uint16_le_t frequency
Definition bl0942.h:6
This class simplifies creating components that periodically check a state.
Definition component.h:532
ReadState next_read_state_after_(ReadState current) const
void start_read_(ReadState state)
bool publish_fields_(std::span< const uint16_t > registers, std::span< const FloatField > fields, uint8_t min_count)
ReadRange read_range_(ReadState state) const
bool decode_em4m_(std::span< const uint16_t > registers)
void decode_serial_number_(std::span< const uint16_t > registers)
void fail_current_read_(const LogString *reason, bool durable=false)
bool wanted_(ReadState state) const
bool decode_em2m_(std::span< const uint16_t > registers)
void decode_dg_sensing_(std::span< const uint16_t > registers)
Base-class for all sensors.
Definition sensor.h:48
bool state
Definition fan.h:2
std::optional< ExceptionCode > ResponseStatus
Definition modbus.h:329
sensor::Sensor *SelecMeter::* sensor
char serial_number[10]
Definition sun_gtil2.cpp:15