Skip to content

Commit

Permalink
Feature: auto-detect SoC precision for MQTT Battery provider
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreasBoehm committed Dec 7, 2024
1 parent 240918b commit bb59ca6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
1 change: 1 addition & 0 deletions include/MqttBattery.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class MqttBattery : public BatteryProvider {
String _voltageTopic;
String _dischargeCurrentLimitTopic;
std::shared_ptr<MqttBatteryStats> _stats = std::make_shared<MqttBatteryStats>();
uint8_t _socPrecision = 0;

void onMqttMessageSoC(espMqttClientTypes::MessageProperties const& properties,
char const* topic, uint8_t const* payload, size_t len, size_t index, size_t total,
Expand Down
15 changes: 12 additions & 3 deletions src/MqttBattery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,20 @@ void MqttBattery::onMqttMessageSoC(espMqttClientTypes::MessageProperties const&
return;
}

_stats->setSoC(*soc, 0/*precision*/, millis());
unsigned factor = 10;
uint8_t precision = 0;
while (precision < 2) {
if (std::floor(*soc * factor) == std::floor(*soc) * factor) { break; }
++precision;
factor *= 10;
}
_socPrecision = std::max(_socPrecision, precision);

_stats->setSoC(*soc, _socPrecision, millis());

if (_verboseLogging) {
MessageOutput.printf("MqttBattery: Updated SoC to %d from '%s'\r\n",
static_cast<uint8_t>(*soc), topic);
MessageOutput.printf("MqttBattery: Updated SoC to %.*f from '%s'\r\n",
_socPrecision, *soc, topic);
}
}

Expand Down

0 comments on commit bb59ca6

Please sign in to comment.