Hi @Koktail ,
I’ve just addressed an edge-case issue in the DSMR reader. This fix resolves a recent problem I faced with duplicated registered generated power when transitioning from single-phase to triple-phase power.
koktaildotcom:master
← lucasvdh:master
opened 10:27AM - 30 Oct 23 UTC
# Fix for incorrectly defaulted values
## Summary
This pull request addresse… s an issue related to the reporting of electricity consumption and production values for triple-phase power.
## Context
After installing a new power meter, I transitioned from single-phase to triple-phase power. However, the existing reader began reporting incorrect values. The issue arises from a unique edge-case scenario: most of my home is connected to the first phase, while solar panels are connected to the second phase. This results in power consumption on the first phase and power generation on the second phase.
## Problem
The discrepancy occurs in the calculation of `electricity.instantaneous.power.negative.L1` due to the defaulting behavior of `output.power.instantaneousProducedElectricityL1`:
```node
output.power.instantaneousProducedElectricityL1 || output.power.actualProduced || 0,
```
This defaulting behavior should only apply when `output.power.instantaneousProducedElectricityL1` is `undefined`, not when it is `0`.
## Solution
A new function `getDefaultedValue` has been introduced to handle the defaulting behavior. It iterates through provided arguments and returns the first non-undefined value or 0 if all provided arguments are undefined:
```node
function getDefaultedValue(...args) {
for (let arg of args) {
if (arg !== undefined) {
return arg;
}
}
return 0;
}
```
## Example
```node
const output = {
power: {
actualProduced: 123,
instantaneousProducedElectricityL1: 0
}
}
// Incorrectly outputs 123
console.log(output.power.instantaneousProducedElectricityL1 || output.power.actualProduced || 0)
// Correctly outputs 0
console.log(getDefaultedValue(output.power.instantaneousProducedElectricityL1, output.power.actualProduced))
```
This change ensures that the defaulting behavior operates as intended, addressing the reported discrepancies.