Skip to content

Commit 75d3e90

Browse files
committed
Harden against the edge case where the inverter responds too slowly to have a valid measurement. Fixes #9.
1 parent f5db4fd commit 75d3e90

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ SMAHomeManager.prototype = {
615615
.then((producedWatts) => {
616616
const estimatedMsOffset = performance.now() - before;
617617
const [timestamp, netWatts] = this._parseDatagram(msg, rinfo);
618-
this._processMeasurement(producedWatts, netWatts, timestamp);
618+
this._processMeasurement(producedWatts, netWatts, timestamp, estimatedMsOffset);
619619
})
620620
}.bind(this));
621621

@@ -628,7 +628,7 @@ SMAHomeManager.prototype = {
628628
// 2. Updates HomeKit.
629629
//
630630
// (Should be called in regular intervals, 1 s assumed.)
631-
_processMeasurement: function(producedWattsFromInverter, netWattsFromEnergyManager, timestampFromEnergyManager) {
631+
_processMeasurement: function(producedWattsFromInverter, netWattsFromEnergyManager, timestampFromEnergyManager, estimatedMsOffsetInverter) {
632632
// Capture the observations in a "measurement" object.
633633
const importWatts = netWattsFromEnergyManager > 0 ? netWattsFromEnergyManager: 0;
634634
const exportWatts = netWattsFromEnergyManager < 0 ? -netWattsFromEnergyManager: 0;
@@ -639,6 +639,10 @@ SMAHomeManager.prototype = {
639639
production: producedWattsFromInverter,
640640
consumption: importWatts + producedWattsFromInverter - exportWatts,
641641
};
642+
if (estimatedMsOffsetInverter > 1000 || measurement.consumption < 0) {
643+
this.log.warn(`Inverter took ${estimatedMsOffsetInverter} ms to respond, resulting in an invalid measurement. Dropping measurement.`);
644+
return;
645+
}
642646

643647
// Store measurement and compute next measurement index.
644648
var currentIndex = this.nextMeasurementIndex;

0 commit comments

Comments
 (0)