Skip to content

Commit

Permalink
⌛Remove need for hours when weight is provided (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheFes authored May 8, 2023
1 parent f02b6c6 commit eca2af3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ Other optional fields are listed below:
It could be that your device doesn't have a stable consumption during the period it is on. A washing machine for example will use most power at the start of the program to heat up the water, and at the end, for the spinning to get the water out again.
To take this into account, you can provide a `weight` list which will be used to find the optimal period to turn your device on. You can even break down the hours into smaller parts, so the result could be that the device should be turned on at eg 11:45.
To break down into smaller time fractions, you can provide a number in the `no_weight_points` setting. So to break it down into parts of 15 minutes, you need to provide `4` for this setting (4 weight points per hour).
The list in the `weight` setting, should have the same number of items as the `hours` multiplied with the `no_weight_points`. If a list is provided, but it is has a lower amount of items then expected, it is assumed that the device is not active for the full amount of hours, and a weight of `0` is added for the missing items.

The number or hours will be calculated based on `weight` and `no_weight_points`. So eg if there are 4 weight points per hour, and the `weight` list has 8 items, `hours` will be set to `2` (8/4). This value will be rounded up, and zeros will be added to the `weight` list to make sure the number of items in the list matches the expected number.

In case `hours` is provided, this will overrule the calculated number of hours, if there are more items in the `weight` list based on the provided number of hours, these will be removed from the list, and not taken into account.

|name|type|default|example|description|
|---|---|---|---|---|
Expand All @@ -69,7 +72,6 @@ It will survive reboots, so you can refer to the data directly in the template f

You can find the script, sensor and an automation example [here](./example_package/package.yaml)


### Basic examples

You always need to import the macro, so the first line should always be:
Expand Down
14 changes: 12 additions & 2 deletions cheapest_energy_hours.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,24 @@
{%- set today = state_attr(sensor, 'raw_today') -%}
{%- set tomorrow = state_attr(sensor, 'raw_tomorrow') -%}
{%- set m = mode | default('start') -%}
{%- set h = hours | default(1) | int(1) -%}
{%- set wp = no_weight_points | default(1) | int(1) -%}
{%- set h = hours | default(1) | int(1) -%}
{%- set w = weight | default(none) -%}
{%- set w = w | map('int', none) | reject('none') | list
if w is iterable and w is not string
else none
-%}
{%- set w = w + [0] * (h*wp - w | count) if w else none -%}
{# set number of hours based on weight points in case hours setting is not provided #}
{%- set h_wp = (w | count / wp) | round(0, 'ceil') | int if w is not none else h -%}
{%- set h = h if hours is defined else h_wp -%}
{# set weight points based on number of hours (either add zeros, or remove unneeded part) #}
{%- if w is not none -%}
{%- if h_wp <= h -%}
{%- set w = w + [0] * (h*wp - w | count) -%}
{%- else -%}
{%- set w = w[:h*wp] %}
{%- endif -%}
{%- endif -%}
{# Check if data is available and mode is correct#}
{%- if not today and not tomorrow -%}
No valid data in selected sensor
Expand Down

0 comments on commit eca2af3

Please sign in to comment.