Skip to content

Commit

Permalink
Publish mqtt string stats values periodically if they are set to zero…
Browse files Browse the repository at this point in the history
… if unreachable
  • Loading branch information
tbnobody committed Sep 4, 2023
1 parent abf9563 commit 7d90937
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
3 changes: 3 additions & 0 deletions include/MqttHandleInverter.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "Configuration.h"
#include <Hoymiles.h>
#include <TimeoutHelper.h>
#include <espMqttClient.h>

class MqttHandleInverterClass {
Expand All @@ -19,6 +20,8 @@ class MqttHandleInverterClass {
uint32_t _lastPublishStats[INV_MAX_COUNT];
uint32_t _lastPublish;

TimeoutHelper _statsTimeout;

FieldId_t _publishFields[14] = {
FLD_UDC,
FLD_IDC,
Expand Down
8 changes: 7 additions & 1 deletion src/MqttHandleInverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#define TOPIC_SUB_POWER "power"
#define TOPIC_SUB_RESTART "restart"

#define PUBLISH_MAX_INTERVAL 60000

MqttHandleInverterClass MqttHandleInverter;

void MqttHandleInverterClass::init()
Expand Down Expand Up @@ -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)) {
Expand Down

0 comments on commit 7d90937

Please sign in to comment.