Skip to content

Commit

Permalink
Add price_factor parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
TheFes committed Nov 8, 2023
1 parent 30ad5e0 commit 8acb3a5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 20 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ Optional parameters are listed below:
|`precision`|integer|`5`|`2`|The number of decimals used for the price output|
|`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))
|`price_factor`|flaot|`1`|`0.01`|All prices will be multiplied with this value, so if your pr`ices are in cents and you want divided by 100, you can use `0.01`. Or if you want to add 20% VAT, you can use `1.2`
|`value_on_error`|any|error description|`as_datetime('2099-12-31)`|You can optionally provide a value to be outputted in case there is an error. This can be useful if you eg want it to use as state in a template sensor which has `device_class: timestamp` which will run in error if the state value is not as expected. Or if you output the data on your dashboard in a markup card and want to provide your own message.

### Output modes
Expand Down
47 changes: 27 additions & 20 deletions cheapest_energy_hours.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,32 @@
{%- macro _find_attr(sensor, type) -%}
{%- set td = now().date() | string -%}
{%- set tm = (now() + timedelta(days=1)).date() | string -%}
{%- for a in states[sensor].attributes.items() | rejectattr('1', 'string') | rejectattr('1', 'mapping') | selectattr('1', 'iterable') -%}
{%- if a[1][0] is mapping -%}
{%- set ns = namespace(td=false, tm=false) -%}
{%- for i in a[1] -%}
{%- set ns.td = i.values() | map('string') | select('contains', td) | list | count > 0 if not ns.td else true %}
{%- set ns.tm = i.values() | map('string') | select('contains', tm) | list | count > 0 if not ns.tm else true %}
{%- endfor -%}
{%- set results =
{
'all': ns.td and ns.tm,
'today': ns.td and not ns.tm,
'tomorrow': not ns.td and ns.tm
}
{%- if sensor | has_value -%}
{%- for a in states[sensor].attributes.items()
| rejectattr('1', 'string')
| rejectattr('1', 'mapping')
| selectattr('1', 'iterable')
-%}
{%- if results[type] -%}
{{ a[0] }}
{%- break -%}
{%- endif -%}
{%- endif -%}
{%- endfor -%}
{%- if a[1][0] is mapping -%}
{%- set ns = namespace(td=false, tm=false) -%}
{%- for i in a[1] -%}
{%- set ns.td = i.values() | map('string') | select('contains', td) | list | count > 0 if not ns.td else true %}
{%- set ns.tm = i.values() | map('string') | select('contains', tm) | list | count > 0 if not ns.tm else true %}
{%- endfor -%}
{%- set results =
{
'all': ns.td and ns.tm,
'today': ns.td and not ns.tm,
'tomorrow': not ns.td and ns.tm
}
-%}
{%- if results[type] -%}
{{ a[0] }}
{%- break -%}
{%- endif -%}
{%- endif -%}
{%- endfor -%}
{%- endif -%}
{%- endmacro -%}

{%- macro cheapest_energy_hours(
Expand All @@ -51,6 +57,7 @@
lowest=true,
look_ahead=false,
precision=5,
price_factor=1,
no_weight_points=1,
weight=none,
program=none,
Expand Down Expand Up @@ -150,7 +157,7 @@
{%- for item in data -%}
{%- set time = item[time_key] -%}
{%- set time = as_datetime(time) if time is string else time -%}
{%- set rebuild.data = rebuild.data + [dict(time=time, value=item[value_key])] -%}
{%- set rebuild.data = rebuild.data + [dict(time=time, value=item[value_key] * price_factor | float(1))] -%}
{%- endfor -%}
{# determine how many data points per hour are used #}
{%- set dph = 3600 / (rebuild.data[1].time - rebuild.data[0].time).seconds -%}
Expand Down

0 comments on commit 8acb3a5

Please sign in to comment.