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 7f62362 commit 3b4874f
Showing 1 changed file with 12 additions and 3 deletions.
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 %.2f from '%s'\r\n",
*soc, topic);
}
}

Expand Down

0 comments on commit 3b4874f

Please sign in to comment.