ESPHome 2025.6.3
Loading...
Searching...
No Matches
havells_solar.cpp
Go to the documentation of this file.
1#include "havells_solar.h"
4#include "esphome/core/log.h"
5
6namespace esphome {
7namespace havells_solar {
8
9static const char *const TAG = "havells_solar";
10
11static const uint8_t MODBUS_CMD_READ_IN_REGISTERS = 0x03;
12static const uint8_t MODBUS_REGISTER_COUNT = 48; // 48 x 16-bit registers
13
14void HavellsSolar::on_modbus_data(const std::vector<uint8_t> &data) {
15 if (data.size() < MODBUS_REGISTER_COUNT * 2) {
16 ESP_LOGW(TAG, "Invalid size for HavellsSolar!");
17 return;
18 }
19
20 /* Usage: returns the float value of 1 register read by modbus
21 Arg1: Register address * number of bytes per register
22 Arg2: Multiplier for final register value
23 */
24 auto havells_solar_get_2_registers = [&](size_t i, float unit) -> float {
25 uint32_t temp = encode_uint32(data[i], data[i + 1], data[i + 2], data[i + 3]);
26 return temp * unit;
27 };
28
29 /* Usage: returns the float value of 2 registers read by modbus
30 Arg1: Register address * number of bytes per register
31 Arg2: Multiplier for final register value
32 */
33 auto havells_solar_get_1_register = [&](size_t i, float unit) -> float {
34 uint16_t temp = encode_uint16(data[i], data[i + 1]);
35 return temp * unit;
36 };
37
38 for (uint8_t i = 0; i < 3; i++) {
39 auto phase = this->phases_[i];
40 if (!phase.setup)
41 continue;
42
43 float voltage = havells_solar_get_1_register(HAVELLS_PHASE_1_VOLTAGE * 2 + (i * 4), ONE_DEC_UNIT);
44 float current = havells_solar_get_1_register(HAVELLS_PHASE_1_CURRENT * 2 + (i * 4), TWO_DEC_UNIT);
45
46 if (phase.voltage_sensor_ != nullptr)
47 phase.voltage_sensor_->publish_state(voltage);
48 if (phase.current_sensor_ != nullptr)
49 phase.current_sensor_->publish_state(current);
50 }
51
52 for (uint8_t i = 0; i < 2; i++) {
53 auto pv = this->pvs_[i];
54 if (!pv.setup)
55 continue;
56
57 float voltage = havells_solar_get_1_register(HAVELLS_PV_1_VOLTAGE * 2 + (i * 4), ONE_DEC_UNIT);
58 float current = havells_solar_get_1_register(HAVELLS_PV_1_CURRENT * 2 + (i * 4), TWO_DEC_UNIT);
59 float active_power = havells_solar_get_1_register(HAVELLS_PV_1_POWER * 2 + (i * 2), MULTIPLY_TEN_UNIT);
60 float voltage_sampled_by_secondary_cpu =
61 havells_solar_get_1_register(HAVELLS_PV1_VOLTAGE_SAMPLED_BY_SECONDARY_CPU * 2 + (i * 2), ONE_DEC_UNIT);
62 float insulation_of_p_to_ground =
63 havells_solar_get_1_register(HAVELLS_PV1_INSULATION_OF_P_TO_GROUND * 2 + (i * 2), NO_DEC_UNIT);
64
65 if (pv.voltage_sensor_ != nullptr)
66 pv.voltage_sensor_->publish_state(voltage);
67 if (pv.current_sensor_ != nullptr)
68 pv.current_sensor_->publish_state(current);
69 if (pv.active_power_sensor_ != nullptr)
70 pv.active_power_sensor_->publish_state(active_power);
71 if (pv.voltage_sampled_by_secondary_cpu_sensor_ != nullptr)
72 pv.voltage_sampled_by_secondary_cpu_sensor_->publish_state(voltage_sampled_by_secondary_cpu);
73 if (pv.insulation_of_p_to_ground_sensor_ != nullptr)
74 pv.insulation_of_p_to_ground_sensor_->publish_state(insulation_of_p_to_ground);
75 }
76
77 float frequency = havells_solar_get_1_register(HAVELLS_GRID_FREQUENCY * 2, TWO_DEC_UNIT);
78 float active_power = havells_solar_get_1_register(HAVELLS_SYSTEM_ACTIVE_POWER * 2, MULTIPLY_TEN_UNIT);
79 float reactive_power = havells_solar_get_1_register(HAVELLS_SYSTEM_REACTIVE_POWER * 2, TWO_DEC_UNIT);
80 float today_production = havells_solar_get_1_register(HAVELLS_TODAY_PRODUCTION * 2, TWO_DEC_UNIT);
81 float total_energy_production = havells_solar_get_2_registers(HAVELLS_TOTAL_ENERGY_PRODUCTION * 2, NO_DEC_UNIT);
82 float total_generation_time = havells_solar_get_2_registers(HAVELLS_TOTAL_GENERATION_TIME * 2, NO_DEC_UNIT);
83 float today_generation_time = havells_solar_get_1_register(HAVELLS_TODAY_GENERATION_TIME * 2, NO_DEC_UNIT);
84 float inverter_module_temp = havells_solar_get_1_register(HAVELLS_INVERTER_MODULE_TEMP * 2, NO_DEC_UNIT);
85 float inverter_inner_temp = havells_solar_get_1_register(HAVELLS_INVERTER_INNER_TEMP * 2, NO_DEC_UNIT);
86 float inverter_bus_voltage = havells_solar_get_1_register(HAVELLS_INVERTER_BUS_VOLTAGE * 2, NO_DEC_UNIT);
87 float insulation_pv_n_to_ground = havells_solar_get_1_register(HAVELLS_INSULATION_OF_PV_N_TO_GROUND * 2, NO_DEC_UNIT);
88 float gfci_value = havells_solar_get_1_register(HAVELLS_GFCI_VALUE * 2, NO_DEC_UNIT);
89 float dci_of_r = havells_solar_get_1_register(HAVELLS_DCI_OF_R * 2, NO_DEC_UNIT);
90 float dci_of_s = havells_solar_get_1_register(HAVELLS_DCI_OF_S * 2, NO_DEC_UNIT);
91 float dci_of_t = havells_solar_get_1_register(HAVELLS_DCI_OF_T * 2, NO_DEC_UNIT);
92
93 if (this->frequency_sensor_ != nullptr)
94 this->frequency_sensor_->publish_state(frequency);
95 if (this->active_power_sensor_ != nullptr)
96 this->active_power_sensor_->publish_state(active_power);
97 if (this->reactive_power_sensor_ != nullptr)
98 this->reactive_power_sensor_->publish_state(reactive_power);
99 if (this->today_production_sensor_ != nullptr)
100 this->today_production_sensor_->publish_state(today_production);
101 if (this->total_energy_production_sensor_ != nullptr)
102 this->total_energy_production_sensor_->publish_state(total_energy_production);
103 if (this->total_generation_time_sensor_ != nullptr)
104 this->total_generation_time_sensor_->publish_state(total_generation_time);
105 if (this->today_generation_time_sensor_ != nullptr)
106 this->today_generation_time_sensor_->publish_state(today_generation_time);
107 if (this->inverter_module_temp_sensor_ != nullptr)
108 this->inverter_module_temp_sensor_->publish_state(inverter_module_temp);
109 if (this->inverter_inner_temp_sensor_ != nullptr)
110 this->inverter_inner_temp_sensor_->publish_state(inverter_inner_temp);
111 if (this->inverter_bus_voltage_sensor_ != nullptr)
112 this->inverter_bus_voltage_sensor_->publish_state(inverter_bus_voltage);
113 if (this->insulation_pv_n_to_ground_sensor_ != nullptr)
114 this->insulation_pv_n_to_ground_sensor_->publish_state(insulation_pv_n_to_ground);
115 if (this->gfci_value_sensor_ != nullptr)
116 this->gfci_value_sensor_->publish_state(gfci_value);
117 if (this->dci_of_r_sensor_ != nullptr)
118 this->dci_of_r_sensor_->publish_state(dci_of_r);
119 if (this->dci_of_s_sensor_ != nullptr)
120 this->dci_of_s_sensor_->publish_state(dci_of_s);
121 if (this->dci_of_t_sensor_ != nullptr)
122 this->dci_of_t_sensor_->publish_state(dci_of_t);
123}
124
125void HavellsSolar::update() { this->send(MODBUS_CMD_READ_IN_REGISTERS, 0, MODBUS_REGISTER_COUNT); }
127 ESP_LOGCONFIG(TAG,
128 "HAVELLS Solar:\n"
129 " Address: 0x%02X",
130 this->address_);
131 for (uint8_t i = 0; i < 3; i++) {
132 auto phase = this->phases_[i];
133 if (!phase.setup)
134 continue;
135 ESP_LOGCONFIG(TAG, " Phase %c", i + 'A');
136 LOG_SENSOR(" ", "Voltage", phase.voltage_sensor_);
137 LOG_SENSOR(" ", "Current", phase.current_sensor_);
138 }
139 for (uint8_t i = 0; i < 2; i++) {
140 auto pv = this->pvs_[i];
141 if (!pv.setup)
142 continue;
143 ESP_LOGCONFIG(TAG, " PV %d", i + 1);
144 LOG_SENSOR(" ", "Voltage", pv.voltage_sensor_);
145 LOG_SENSOR(" ", "Current", pv.current_sensor_);
146 LOG_SENSOR(" ", "Active Power", pv.active_power_sensor_);
147 LOG_SENSOR(" ", "Voltage Sampled By Secondary CPU", pv.voltage_sampled_by_secondary_cpu_sensor_);
148 LOG_SENSOR(" ", "Insulation Of PV+ To Ground", pv.insulation_of_p_to_ground_sensor_);
149 }
150 LOG_SENSOR(" ", "Frequency", this->frequency_sensor_);
151 LOG_SENSOR(" ", "Active Power", this->active_power_sensor_);
152 LOG_SENSOR(" ", "Reactive Power", this->reactive_power_sensor_);
153 LOG_SENSOR(" ", "Today Generation", this->today_production_sensor_);
154 LOG_SENSOR(" ", "Total Generation", this->total_energy_production_sensor_);
155 LOG_SENSOR(" ", "Total Generation Time", this->total_generation_time_sensor_);
156 LOG_SENSOR(" ", "Today Generation Time", this->today_generation_time_sensor_);
157 LOG_SENSOR(" ", "Inverter Module Temp", this->inverter_module_temp_sensor_);
158 LOG_SENSOR(" ", "Inverter Inner Temp", this->inverter_inner_temp_sensor_);
159 LOG_SENSOR(" ", "Inverter Bus Voltage", this->inverter_bus_voltage_sensor_);
160 LOG_SENSOR(" ", "Insulation Of PV- To Ground", this->insulation_pv_n_to_ground_sensor_);
161 LOG_SENSOR(" ", "GFCI Value", this->gfci_value_sensor_);
162 LOG_SENSOR(" ", "DCI Of R", this->dci_of_r_sensor_);
163 LOG_SENSOR(" ", "DCI Of S", this->dci_of_s_sensor_);
164 LOG_SENSOR(" ", "DCI Of T", this->dci_of_t_sensor_);
165}
166
167} // namespace havells_solar
168} // namespace esphome
uint16_le_t frequency
Definition bl0942.h:6
void on_modbus_data(const std::vector< uint8_t > &data) override
sensor::Sensor * insulation_pv_n_to_ground_sensor_
sensor::Sensor * total_energy_production_sensor_
struct esphome::havells_solar::HavellsSolar::HAVELLSPV pvs_[2]
struct esphome::havells_solar::HavellsSolar::HAVELLSPhase phases_[3]
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 publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:39
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
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
Definition helpers.h:192