Skip to content

Commit

Permalink
Fix rounding in estimated cost calcualtion (#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheFes authored Mar 14, 2024
1 parent 4541665 commit 06c1ded
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions cheapest_energy_hours.jinja
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{%- set _version = '5.4.3'-%}
{%- set _version = 'v5.4.4'-%}

{#- sub-macro to format datetimes to selected time_format -#}
{%- macro _format_date(datetime, time_format) -%}
Expand Down Expand Up @@ -636,7 +636,7 @@
{%- set macro_output = output.all if mode == 'split' else output[mode] -%}
{%- else -%}
{#- create output for all modes -#}
{%- set output = namespace(average=none, start=none, min=none, max=none, weighted_average=none, compare=none, time_min=none, time_max=none, list=[], is_now=false, extreme_now=none, estimated_costs="unknown") -%}
{%- set output = namespace(average=none, start=none, min=none, max=none, weighted_average=none, compare=none, time_min=none, time_max=none, prices_list=[], list=[], is_now=false, extreme_now=none, estimated_costs="unknown") -%}
{%- set last_values = (h * no_weight_points) | round(0) | int -%}
{%- for i in range(values | count - (last_values - 1)) -%}
{%- set prices_list = values[i:i + last_values] | map(attribute=value_key) | list -%}
Expand Down Expand Up @@ -665,16 +665,16 @@
{%- set min, max = (prices_list | min, prices_list | max) if pt == 0 else (original_price_list | min, original_price_list | max) -%}
{%- set avg = prices_list | average | round(pr) if pt == 0 else original_price_list | average | round(pr) -%}
{%- if loop.first or (((a <= b) if lowest else (a >= b)) if latest_possible | bool else ((a < b) if lowest else (a > b))) -%}
{%- set prices_list = original_price_list if pt != 0 else prices_list -%}
{%- set output.list = prices_list | map('round', pr) | list -%}
{%- set output.prices_list = original_price_list if pt != 0 else prices_list -%}
{%- set output.list = output.prices_list | map('round', pr) | list -%}
{%- set output.min = min | round(pr) -%}
{%- set output.max = max | round(pr) -%}
{%- set output.weighted_average = a | round(pr) if pt == 0 else original_w_avg | default(avg) | round(pr) -%}
{%- set output.compare = [a, b | default(a, true)] | min if lowest else [a, b | default(a, true)] | max %}
{%- set output.average = avg -%}
{%- set output.start = _format_date(values[i][time_key], time_format | default(none)) -%}
{%- set output.end = _format_date(values[i][time_key] + timedelta(hours=h), time_format | default(none)) -%}
{%- set index_min, index_max = prices_list.index(min), prices_list.index(max) -%}
{%- set index_min, index_max = output.prices_list.index(min), output.prices_list.index(max) -%}
{%- set output.time = values[i:i+last_values] | map(attribute=time_key) | list -%}
{%- set output.time_min = _format_date(output.time[index_min], time_format | default(none)) -%}
{%- set output.time_max = _format_date(output.time[index_max], time_format | default(none)) -%}
Expand All @@ -683,7 +683,7 @@
{%- endfor -%}
{#- determine estimated_costs if kwh is provided -#}
{%- if kwh is not none -%}
{%- set price_count = output.list | count %}
{%- set price_count = output.prices_list | count %}
{%- if w is none -%}
{%- set kwh_list = [kwh/price_count] * price_count -%}
{%- else -%}
Expand All @@ -694,8 +694,9 @@
{%- set kwh_list = ns.kwh_list-%}
{%- endif -%}
{%- set output.estimated_costs = 0 -%}
{%- for p in output.list -%}
{%- set output.estimated_costs = (output.estimated_costs + p * kwh_list[loop.index0]) | round(pr) -%}
{%- for p in output.prices_list -%}
{%- set output.estimated_costs = (output.estimated_costs + p * kwh_list[loop.index0]) -%}
{%- set output.estimated_costs = output.estimated_costs | round(pr) if loop.last else output.estimated_costs -%}
{%- endfor -%}
{%- endif -%}
{#- determine extreme_now -#}
Expand Down Expand Up @@ -738,7 +739,7 @@
{%- set macro_output = value_on_error if use_voe and not debug else error_msg -%}
{%- endif -%}
{%- if debug -%}
{%- set dict_add = dict(values_count=values|count, datapoints_source=datapoints_source, datapoints_used=datapoints_used) -%}
{%- set dict_add = dict(values_count=values|count, datapoints_source=datapoints_source, datapoints_used=datapoints_used, original_prices=output.prices_list, price_count=price_count, kwh_list=kwh_list) -%}
{{- dict(
version=_version,
output=macro_output,
Expand Down

0 comments on commit 06c1ded

Please sign in to comment.