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