Publish mqtt string stats values periodically if they are set to zero if unreachable

This commit is contained in:
Thomas Basler 2023-09-04 18:20:51 +02:00
parent abf95634db
commit 7d90937d0f
2 changed files with 10 additions and 1 deletions

View File

@ -3,6 +3,7 @@
#include "Configuration.h"
#include <Hoymiles.h>
#include <TimeoutHelper.h>
#include <espMqttClient.h>
class MqttHandleInverterClass {
@ -19,6 +20,8 @@ private:
uint32_t _lastPublishStats[INV_MAX_COUNT];
uint32_t _lastPublish;
TimeoutHelper _statsTimeout;
FieldId_t _publishFields[14] = {
FLD_UDC,
FLD_IDC,

View File

@ -14,6 +14,8 @@
#define TOPIC_SUB_POWER "power"
#define TOPIC_SUB_RESTART "restart"
#define PUBLISH_MAX_INTERVAL 60000
MqttHandleInverterClass MqttHandleInverter;
void MqttHandleInverterClass::init()
@ -92,9 +94,13 @@ void MqttHandleInverterClass::loop()
}
uint32_t lastUpdate = inv->Statistics()->getLastUpdate();
if (lastUpdate > 0 && lastUpdate != _lastPublishStats[i]) {
if (lastUpdate > 0 && (lastUpdate != _lastPublishStats[i] || (inv->getZeroValuesIfUnreachable() && _statsTimeout.occured()))) {
_lastPublishStats[i] = lastUpdate;
// At first a change of the stats have to occour. Then the stats
// are published on every change or every 60 seconds
_statsTimeout.set(PUBLISH_MAX_INTERVAL);
// Loop all channels
for (auto& t : inv->Statistics()->getChannelTypes()) {
for (auto& c : inv->Statistics()->getChannelsByType(t)) {