Skip to content

Commit

Permalink
Add total_energy_production_t4_kwh, total_energy_consumption_t4_kwh
Browse files Browse the repository at this point in the history
  • Loading branch information
augustynski-lukasz authored Jun 20, 2024
1 parent dfa4706 commit a798cc8
Showing 1 changed file with 32 additions and 3 deletions.
35 changes: 32 additions & 3 deletions driver_amiplus.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@ struct Amiplus: Driver
add_to_map(ret_val, "total_energy_consumption_t1_kwh", this->get_total_energy_consumption_t_kwh(1, telegram));
add_to_map(ret_val, "total_energy_consumption_t2_kwh", this->get_total_energy_consumption_t_kwh(2, telegram));
add_to_map(ret_val, "total_energy_consumption_t3_kwh", this->get_total_energy_consumption_t_kwh(3, telegram));
add_to_map(ret_val, "total_energy_consumption_t4_kwh", this->get_total_energy_consumption_t_kwh(4, telegram));
add_to_map(ret_val, "current_power_consumption_kw", this->get_current_power_consumption_kw(telegram));
add_to_map(ret_val, "total_energy_production_kwh", this->get_total_energy_production_kwh(telegram));
add_to_map(ret_val, "total_energy_production_t1_kwh", this->get_total_energy_production_t_kwh(1, telegram));
add_to_map(ret_val, "total_energy_production_t2_kwh", this->get_total_energy_production_t_kwh(2, telegram));
add_to_map(ret_val, "total_energy_production_t3_kwh", this->get_total_energy_production_t_kwh(3, telegram));
add_to_map(ret_val, "total_energy_production_t4_kwh", this->get_total_energy_production_t_kwh(4, telegram));
add_to_map(ret_val, "current_power_production_kw", this->get_current_power_production_kw(telegram));
add_to_map(ret_val, "voltage_at_phase_1_v", this->get_voltage_at_phase_v(1, telegram));
add_to_map(ret_val, "voltage_at_phase_2_v", this->get_voltage_at_phase_v(2, telegram));
Expand Down Expand Up @@ -62,11 +67,34 @@ struct Amiplus: Driver
esphome::optional<double> ret_val{};
uint32_t usage = 0;
size_t i = 11;
uint32_t total_register = 0x8E0003 | ((tarrif*16) << 8);
uint32_t total_register = tarrif < 4 ? 0x8E0003 | ((tarrif*16) << 8) : 0x8E801003;
while (i < telegram.size()) {
uint32_t c = (((uint32_t)telegram[i+0] << 16) | ((uint32_t)telegram[i+1] << 8 ) | ((uint32_t)telegram[i+2]) );
uint32_t c = tarrif < 4 ?
(((uint32_t)telegram[i+0] << 16) | ((uint32_t)telegram[i+1] << 8 ) | ((uint32_t)telegram[i+2]) )
: (((uint32_t)telegram[i+0] << 24) | ((uint32_t)telegram[i+1] << 16 ) | ((uint32_t)telegram[i+2] << 8) | ((uint32_t)telegram[i+3]) );
if (c == total_register) {
i += 3;
i += tarrif < 4 ? 3 : 4;
usage = bcd_2_int(telegram, i, 6);
ret_val = usage / 1000.0;
ESP_LOGVV(TAG, "Found register '%X' with '%d'->'%f'", total_register, usage, ret_val.value());
break;
}
i++;
}
return ret_val;
};

esphome::optional<double> get_total_energy_production_t_kwh(uint8_t tarrif, std::vector<unsigned char> &telegram) {
esphome::optional<double> ret_val{};
uint32_t usage = 0;
size_t i = 11;
uint64_t total_register = tarrif < 4 ? 0x8E00833C | ((tarrif*16) << 24) : 0x8E8010833C;
while (i < telegram.size()) {
uint64_t c = tarrif < 4 ?
(((uint32_t)telegram[i+0] << 24) | ((uint32_t)telegram[i+1] << 16 ) | ((uint32_t)telegram[i+2] << 8) | ((uint32_t)telegram[i+3]) )
: (((uint64_t)telegram[i+0] << 32) | ((uint32_t)telegram[i+1] << 24 ) | ((uint32_t)telegram[i+2] << 16) | ((uint32_t)telegram[i+3] << 8) | ((uint32_t)telegram[i+4]));
if (c == total_register) {
i += tarrif < 4 ? 4 : 5;
usage = bcd_2_int(telegram, i, 6);
ret_val = usage / 1000.0;
ESP_LOGVV(TAG, "Found register '%X' with '%d'->'%f'", total_register, usage, ret_val.value());
Expand Down Expand Up @@ -115,6 +143,7 @@ struct Amiplus: Driver
return ret_val;
};


esphome::optional<double> get_total_energy_production_kwh(std::vector<unsigned char> &telegram) {
esphome::optional<double> ret_val{};
uint32_t usage = 0;
Expand Down

0 comments on commit a798cc8

Please sign in to comment.