ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
pzem6l24.h
Go to the documentation of this file.
1#pragma once
2
6
7#include <array>
8#include <span>
9
10namespace esphome::pzem6l24 {
11
12// Options for the reset_energy action
13enum ResetPhase : uint8_t {
14 RESET_PHASE_A = 0x00, // Reset phase A energy only
15 RESET_PHASE_B = 0x01, // Reset phase B energy only
16 RESET_PHASE_C = 0x02, // Reset phase C energy only
17 RESET_PHASE_COMBINED = 0x03, // Reset combined (sum) energy only
18 RESET_PHASE_ALL = 0x0F, // Reset all energy counters
19};
20
21// Reset energy function code (PZEM-6L24 specific, non-standard Modbus)
22static constexpr uint8_t PZEM_CMD_RESET_ENERGY = 0x42;
23
24// Input registers read per poll (0x0000 - 0x003F inclusive) and the payload size that yields.
25static constexpr uint8_t PZEM_REGISTER_COUNT = 64;
26static constexpr size_t PZEM_PAYLOAD_SIZE = PZEM_REGISTER_COUNT * 2;
27
28// Failed polls tolerated before the readings are blanked; one collision on a shared RS-485 bus should
29// not take every entity unavailable for a whole update interval.
30static constexpr uint8_t MAX_CONSECUTIVE_READ_FAILURES = 3;
31
32// The energy reset command as it goes on the wire: function code, reserved byte, phase selector. The
33// hub adds the address and CRC. Split out so the phase byte can be pinned by a test.
34constexpr std::array<uint8_t, 3> build_reset_pdu(ResetPhase phase) {
35 return {PZEM_CMD_RESET_ENERGY, 0x00, static_cast<uint8_t>(phase)};
36}
37
39 protected:
40 // Ahead of the sensor pointers so the two bytes share the base class's tail padding.
41 // Register reads accepted by the hub but not yet resolved by a terminal callback.
43 // Failed polls since the last good one; stops counting at MAX_CONSECUTIVE_READ_FAILURES.
45
46 public:
47 SUB_SENSOR(voltage_a)
48 SUB_SENSOR(voltage_b)
49 SUB_SENSOR(voltage_c)
50 SUB_SENSOR(current_a)
51 SUB_SENSOR(current_b)
52 SUB_SENSOR(current_c)
53 SUB_SENSOR(active_power_a)
54 SUB_SENSOR(active_power_b)
55 SUB_SENSOR(active_power_c)
56 SUB_SENSOR(reactive_power_a)
57 SUB_SENSOR(reactive_power_b)
58 SUB_SENSOR(reactive_power_c)
59 SUB_SENSOR(apparent_power_a)
60 SUB_SENSOR(apparent_power_b)
61 SUB_SENSOR(apparent_power_c)
62 SUB_SENSOR(power_factor_a)
63 SUB_SENSOR(power_factor_b)
64 SUB_SENSOR(power_factor_c)
65 SUB_SENSOR(active_energy_a)
66 SUB_SENSOR(active_energy_b)
67 SUB_SENSOR(active_energy_c)
68 SUB_SENSOR(reactive_energy_a)
69 SUB_SENSOR(reactive_energy_b)
70 SUB_SENSOR(reactive_energy_c)
71 SUB_SENSOR(apparent_energy_a)
72 SUB_SENSOR(apparent_energy_b)
73 SUB_SENSOR(apparent_energy_c)
74 SUB_SENSOR(frequency)
75 SUB_SENSOR(total_active_power)
76 SUB_SENSOR(total_reactive_power)
77 SUB_SENSOR(total_apparent_power)
78 SUB_SENSOR(total_power_factor)
79 SUB_SENSOR(total_active_energy)
80 SUB_SENSOR(total_reactive_energy)
81 SUB_SENSOR(total_apparent_energy)
82
83 public:
84 // Queues the energy reset command for the selected phase(s); the pzem6l24.reset_energy action calls this.
85 void reset_energy(ResetPhase phase_option);
86
87 void update() override;
88
89 void on_response(std::span<const uint8_t> request_pdu, std::span<const uint8_t> response_pdu) override;
90
91 void on_error(std::span<const uint8_t> request_pdu, modbus::ExceptionCode exception_code) override;
92
93 bool on_no_response(std::span<const uint8_t> request_pdu) override;
94
95 void on_not_sent(std::span<const uint8_t> request_pdu) override;
96
97 void dump_config() override;
98
99 protected:
100 void publish_(const uint8_t *data);
101 void request_failed_(std::span<const uint8_t> request_pdu);
102 void read_failed_();
103 void read_finished_();
104};
105
106} // namespace esphome::pzem6l24
uint16_le_t frequency
Definition bl0942.h:6
This class simplifies creating components that periodically check a state.
Definition component.h:514
void publish_(const uint8_t *data)
Definition pzem6l24.cpp:164
bool on_no_response(std::span< const uint8_t > request_pdu) override
Definition pzem6l24.cpp:144
void on_response(std::span< const uint8_t > request_pdu, std::span< const uint8_t > response_pdu) override
Definition pzem6l24.cpp:124
void dump_config() override
Definition pzem6l24.cpp:265
void on_error(std::span< const uint8_t > request_pdu, modbus::ExceptionCode exception_code) override
Definition pzem6l24.cpp:140
SUB_SENSOR(voltage_a) SUB_SENSOR(voltage_b) SUB_SENSOR(voltage_c) SUB_SENSOR(current_a) SUB_SENSOR(current_b) SUB_SENSOR(current_c) SUB_SENSOR(active_power_a) SUB_SENSOR(active_power_b) SUB_SENSOR(active_power_c) SUB_SENSOR(reactive_power_a) SUB_SENSOR(reactive_power_b) SUB_SENSOR(reactive_power_c) SUB_SENSOR(apparent_power_a) SUB_SENSOR(apparent_power_b) SUB_SENSOR(apparent_power_c) SUB_SENSOR(power_factor_a) SUB_SENSOR(power_factor_b) SUB_SENSOR(power_factor_c) SUB_SENSOR(active_energy_a) SUB_SENSOR(active_energy_b) SUB_SENSOR(active_energy_c) SUB_SENSOR(reactive_energy_a) SUB_SENSOR(reactive_energy_b) SUB_SENSOR(reactive_energy_c) SUB_SENSOR(apparent_energy_a) SUB_SENSOR(apparent_energy_b) SUB_SENSOR(apparent_energy_c) SUB_SENSOR(frequency) SUB_SENSOR(total_active_power) SUB_SENSOR(total_reactive_power) SUB_SENSOR(total_apparent_power) SUB_SENSOR(total_power_factor) SUB_SENSOR(total_active_energy) SUB_SENSOR(total_reactive_energy) SUB_SENSOR(total_apparent_energy) public void update() override
Definition pzem6l24.cpp:255
void on_not_sent(std::span< const uint8_t > request_pdu) override
Definition pzem6l24.cpp:149
void request_failed_(std::span< const uint8_t > request_pdu)
Definition pzem6l24.cpp:153
constexpr std::array< uint8_t, 3 > build_reset_pdu(ResetPhase phase)
Definition pzem6l24.h:34