6 namespace havells_solar {
8 static const char *
const TAG =
"havells_solar";
10 static const uint8_t MODBUS_CMD_READ_IN_REGISTERS = 0x03;
11 static const uint8_t MODBUS_REGISTER_COUNT = 48;
14 if (data.size() < MODBUS_REGISTER_COUNT * 2) {
15 ESP_LOGW(TAG,
"Invalid size for HavellsSolar!");
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]);
32 auto havells_solar_get_1_register = [&](
size_t i,
float unit) ->
float {
37 for (uint8_t i = 0; i < 3; i++) {
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);
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);
51 for (uint8_t i = 0; i < 2; i++) {
52 auto pv = this->
pvs_[i];
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);
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);
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);
126 ESP_LOGCONFIG(TAG,
"HAVELLS Solar:");
127 ESP_LOGCONFIG(TAG,
" Address: 0x%02X", this->
address_);
128 for (uint8_t i = 0; i < 3; i++) {
132 ESP_LOGCONFIG(TAG,
" Phase %c", i +
'A');
133 LOG_SENSOR(
" ",
"Voltage", phase.voltage_sensor_);
134 LOG_SENSOR(
" ",
"Current", phase.current_sensor_);
136 for (uint8_t i = 0; i < 2; i++) {
137 auto pv = this->
pvs_[i];
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_);
sensor::Sensor * today_production_sensor_
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.
sensor::Sensor * inverter_module_temp_sensor_
sensor::Sensor * active_power_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
sensor::Sensor * frequency_sensor_
void publish_state(float state)
Publish a new state to the front-end.
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.
sensor::Sensor * dci_of_s_sensor_
sensor::Sensor * total_energy_production_sensor_
sensor::Sensor * gfci_value_sensor_
sensor::Sensor * reactive_power_sensor_
void dump_config() override
Implementation of SPI Controller mode.
void send(uint8_t function, uint16_t start_address, uint16_t number_of_entities, uint8_t payload_len=0, const uint8_t *payload=nullptr)
sensor::Sensor * insulation_pv_n_to_ground_sensor_
sensor::Sensor * dci_of_t_sensor_
sensor::Sensor * dci_of_r_sensor_
sensor::Sensor * inverter_inner_temp_sensor_