Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: incorrect defaulted values #27

Merged
merged 1 commit into from Mar 22, 2024

Conversation

lucasvdh
Copy link
Contributor

Fix for incorrectly defaulted values

Summary

This pull request addresses 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:

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:

function getDefaultedValue(...args) {
  for (let arg of args) {
    if (arg !== undefined) {
      return arg;
    }
  }
  return 0;
}

Example

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.

@koktaildotcom koktaildotcom merged commit fde1433 into koktaildotcom:master Mar 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants