diff --git a/README.md b/README.md index faa2d93..eca36d8 100644 --- a/README.md +++ b/README.md @@ -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)) diff --git a/cheapest_energy_hours.jinja b/cheapest_energy_hours.jinja index c637bda..9315005 100644 --- a/cheapest_energy_hours.jinja +++ b/cheapest_energy_hours.jinja @@ -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 #} @@ -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 -%} @@ -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)) -}}