Skip to content

Commit 661cc9b

Browse files
[mqtt] add delay to ac/is_valid and dc/is_valid
prevents home assistant from interpreting invalid data after startup. the order of publishing topics is important. when going from available -> unavailable, is_valid must be published first, before any invalid data is sent.
1 parent 10ed086 commit 661cc9b

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/MqttHandleInverterTotal.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ MqttHandleInverterTotalClass::MqttHandleInverterTotalClass()
1717

1818
void MqttHandleInverterTotalClass::init(Scheduler& scheduler)
1919
{
20+
this->_connected_iterations = 0;
2021
scheduler.addTask(_loopTask);
2122
_loopTask.setInterval(Configuration.get().Mqtt.PublishInterval * TASK_SECOND);
2223
_loopTask.enable();
@@ -27,11 +28,24 @@ void MqttHandleInverterTotalClass::loop()
2728
// Update interval from config
2829
_loopTask.setInterval(Configuration.get().Mqtt.PublishInterval * TASK_SECOND);
2930

31+
if (!MqttSettings.getConnected()) {
32+
this->_connected_iterations = 0;
33+
} else {
34+
this->_connected_iterations++;
35+
}
36+
3037
if (!MqttSettings.getConnected() || !Hoymiles.isAllRadioIdle()) {
3138
_loopTask.forceNextIteration();
3239
return;
3340
}
3441

42+
if(this->_connected_iterations < 2) {
43+
// publish is_valid false after connecting to ensure statistics don't get a wrong value during startup
44+
MqttSettings.publish("ac/is_valid", String(false));
45+
MqttSettings.publish("dc/is_valid", String(false));
46+
return;
47+
}
48+
3549
MqttSettings.publish("ac/power", String(Datastore.getTotalAcPowerEnabled(), Datastore.getTotalAcPowerDigits()));
3650
MqttSettings.publish("ac/yieldtotal", String(Datastore.getTotalAcYieldTotalEnabled(), Datastore.getTotalAcYieldTotalDigits()));
3751
MqttSettings.publish("ac/yieldday", String(Datastore.getTotalAcYieldDayEnabled(), Datastore.getTotalAcYieldDayDigits()));

0 commit comments

Comments
 (0)