Skip to content

Commit

Permalink
Add time_min and time_max modes (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
basbruss authored Jul 4, 2023
1 parent 8f8eda8 commit 56b3ce3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Other optional fields are listed below:
|name|type|default|example|description|
|---|---|---|---|---|
|`lowest`|boolean|`true`|`false`|Boolean to select if the marco should find the lowest price, set to `false` to find the highest price|
|`mode`|string|`"start"`|`"average"`|You can choose what to output, these values are accepted: `min` (lowest price in hours found), `max` (highest price in hours found), `average` (average price in hours found), `start` (start of the hours found), `end` (end of the hours found), `list` (list with the prices in hours found), `weighted_average` (the average price taking into account the weight for the `top_hour`)|
|`mode`|string|`"start"`|`"average"`|You can choose what to output, these values are accepted: `min` (lowest price in hours found), `max` (highest price in hours found),`time_min` (time of lowest price in hours found),`time_max` (time of highest price in hours found), `average` (average price in hours found), `start` (start of the hours found), `end` (end of the hours found), `list` (list with the prices in hours found), `weighted_average` (the average price taking into account the weight for the `top_hour`)|
|`look_ahead`|boolean|`false`|`true`|When set to true, only the hours as of the current hour are taken into account. This overrides the `start` time if that time is earlier than the current hour.
|`time_format`|string|`none`|`"time24"`|You can use `time12` for the 12-hour format including `AM` or `PM`, `time24` for the 24-hour format, or any custom format using the variables from the python strftime method ([cheatsheet](https://strftime.org))

Expand Down
11 changes: 8 additions & 3 deletions cheapest_energy_hours.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
{# Check if data is available and mode is correct#}
{%- if not today and not tomorrow -%}
No valid data in selected sensor
{%- elif m not in ['min', 'max', 'average', 'start', 'end', 'list', 'weighted_average'] -%}
{%- elif m not in ['min', 'max', 'average', 'start', 'end', 'list', 'weighted_average','time_min','time_max'] -%}
Invalid mode selected
{%- else -%}
{# Set defaults for variables which are not provided #}
Expand Down Expand Up @@ -63,7 +63,7 @@
{%- set values = ns.values -%}
{%- set str = false -%}
{%- endif -%}
{%- set ns = namespace(average=none, start=none, min=none, max=none, weighted_average=none) -%}
{%- set ns = namespace(average=none, start=none, min=none, max=none, weighted_average=none, time_min=none, time_max=none) -%}
{%- for i in values[:values|length-(h*wp-1)] -%}
{%- set ix = loop.index0 -%}
{%- set list = values[ix:ix+h*wp] | map(attribute=vk) | list -%}
Expand All @@ -89,10 +89,15 @@
{%- set ns.average = list | average -%}
{%- set ns.start = as_datetime(i[tk]) if str else i[tk] -%}
{%- set ns.end = ns.start + timedelta(hours=h) -%}
{%- set index_min = ns.list.index(ns.min) -%}
{%- set index_max = ns.list.index(max) -%}
{%- set ns.time = values[ix:ix+h*wp] | map(attribute=tk) | list -%}
{%- set ns.time_min = as_datetime(ns.time[index_min]) if str else ns.time[index_min] -%}
{%- set ns.time_max = as_datetime(ns.time[index_max]) if str else ns.time[index_max] -%}
{%- endif -%}
{%- endfor -%}
{# output date based on the selected mode#}
{%- if m in [ 'start', 'end'] -%}
{%- if m in [ 'start', 'end','time_min','time_max'] -%}
{%- if time_format is defined -%}
{%- set format = dict(time12='%I:%M %p', time24='%H:%M') -%}
{{- ns[m].strftime(format[time_format] | default(time_format)) -}}
Expand Down

0 comments on commit 56b3ce3

Please sign in to comment.