Feature: Implemented MQTT publishing of total values
If one or more inverter is not reachable the flag is_valid changes to zero. Disabled inverters are ignored.
This commit is contained in:
parent
793cd9db91
commit
26cbc496a7
15
include/MqttHandleInverterTotal.h
Normal file
15
include/MqttHandleInverterTotal.h
Normal file
@ -0,0 +1,15 @@
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
#pragma once
|
||||
|
||||
#include <TimeoutHelper.h>
|
||||
|
||||
class MqttHandleInverterTotalClass {
|
||||
public:
|
||||
void init();
|
||||
void loop();
|
||||
|
||||
private:
|
||||
TimeoutHelper _lastPublish;
|
||||
};
|
||||
|
||||
extern MqttHandleInverterTotalClass MqttHandleInverterTotal;
|
||||
77
src/MqttHandleInverterTotal.cpp
Normal file
77
src/MqttHandleInverterTotal.cpp
Normal file
@ -0,0 +1,77 @@
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
/*
|
||||
* Copyright (C) 2023 Thomas Basler and others
|
||||
*/
|
||||
#include "MqttHandleInverterTotal.h"
|
||||
#include "Configuration.h"
|
||||
#include "MqttSettings.h"
|
||||
#include <Hoymiles.h>
|
||||
|
||||
MqttHandleInverterTotalClass MqttHandleInverterTotal;
|
||||
|
||||
void MqttHandleInverterTotalClass::init()
|
||||
{
|
||||
_lastPublish.set(Configuration.get().Mqtt_PublishInterval * 1000);
|
||||
}
|
||||
|
||||
void MqttHandleInverterTotalClass::loop()
|
||||
{
|
||||
if (!MqttSettings.getConnected() || !Hoymiles.isAllRadioIdle()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (_lastPublish.occured()) {
|
||||
float totalAcPower = 0;
|
||||
float totalDcPower = 0;
|
||||
float totalDcPowerIrr = 0;
|
||||
float totalDcPowerIrrInst = 0;
|
||||
float totalAcYieldDay = 0;
|
||||
float totalAcYieldTotal = 0;
|
||||
uint8_t totalAcPowerDigits = 0;
|
||||
uint8_t totalDcPowerDigits = 0;
|
||||
uint8_t totalAcYieldDayDigits = 0;
|
||||
uint8_t totalAcYieldTotalDigits = 0;
|
||||
bool totalReachable = true;
|
||||
|
||||
for (uint8_t i = 0; i < Hoymiles.getNumInverters(); i++) {
|
||||
auto inv = Hoymiles.getInverterByPos(i);
|
||||
if (inv == nullptr || !inv->getEnablePolling()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!inv->isReachable()) {
|
||||
totalReachable = false;
|
||||
}
|
||||
|
||||
for (auto& c : inv->Statistics()->getChannelsByType(TYPE_AC)) {
|
||||
totalAcPower += inv->Statistics()->getChannelFieldValue(TYPE_AC, c, FLD_PAC);
|
||||
totalAcPowerDigits = max<uint8_t>(totalAcPowerDigits, inv->Statistics()->getChannelFieldDigits(TYPE_AC, c, FLD_PAC));
|
||||
|
||||
totalAcYieldDay += inv->Statistics()->getChannelFieldValue(TYPE_AC, c, FLD_YD);
|
||||
totalAcYieldDayDigits = max<uint8_t>(totalAcYieldDayDigits, inv->Statistics()->getChannelFieldDigits(TYPE_AC, c, FLD_YD));
|
||||
|
||||
totalAcYieldTotal += inv->Statistics()->getChannelFieldValue(TYPE_AC, c, FLD_YT);
|
||||
totalAcYieldTotalDigits = max<uint8_t>(totalAcYieldTotalDigits, inv->Statistics()->getChannelFieldDigits(TYPE_AC, c, FLD_YT));
|
||||
}
|
||||
for (auto& c : inv->Statistics()->getChannelsByType(TYPE_DC)) {
|
||||
totalDcPower += inv->Statistics()->getChannelFieldValue(TYPE_DC, c, FLD_PDC);
|
||||
totalDcPowerDigits = max<uint8_t>(totalDcPowerDigits, inv->Statistics()->getChannelFieldDigits(TYPE_DC, c, FLD_PDC));
|
||||
|
||||
if (inv->Statistics()->getStringMaxPower(c) > 0) {
|
||||
totalDcPowerIrr += inv->Statistics()->getChannelFieldValue(TYPE_DC, c, FLD_PDC);
|
||||
totalDcPowerIrrInst += inv->Statistics()->getStringMaxPower(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
MqttSettings.publish("ac/power", String(totalAcPower, static_cast<unsigned int>(totalAcPowerDigits)));
|
||||
MqttSettings.publish("ac/yieldtotal", String(totalAcYieldDay, static_cast<unsigned int>(totalAcYieldDayDigits)));
|
||||
MqttSettings.publish("ac/yieldday", String(totalAcYieldTotal, static_cast<unsigned int>(totalAcYieldTotalDigits)));
|
||||
MqttSettings.publish("ac/is_valid", String(totalReachable));
|
||||
MqttSettings.publish("dc/power", String(totalDcPower, static_cast<unsigned int>(totalAcPowerDigits)));
|
||||
MqttSettings.publish("dc/irradiation", String(totalDcPowerIrrInst > 0 ? totalDcPowerIrr / totalDcPowerIrrInst * 100.0f : 0, 3));
|
||||
MqttSettings.publish("dc/is_valid", String(totalReachable));
|
||||
|
||||
_lastPublish.set(Configuration.get().Mqtt_PublishInterval * 1000);
|
||||
}
|
||||
}
|
||||
@ -10,6 +10,7 @@
|
||||
#include "MqttHandleDtu.h"
|
||||
#include "MqttHandleHass.h"
|
||||
#include "MqttHandleInverter.h"
|
||||
#include "MqttHandleInverterTotal.h"
|
||||
#include "MqttSettings.h"
|
||||
#include "NetworkSettings.h"
|
||||
#include "NtpSettings.h"
|
||||
@ -92,6 +93,7 @@ void setup()
|
||||
MqttSettings.init();
|
||||
MqttHandleDtu.init();
|
||||
MqttHandleInverter.init();
|
||||
MqttHandleInverterTotal.init();
|
||||
MqttHandleHass.init();
|
||||
MessageOutput.println("done");
|
||||
|
||||
@ -146,6 +148,7 @@ void loop()
|
||||
yield();
|
||||
MqttHandleInverter.loop();
|
||||
yield();
|
||||
MqttHandleInverterTotal.loop();
|
||||
MqttHandleHass.loop();
|
||||
yield();
|
||||
WebApi.loop();
|
||||
|
||||
Loading…
Reference in New Issue
Block a user