Skip to content

Commit

Permalink
Moved home load calculation to seperate file and changed calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
GJSBRT committed Apr 12, 2024
1 parent 4af7861 commit b0bce65
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
32 changes: 32 additions & 0 deletions control/calculations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package control

import (
"gijs.eu/vonkje/metrics"
)

// calculateHomeLoad calculates the home load based on the active power of the inverter and power meter.
// If the sun is out, the home load is calculated based on the active power of the inverter.
// Many hours have been spent on trying to find a calculation for this any many more will be spent :/.
func calculateHomeLoad() (float64, error) {
var homeLoad float64

inverterInputPower, err := metrics.GetMetricLastEntrySum("sun2000", "active_power")
if err != nil {
return 0, err
}

powerMeterActivePower, err := metrics.GetMetricLastEntrySum("power_meter", "active_power")
if err != nil {
return 0, err
}

// Calculate home load when sun is out.
homeLoad = (inverterInputPower * 1000) - powerMeterActivePower

// If sun is not out, calculate home load based on power meter.
if homeLoad < 0 {
homeLoad = powerMeterActivePower - (inverterInputPower * 1000)
}

return homeLoad, nil
}
11 changes: 3 additions & 8 deletions control/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,20 +80,15 @@ func (c *Control) Start() {
metrics.SetMetricValue("control", "action", map[string]string{"action": "pull_from_grid"}, 0)

// 1. Get current home energy consumption
inverterInputPower, err := metrics.GetMetricLastEntrySum("sun2000", "active_power")
avgHomeLoad, err := calculateHomeLoad()
if err != nil {
c.errChannel <- err
continue
}
avgHomeLoad = math.Ceil(avgHomeLoad)

powerMeterActivePower, err := metrics.GetMetricLastEntrySum("power_meter", "active_power")
if err != nil {
c.errChannel <- err
continue
}

avgHomeLoad := (inverterInputPower * 1000) - powerMeterActivePower
if avgHomeLoad < 0 {
c.logger.WithFields(logrus.Fields{"avgHomeLoad": avgHomeLoad}).Info("Home load is negative, setting to 0")
avgHomeLoad = 0
}

Expand Down

0 comments on commit b0bce65

Please sign in to comment.