Skip to content

Commit

Permalink
Add more handling for NA values.
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Mar 22, 2024
1 parent 0ec3a15 commit fd112a9
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
36 changes: 30 additions & 6 deletions components/victron_ble/sensor/victron_sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,18 @@ void VictronSensor::setup() {
this->parent_->add_on_message_callback([this](const VictronBleData *msg) {
switch (msg->record_type) {
case VICTRON_BLE_RECORD_TYPE::MULTI_RS:
this->publish_state(msg->data.multi_rs.active_ac_in_power);
if (msg->data.multi_rs.active_ac_in_power == 0x7FFF) {
this->publish_state(NAN);
} else {
this->publish_state(msg->data.multi_rs.active_ac_in_power);
}
break;
case VICTRON_BLE_RECORD_TYPE::VE_BUS:
this->publish_state(msg->data.ve_bus.active_ac_in_power);
if (msg->data.ve_bus.active_ac_in_power == 0x3FFFF) {
this->publish_state(NAN);
} else {
this->publish_state(msg->data.ve_bus.active_ac_in_power);
}
break;
default:
ESP_LOGW(TAG, "[%s] Device has no `ac in power` field.", this->parent_->address_str().c_str());
Expand All @@ -51,7 +59,11 @@ void VictronSensor::setup() {
this->parent_->add_on_message_callback([this](const VictronBleData *msg) {
switch (msg->record_type) {
case VICTRON_BLE_RECORD_TYPE::INVERTER:
this->publish_state(msg->data.inverter.ac_apparent_power);
if (msg->data.inverter.ac_apparent_power == 0xFFFF) {
this->publish_state(NAN);
} else {
this->publish_state(msg->data.inverter.ac_apparent_power);
}
break;
default:
ESP_LOGW(TAG, "[%s] Device has no `ac apparent power` field.", this->parent_->address_str().c_str());
Expand Down Expand Up @@ -79,13 +91,25 @@ void VictronSensor::setup() {
this->parent_->add_on_message_callback([this](const VictronBleData *msg) {
switch (msg->record_type) {
case VICTRON_BLE_RECORD_TYPE::INVERTER_RS:
this->publish_state(msg->data.inverter_rs.ac_out_power);
if (msg->data.inverter_rs.ac_out_power == 0x7FFF) {
this->publish_state(NAN);
} else {
this->publish_state(msg->data.inverter_rs.ac_out_power);
}
break;
case VICTRON_BLE_RECORD_TYPE::MULTI_RS:
this->publish_state(msg->data.multi_rs.active_ac_out_power);
if (msg->data.multi_rs.active_ac_out_power == 0x7FFF) {
this->publish_state(NAN);
} else {
this->publish_state(msg->data.multi_rs.active_ac_out_power);
}
break;
case VICTRON_BLE_RECORD_TYPE::VE_BUS:
this->publish_state(msg->data.ve_bus.active_ac_out_power);
if (msg->data.ve_bus.ac_out_power == 0x3FFFF) {
this->publish_state(NAN);
} else {
this->publish_state(msg->data.ve_bus.ac_out_power);
}
break;
default:
ESP_LOGW(TAG, "[%s] Device has no `ac out power` field.", this->parent_->address_str().c_str());
Expand Down
6 changes: 6 additions & 0 deletions components/victron_ble/text_sensor/victron_text_sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,9 @@ void VictronTextSensor::publish_state_(VE_REG_DEVICE_STATE val) {
case VE_REG_DEVICE_STATE::EXTERNAL_CONTROL:
this->publish_state("External Control");
break;
case VE_REG_DEVICE_STATE::NOT_AVAILABLE:
this->publish_state("Not available");
break;
default:
ESP_LOGW(TAG, "[%s] Unknown device state (%u).", this->parent_->address_str().c_str(), (u_int8_t) val);
this->publish_state(to_string((u_int8_t) val));
Expand Down Expand Up @@ -488,6 +491,9 @@ void VictronTextSensor::publish_state_(VE_REG_CHR_ERROR_CODE val) {
case VE_REG_CHR_ERROR_CODE::INTERNAL_SUPPLY_D:
this->publish_state("Err 215 - Internal supply voltage error");
break;
case VE_REG_CHR_ERROR_CODE::NOT_AVAILABLE:
this->publish_state("Not available");
break;
default:
ESP_LOGW(TAG, "[%s] Unknown device error (%u).", this->parent_->address_str().c_str(), (u_int8_t) val);
this->publish_state(to_string((u_int8_t) val));
Expand Down
6 changes: 5 additions & 1 deletion components/victron_ble/victron_ble.h
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,8 @@ enum class VE_REG_DEVICE_STATE : u_int8_t {
BATTERY_SAFE = 0xF8,
// External Control
EXTERNAL_CONTROL = 0xFC,

NOT_AVAILABLE = 0xFF,
};

// Source: VE.Direct-Protocol-3.32.pdf & https://www.victronenergy.com/live/mppt-error-codes
Expand Down Expand Up @@ -524,6 +526,8 @@ enum class VE_REG_CHR_ERROR_CODE : u_int8_t {
INTERNAL_SUPPLY_C = 212,
// Err 215 - Internal supply voltage error
INTERNAL_SUPPLY_D = 215,

NOT_AVAILABLE = 0xFF,
};

struct VICTRON_BLE_RECORD_SOLAR_CHARGER { // NOLINT(readability-identifier-naming,altera-struct-pack-align)
Expand Down Expand Up @@ -801,7 +805,7 @@ struct VICTRON_BLE_RECORD_VE_BUS { // NOLINT(readability-identifier-naming,alte
// 1 W, -262,144 .. 262,142 W
int32_t active_ac_in_power : 19;
// 1 W, -262,144 .. 262,142 W
int32_t active_ac_out_power : 19;
int32_t ac_out_power : 19;
VE_REG_ALARM_NOTIFICATION alarm : 2;
// 1 °C, -40 .. 86 °C - Temperature = Record value - 40
u_int8_t battery_temperature : 7;
Expand Down

0 comments on commit fd112a9

Please sign in to comment.