From 28a806f584f3dd6bf5ed1e706569a07e22d4fa72 Mon Sep 17 00:00:00 2001 From: Aart van Halteren Date: Mon, 10 Jan 2022 22:27:25 +0100 Subject: [PATCH] Fewer no grid charge months --- app/models/cost.rb | 10 +++++----- app/models/entsoe.rb | 6 ++++-- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/app/models/cost.rb b/app/models/cost.rb index 1a436a7..228bb4d 100644 --- a/app/models/cost.rb +++ b/app/models/cost.rb @@ -3,8 +3,8 @@ EASY_ENERGY_TARIFFS = {} class Cost - - attr_reader :battery, :max_charge_kwh, :entsoe + attr_accessor :max_charge_kwh + attr_reader :battery, :entsoe def initialize(battery_capacity=10.0, max_charge=5.0) @entsoe = Entsoe.new @@ -161,8 +161,8 @@ class Cost # charge battery with return_kwh return_kwh -= battery.charge(return_kwh) - if lowest_hour.eql?(formatted_hour) # lowest_hour = "" if small difference between high/low - # charge battery during lowest hour + if (lowest_hour.eql?(formatted_hour) || entsoe.price_at(formatted_hour) < 0) # lowest_hour = "" if small difference between high/low + # charge battery during lowest hour, or when prices are negative usage_kwh += battery.charge(max_charge_kwh) else # if during expensive hours || more than kwh then discharge_battery @@ -197,7 +197,7 @@ class Cost oxxio_rate, oxxio_cost] - p "%s,%s,%s,%s,%s,%s,%s,%03f,%s,%s,%s,%s,%s" % (one_hour[0..7] + one_hour[8..12].map{|c| format_cost(c)}) + p "%s,%s,%s,%s,%s,%s,%s,%0.02f,%s,%s,%s,%s,%s" % (one_hour[0..7] + one_hour[8..12].map{|c| format_cost(c)}) result << one_hour hour_start = hour_start.advance(:hours => 1) diff --git a/app/models/entsoe.rb b/app/models/entsoe.rb index 83f925b..66ed0bb 100644 --- a/app/models/entsoe.rb +++ b/app/models/entsoe.rb @@ -10,12 +10,14 @@ class Entsoe URL = 'https://transparency.entsoe.eu' + attr_accessor :no_grid_charge_months attr_reader :storage_cost def initialize(api_key = "c2287e07-0c26-4950-b430-22b7f75a8f2e") @api_key = api_key @kwh_prices = {} @storage_cost = 0.05 # how much does it cost to store 1 kwh in battery + @no_grid_charge_months = [5,6,7] # months where we assume there is enough surplus from sun power end def price_at(formatted_hour) @@ -42,8 +44,8 @@ class Entsoe sorted_prices = prices_at(date).to_a.sort_by(&:last) # sort according to price highest_hour = sorted_prices.last[0] - # From Apr-Oct: do not charge; every hour that has cost > 0 is a high_hour - if [4,5,6,7,8,9,10].include?(date.month) + # Some months: do not charge; every hour that has cost > 0 is a high_hour + if @no_grid_charge_months.include?(date.month) lowest_hour = "" # effectively no charging from grid high_hours = sorted_prices.select{|p| p[1] > 0}.to_h.keys else