Skip to content

Commit

Permalink
🌟 Improve error messages and add parameters (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheFes authored Oct 18, 2023
1 parent e9cb887 commit 8614c35
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ In case `hours` is provided, this will overrule the calculated number of hours,
|---|---|---|---|---|
|`no_weight_points`|integer|`1`|`4`|The number of weight points per hour, eg set to `4` if each weight point represents 15 minutes. This should match with the datapoints per hour, meaning the number of minutes each list item in the sensor represents (normally 60, for dynamic prices per hour) shoudl be divisable by the number of minutes per weight point. So with hourly data you can use `4` or `12` but not `7`|
|`weight`|list|`none`|`[25, 1, 4, 0]`|The list with weight factors to be used for the calculation|
|`program`|string|none|`"Dryer Clothes"`| Description of data used in the energy plot sensor. Adds automatically the correct weight and number of weight points.
|`program`|string|none|`"Dryer Clothes"`|Description of data used in the energy plot sensor. Adds automatically the correct weight and number of weight points.|
|`plot_sensor`|string|`"sensor.energy_plots"`|`"sensor.plot_data"`| The entity_id of the sensor with the energy plots.|
|`plot_attr`|string|`"energy_plots"`|`"plot_data"`|The attribute in which the enery plots are stored.|

To help getting the weight data from your device, I created a script and a template sensor which stores the data. The script requires a energy sensor for the device you want to track, and some entity to determine when to stop plotting the data (this can be an input_boolean, or the state of the device iteself)
You can start the script manually, or automate it. The data will be stored in a template sensor called `sensor.energy_plots`.
Expand Down
27 changes: 15 additions & 12 deletions cheapest_energy_hours.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
look_ahead=false,
no_weight_points=1,
weight=none,
program=none
program=none,
plot_sensor='sensor.energy_plots'
plot_attr='energy_plots'
) -%}
{%- set modes = ['min', 'max', 'average', 'start', 'end', 'list', 'weighted_average','time_min','time_max', 'split'] -%}
{# Get data out of the selected entity #}
Expand All @@ -35,8 +37,9 @@
{%- set use_voe = value_on_error is defined -%}
{# set weight points based on energy plot sensor #}
{%-if program is not none-%}
{%- set weight = state_attr('sensor.energy_plots', 'energy_plots').get(program, {}).get('data') | default(none, true) -%}
{%- set no_weight_points = state_attr('sensor.energy_plots', 'energy_plots').get(program, {}).get('no_weight_points', 1) -%}
{%- set plot_data = state_attr(plot_sensor, plot_attr) | default({}, true) -%}
{%- set weight = plot_data.get(program, {}).get('data') | default(none, true) -%}
{%- set no_weight_points = plot_data.get(program, {}).get('no_weight_points', 1) -%}
{%- endif -%}
{%- set w = weight | map('float', none) | reject('none') | list
if weight is iterable and weight is not string
Expand All @@ -55,15 +58,15 @@
{%- endif -%}
{# Return error messages for input #}
{%- set errors = [
'No valid data in selected sensor' if not today and not tomorrow,
'Time key not found in data' if today is not none and time_key not in today[0],
'Value key not found in data' if today is not none and value_key not in today[0],
'Invalid mode selected' if mode not in modes,
'Boolean input expected for include_today' if include_today | bool('') is not boolean,
'Boolean input expected for include_tomorrow' if include_tomorrow | bool('') is not boolean,
'Boolean input expected for look_ahead' if look_ahead | bool('') is not boolean,
'Boolean input expected for lowest' if lowest | bool('') is not boolean,
'Selected program is not available or has no data' if program is not none and w is none
'No valid data in {}'.format(sensor) if not today and not tomorrow,
'Time key "{}" not found in data'.format(time_key) if today is not none and time_key not in today[0],
'Value key "{}" not found in data'.format(value_key) if today is not none and value_key not in today[0],
'Invalid mode "{}" selected'.format(mode) if mode not in modes,
'Boolean input expected for include_today, "{}" can not be processed as a boolean'.format(include_today) if include_today | bool('') is not boolean,
'Boolean input expected for include_tomorrow, "{}" can not be processed as a boolean'.format(include_tomorrow) if include_tomorrow | bool('') is not boolean,
'Boolean input expected for look_ahead, "{}" can not be processed as a boolean'.format(look_ahead) if look_ahead | bool('') is not boolean,
'Boolean input expected for lowest, "{}" can not be processed as a boolean'.format(lowest) if lowest | bool('') is not boolean,
'Selected program "{}" is not available or has no data'.format(program) if program is not none and w is none
] | select() | list
-%}
{%- if errors | count > 0 -%}
Expand Down

0 comments on commit 8614c35

Please sign in to comment.