Skip to content

Commit

Permalink
Treat battery as a consumer (no prioritisation)
Browse files Browse the repository at this point in the history
  • Loading branch information
ledermann committed Feb 8, 2025
1 parent 56ec6b1 commit f2bec54
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 32 deletions.
29 changes: 7 additions & 22 deletions lib/splitter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ def initialize(config, **kwargs)
:custom_power

def call
remaining = grid_power_for_consumers
remaining = grid_import_power

# Prioritize wallbox power over all other consumers
# Prioritize wallbox and battery charging over all other consumers
if remaining&.positive? && wallbox_power&.positive?
wallbox_power_grid = [wallbox_power, remaining].min
remaining -= wallbox_power_grid
Expand All @@ -32,7 +32,8 @@ def call
# Distribute the remaining grid power among the other consumers
{
wallbox_power_grid:,
battery_charging_power_grid:,
battery_charging_power_grid:
grid_power(remaining, battery_charging_power),
house_power_grid: grid_power(remaining, house_power),
heatpump_power_grid: grid_power(remaining, heatpump_power),
}.tap do |result|
Expand All @@ -46,27 +47,11 @@ def call

private

# Calculate the grid power used by all consumers
def grid_power_for_consumers
return unless grid_import_power

# When battery is charging while importing from the grid,
# the battery is prioritized over all consumers.
# Examples: Emergency charging or charging during low grid prices
[grid_import_power - (battery_charging_power || 0), 0].max
end

def battery_charging_power_grid
return unless grid_power_for_consumers

# All the grid power not used by consumers is used to charge the battery
grid_import_power - grid_power_for_consumers
end

# Sum up all consumers except wallbox power
# Sum up all consumers except wallbox power and battery charging power
def other_total
@other_total ||=
(house_power || 0) + (heatpump_power || 0) + custom_power_total
(house_power || 0) + (heatpump_power || 0) +
(battery_charging_power || 0) + custom_power_total
end

# Sum up only the custom sensors explicitly excluded from house power
Expand Down
44 changes: 34 additions & 10 deletions spec/lib/splitter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,22 +126,22 @@
context 'when battery is charging from grid' do
let(:record) do
{
# inverter_power: 0
# inverter_power: 100
grid_import_power: 100,
house_power: 10,
heatpump_power: 30,
wallbox_power: 60,
wallbox_power: 30,
battery_charging_power: 100,
}
end

it 'returns battery-first' do
expect(call).to eq(
{
house_power_grid: 0,
heatpump_power_grid: 0,
wallbox_power_grid: 0,
battery_charging_power_grid: 100,
house_power_grid: 5,
heatpump_power_grid: 15,
wallbox_power_grid: 30,
battery_charging_power_grid: 50,
},
)
end
Expand All @@ -150,8 +150,8 @@
context 'when battery is charging partly from grid' do
let(:record) do
{
# inverter_power: 20
grid_import_power: 40,
# inverter_power: 30
grid_import_power: 30,
house_power: 10,
heatpump_power: 30,
wallbox_power: 0,
Expand All @@ -160,13 +160,13 @@
}
end

it 'returns all zero' do
it 'calculates' do
expect(call).to eq(
{
house_power_grid: 5,
heatpump_power_grid: 15,
wallbox_power_grid: 0,
battery_charging_power_grid: 20,
battery_charging_power_grid: 10,
},
)
end
Expand Down Expand Up @@ -220,6 +220,30 @@
end
end

context 'when wallbox and battery charging' do
let(:record) do
{
# inverter_power: 2000
grid_import_power: 21_000,
wallbox_power: 21_000,
house_power: 600,
heatpump_power: 100,
battery_charging_power: 1300,
}
end

it 'returns wallbox-first' do
expect(call).to eq(
{
battery_charging_power_grid: 0.0,
heatpump_power_grid: 0.0,
house_power_grid: 0.0,
wallbox_power_grid: 21_000,
},
)
end
end

context 'when custom power is given (100% consumption)' do
let(:record) do
{
Expand Down

0 comments on commit f2bec54

Please sign in to comment.