Skip to content

Commit 0d64015

Browse files
Merge pull request #11 from BottlecapDave/develop
New Release
2 parents 1c18678 + b778dda commit 0d64015

13 files changed

+409
-43
lines changed

_docs/blueprints.md

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,36 @@
44

55
## Data Sources
66

7-
The following blueprints can help you configure data sources provided by other integrations
7+
The following blueprints can help you use data from other integrations as the source of your target timeframes.
88

99
### Carbon Intensity
1010

11-
[Install blueprint](https://my.home-assistant.io/redirect/blueprint_import/?blueprint_url=https%3A%2F%2Fgithub.com%2FBottlecapDave%2FHomeAssistant-OctopusEnergy%2Fblob%2Fmain%2F_docs%2Fblueprints%target_timeframes_carbon_intensity.yaml) | [Source](./blueprints/target_timeframes_carbon_intensity.yaml)
11+
[Install blueprint](https://my.home-assistant.io/redirect/blueprint_import/?blueprint_url=https%3A%2F%2Fbottlecapdave.github.io%2FHomeAssistant-TargetTimeframes%2Fblueprints%2Ftarget_timeframes_carbon_intensity.yaml) | [Source](./blueprints/target_timeframes_carbon_intensity.yaml)
1212

1313
This blueprint will provide the data source for the UK Carbon Intensity as provided by the [Carbon Intensity](https://github.com/BottlecapDave/HomeAssistant-CarbonIntensity) integration.
1414

15+
!!! warning
16+
17+
This automation will run when any of the underlying entities update. This make take a while initially. If you want the data available immediately, then you'll need to run the automation manually.
18+
1519
### Octopus Energy
1620

17-
[Install blueprint](https://my.home-assistant.io/redirect/blueprint_import/?blueprint_url=https%3A%2F%2Fgithub.com%2FBottlecapDave%2FHomeAssistant-OctopusEnergy%2Fblob%2Fmain%2F_docs%2Fblueprints%target_timeframes_octopus_energy.yaml) | [Source](./blueprints/target_timeframes_octopus_energy.yaml)
21+
[Install blueprint (No Free Electricity)](https://my.home-assistant.io/redirect/blueprint_import/?blueprint_url=https%3A%2F%2Fbottlecapdave.github.io%2FHomeAssistant-TargetTimeframes%2Fblueprints%2Ftarget_timeframes_octopus_energy.yaml) | [Source](./blueprints/target_timeframes_octopus_energy.yaml)
22+
23+
[Install blueprint (Free Electricity Included)](https://my.home-assistant.io/redirect/blueprint_import/?blueprint_url=https%3A%2F%2Fbottlecapdave.github.io%2FHomeAssistant-TargetTimeframes%2Fblueprints%2Ftarget_timeframes_octopus_energy.yaml) | [Source](./blueprints/target_timeframes_octopus_energy.yaml)
24+
25+
This blueprint will provide the data source for Octopus Energy rates as provided by the [Octopus Energy](https://github.com/BottlecapDave/HomeAssistant-OctopusEnergy) integration.
26+
27+
!!! warning
28+
29+
This automation will run when any of the underlying entities update. This make take a while initially. If you want the data available immediately, then you'll need to run the automation manually.
30+
31+
### Octopus Energy and Carbon Intensity
32+
33+
[Install blueprint](https://my.home-assistant.io/redirect/blueprint_import/?blueprint_url=https%3A%2F%2Fbottlecapdave.github.io%2FHomeAssistant-TargetTimeframes%2Fblueprints%2Ftarget_timeframes_octopus_energy_carbon_intensity.yaml) | [Source](./blueprints/target_timeframes_octopus_energy_carbon_intensity.yaml)
34+
35+
This blueprint will provide the data source for Octopus Energy rates as provided by the [Octopus Energy](https://github.com/BottlecapDave/HomeAssistant-OctopusEnergy) integration and UK Carbon Intensity as provided by the [Carbon Intensity](https://github.com/BottlecapDave/HomeAssistant-CarbonIntensity) integration. It will multiply the OE rate by the carbon intensity, resulting in target timeframes favouring periods with low rates, and then falling back to carbon intensity where rates are the same.
36+
37+
!!! warning
1838

19-
This blueprint will provide the data source for Octopus Energy rates as provided by the [Octopus Energy](https://github.com/BottlecapDave/HomeAssistant-OctopusEnergy) integration.
39+
This automation will run when any of the underlying entities update. This make take a while initially. If you want the data available immediately, then you'll need to run the automation manually.

_docs/blueprints/target_timeframes_carbon_intensity.yaml

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
blueprint:
2-
name: Target Timeframess - Carbon Intensity source
3-
description: Configures a targe timeframe data source from Carbon Intensity
2+
name: Target Timeframes - Carbon Intensity source
3+
description: Configures a target timeframe data source from Carbon Intensity
44
domain: automation
55
author: BottlecapDave
66
input:
@@ -16,7 +16,7 @@ blueprint:
1616
multiple: false
1717
carbon_intensity_current_day_rates:
1818
name: Current day rates
19-
description: The current day rates event sensor supplied by Carbon Intensity
19+
description: The current day rates event sensor supplied by Carbon Intensity. More information can be found at https://bottlecapdave.github.io/HomeAssistant-CarbonIntensity/entities/#current-day-rates.
2020
selector:
2121
entity:
2222
filter:
@@ -26,7 +26,7 @@ blueprint:
2626
multiple: false
2727
carbon_intensity_next_day_rates:
2828
name: Next day rates
29-
description: The next day rates event sensor supplied by Carbon Intensity
29+
description: The next day rates event sensor supplied by Carbon Intensity. More information can be found at https://bottlecapdave.github.io/HomeAssistant-CarbonIntensity/entities/#next-day-rates.
3030
selector:
3131
entity:
3232
filter:
@@ -49,14 +49,20 @@ condition: []
4949
action:
5050
- action: target_timeframes.update_target_timeframe_data_source
5151
data: >
52-
{% set all_rates = state_attr(carbon_intensity_current_day_rates, 'rates') + state_attr(carbon_intensity_next_day_rates, 'rates') %}
53-
{% set data = namespace(new_rates=[]) %}
54-
{% for rate in all_rates %}
55-
{% set start = rate["from"] | as_timestamp | timestamp_utc %}
56-
{% set end = rate["to"] | as_timestamp | timestamp_utc %}
57-
{% set value = rate["intensity_forecast"] | float %}
58-
{% set data.new_rates = data.new_rates + [{ 'start': start , 'end': end, 'value': value, 'metadata': { "generation_mix": rate["generation_mix"] } }] %}
59-
{% endfor %}
52+
{%- set all_rates = [] -%}
53+
{%- if state_attr(carbon_intensity_current_day_rates, 'rates') != None -%}
54+
{%- set all_rates = all_rates + state_attr(carbon_intensity_current_day_rates, 'rates') -%}
55+
{%- endif -%}
56+
{%- if state_attr(carbon_intensity_next_day_rates, 'rates') != None -%}
57+
{%- set all_rates = all_rates + state_attr(carbon_intensity_next_day_rates, 'rates') -%}
58+
{%- endif -%}
59+
{%- set data = namespace(new_rates=[]) -%}
60+
{%- for rate in all_rates -%}
61+
{%- set start = rate["from"] | as_timestamp | timestamp_utc -%}
62+
{%- set end = rate["to"] | as_timestamp | timestamp_utc -%}
63+
{%- set value = rate["intensity_forecast"] | float -%}
64+
{%- set data.new_rates = data.new_rates + [{ 'start': start , 'end': end, 'value': value, 'metadata': { "generation_mix": rate["generation_mix"] } }] -%}
65+
{%- endfor -%}
6066
{{ { 'data': data.new_rates } | to_json }}
6167
target:
6268
entity_id: !input target_timeframe_data_source_sensor

_docs/blueprints/target_timeframes_octopus_energy.yaml

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
blueprint:
2-
name: Target Timeframess - Octopus Energy source
3-
description: Configures a targe timeframe data source from Octopus Energy
2+
name: Target Timeframes - Octopus Energy source
3+
description: Configures a target timeframe data source from Octopus Energy
44
domain: automation
55
author: BottlecapDave
66
input:
@@ -16,7 +16,7 @@ blueprint:
1616
multiple: false
1717
octopus_energy_previous_day_rates:
1818
name: Previous day rates
19-
description: The previous day rates event sensor supplied by Octopus Energy
19+
description: The previous day rates event sensor supplied by Octopus Energy. More information can be found at https://bottlecapdave.github.io/HomeAssistant-OctopusEnergy/entities/electricity/#previous-day-rates.
2020
selector:
2121
entity:
2222
filter:
@@ -26,7 +26,7 @@ blueprint:
2626
multiple: false
2727
octopus_energy_current_day_rates:
2828
name: Current day rates
29-
description: The current day rates event sensor supplied by Octopus Energy
29+
description: The current day rates event sensor supplied by Octopus Energy. More information can be found at https://bottlecapdave.github.io/HomeAssistant-OctopusEnergy/entities/electricity/#current-day-rates.
3030
selector:
3131
entity:
3232
filter:
@@ -36,7 +36,7 @@ blueprint:
3636
multiple: false
3737
octopus_energy_next_day_rates:
3838
name: Next day rates
39-
description: The next day rates event sensor supplied by Octopus Energy
39+
description: The next day rates event sensor supplied by Octopus Energy. More information can be found at https://bottlecapdave.github.io/HomeAssistant-OctopusEnergy/entities/electricity/#current-day-rates.
4040
selector:
4141
entity:
4242
filter:
@@ -52,6 +52,8 @@ variables:
5252
mode: queued
5353
max: 4
5454
triggers:
55+
- platform: state
56+
entity_id: !input octopus_energy_previous_day_rates
5557
- platform: state
5658
entity_id: !input octopus_energy_current_day_rates
5759
- platform: state
@@ -60,14 +62,27 @@ condition: []
6062
action:
6163
- action: target_timeframes.update_target_timeframe_data_source
6264
data: >
63-
{% set all_rates = state_attr(octopus_energy_previous_day_rates, 'rates') + state_attr(octopus_energy_current_day_rates, 'rates') + state_attr(octopus_energy_next_day_rates, 'rates') %}
64-
{% set data = namespace(new_rates=[]) %}
65-
{% for rate in all_rates %}
66-
{% set start = rate["start"] | as_timestamp | timestamp_utc %}
67-
{% set end = rate["end"] | as_timestamp | timestamp_utc %}
68-
{% set value = rate["value"] | float %}
69-
{% set data.new_rates = data.new_rates + [{ 'start': start , 'end': end, 'value': value, 'metadata': { "is_capped": rate["is_capped"] } }] %}
70-
{% endfor %}
65+
{%- set all_rates = [] -%}
66+
{%- if state_attr(octopus_energy_previous_day_rates, 'rates') != None -%}
67+
{%- set all_rates = all_rates + state_attr(octopus_energy_previous_day_rates, 'rates') -%}
68+
{%- endif -%}
69+
{%- if state_attr(octopus_energy_current_day_rates, 'rates') != None -%}
70+
{%- set all_rates = all_rates + state_attr(octopus_energy_current_day_rates, 'rates') -%}
71+
{%- endif -%}
72+
{%- if state_attr(octopus_energy_next_day_rates, 'rates') != None -%}
73+
{%- set all_rates = all_rates + state_attr(octopus_energy_next_day_rates, 'rates') -%}
74+
{%- endif -%}
75+
{%- set data = namespace(new_rates=[]) -%}
76+
{%- for rate in all_rates -%}
77+
{%- set start = rate["start"] | as_timestamp | timestamp_utc -%}
78+
{%- set end = rate["end"] | as_timestamp | timestamp_utc -%}
79+
{%- set value = rate["value_inc_vat"] | float -%}
80+
81+
{%- set metadata = { "is_capped": rate["is_capped"] } -%}
82+
{%- set new_rate = [{ 'start': start , 'end': end, 'value': value, 'metadata': metadata }] -%}
83+
84+
{%- set data.new_rates = data.new_rates + new_rate -%}
85+
{%- endfor -%}
7186
{{ { 'data': data.new_rates } }}
7287
target:
7388
entity_id: !input target_timeframe_data_source_sensor
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
blueprint:
2+
name: Target Timeframes - Octopus Energy and Carbon Intensity source
3+
description: Configures a target timeframe data source that priorities Octopus Energy and then falls back onto Carbon Intensity where the rate is the same
4+
domain: automation
5+
author: BottlecapDave
6+
input:
7+
target_timeframe_data_source_sensor:
8+
name: Target timeframe data source sensor
9+
description: The data source sensor which represents the data source to update
10+
selector:
11+
entity:
12+
filter:
13+
- domain:
14+
- sensor
15+
integration: target_timeframes
16+
multiple: false
17+
octopus_energy_previous_day_rates:
18+
name: Previous day rates
19+
description: The previous day rates event sensor supplied by Octopus Energy. More information can be found at https://bottlecapdave.github.io/HomeAssistant-OctopusEnergy/entities/electricity/#previous-day-rates.
20+
selector:
21+
entity:
22+
filter:
23+
- domain:
24+
- event
25+
integration: octopus_energy
26+
multiple: false
27+
octopus_energy_current_day_rates:
28+
name: Current day rates
29+
description: The current day rates event sensor supplied by Octopus Energy. More information can be found at https://bottlecapdave.github.io/HomeAssistant-OctopusEnergy/entities/electricity/#current-day-rates.
30+
selector:
31+
entity:
32+
filter:
33+
- domain:
34+
- event
35+
integration: octopus_energy
36+
multiple: false
37+
octopus_energy_next_day_rates:
38+
name: Next day rates
39+
description: The next day rates event sensor supplied by Octopus Energy. More information can be found at https://bottlecapdave.github.io/HomeAssistant-OctopusEnergy/entities/electricity/#current-day-rates.
40+
selector:
41+
entity:
42+
filter:
43+
- domain:
44+
- event
45+
integration: octopus_energy
46+
multiple: false
47+
octopus_energy_free_electricity:
48+
name: Free Electricity events
49+
description: The free electricity event sensor supplied by Octopus Energy. More information can be found at https://bottlecapdave.github.io/HomeAssistant-OctopusEnergy/entities/octoplus/#free-electricity-session-events.
50+
selector:
51+
entity:
52+
filter:
53+
- domain:
54+
- event
55+
integration: octopus_energy
56+
multiple: false
57+
octopus_energy_free_electricity_weighting:
58+
name: Free Electricity weighting
59+
description: The weighting to apply to the value if a rate period falls within a free electricity session.
60+
default: 1
61+
selector:
62+
number:
63+
mode: box
64+
carbon_intensity_current_day_rates:
65+
name: Current day rates
66+
description: The current day rates event sensor supplied by Carbon Intensity. More information can be found at https://bottlecapdave.github.io/HomeAssistant-CarbonIntensity/entities/#current-day-rates.
67+
selector:
68+
entity:
69+
filter:
70+
- domain:
71+
- event
72+
integration: carbon_intensity
73+
multiple: false
74+
carbon_intensity_next_day_rates:
75+
name: Next day rates
76+
description: The next day rates event sensor supplied by Carbon Intensity. More information can be found at https://bottlecapdave.github.io/HomeAssistant-CarbonIntensity/entities/#next-day-rates.
77+
selector:
78+
entity:
79+
filter:
80+
- domain:
81+
- event
82+
integration: carbon_intensity
83+
multiple: false
84+
variables:
85+
target_timeframe_data_source_sensor: !input target_timeframe_data_source_sensor
86+
octopus_energy_previous_day_rates: !input octopus_energy_previous_day_rates
87+
octopus_energy_current_day_rates: !input octopus_energy_current_day_rates
88+
octopus_energy_next_day_rates: !input octopus_energy_next_day_rates
89+
octopus_energy_free_electricity: !input octopus_energy_free_electricity
90+
octopus_energy_free_electricity_weighting: !input octopus_energy_free_electricity_weighting
91+
carbon_intensity_current_day_rates: !input carbon_intensity_current_day_rates
92+
carbon_intensity_next_day_rates: !input carbon_intensity_next_day_rates
93+
mode: queued
94+
max: 4
95+
triggers:
96+
- platform: state
97+
entity_id: !input octopus_energy_previous_day_rates
98+
- platform: state
99+
entity_id: !input octopus_energy_current_day_rates
100+
- platform: state
101+
entity_id: !input octopus_energy_next_day_rates
102+
- platform: state
103+
entity_id: !input octopus_energy_free_electricity
104+
- platform: state
105+
entity_id: !input carbon_intensity_current_day_rates
106+
- platform: state
107+
entity_id: !input carbon_intensity_next_day_rates
108+
condition: []
109+
action:
110+
- action: target_timeframes.update_target_timeframe_data_source
111+
data: >
112+
{%- set all_oe_rates = [] -%}
113+
{%- if state_attr(octopus_energy_previous_day_rates, 'rates') != None -%}
114+
{%- set all_oe_rates = all_oe_rates + state_attr(octopus_energy_previous_day_rates, 'rates') -%}
115+
{%- endif -%}
116+
{%- if state_attr(octopus_energy_current_day_rates, 'rates') != None -%}
117+
{%- set all_oe_rates = all_oe_rates + state_attr(octopus_energy_current_day_rates, 'rates') -%}
118+
{%- endif -%}
119+
{%- if state_attr(octopus_energy_next_day_rates, 'rates') != None -%}
120+
{%- set all_oe_rates = all_oe_rates + state_attr(octopus_energy_next_day_rates, 'rates') -%}
121+
{%- endif -%}
122+
{%- set free_electricity_rates = [] -%}
123+
{%- if state_attr(octopus_energy_free_electricity, 'rates') != None -%}
124+
{%- set free_electricity_rates = state_attr(octopus_energy_free_electricity, 'events') -%}
125+
{%- endif -%}
126+
127+
{%- set all_ci_rates = [] -%}
128+
{%- if state_attr(carbon_intensity_current_day_rates, 'rates') != None -%}
129+
{%- set all_ci_rates = all_ci_rates + state_attr(carbon_intensity_current_day_rates, 'rates') -%}
130+
{%- endif -%}
131+
{%- if state_attr(carbon_intensity_next_day_rates, 'rates') != None -%}
132+
{%- set all_ci_rates = all_ci_rates + state_attr(carbon_intensity_next_day_rates, 'rates') -%}
133+
{%- endif -%}
134+
135+
{%- set data = namespace(new_rates=[]) -%}
136+
{%- for rate in all_oe_rates -%}
137+
{%- set start = rate["start"] | as_timestamp | timestamp_utc -%}
138+
{%- set end = rate["end"] | as_timestamp | timestamp_utc -%}
139+
{%- set value = rate["value_inc_vat"] | float -%}
140+
141+
{%- set free_namespace = namespace(is_free=False) -%}
142+
{%- for free_session in free_electricity_rates -%}
143+
{%- set free_start = free_session["start"] | as_timestamp | timestamp_utc -%}
144+
{%- set free_end = free_session["end"] | as_timestamp | timestamp_utc -%}
145+
{%- if free_start <= start and free_end > start -%}
146+
{%- set free_namespace.is_free = True -%}
147+
{%- endif -%}
148+
{%- endfor -%}
149+
150+
{%- set carbon_intensity_namespace = namespace(rate=None) -%}
151+
{%- for carbon_intensity_rate in all_ci_rates -%}
152+
{%- set carbon_intensity_start = carbon_intensity_rate["from"] | as_timestamp | timestamp_utc -%}
153+
{%- set carbon_intensity_end = carbon_intensity_rate["to"] | as_timestamp | timestamp_utc -%}
154+
{%- if carbon_intensity_start == start and carbon_intensity_end == end -%}
155+
{%- set carbon_intensity_namespace.rate = carbon_intensity_rate -%}
156+
{%- endif -%}
157+
{%- endfor -%}
158+
159+
{%- set metadata = { "rate": rate["value_inc_vat"], "is_capped": rate["is_capped"] } -%}
160+
{%- if carbon_intensity_namespace.rate -%}
161+
{%- set value = value * (carbon_intensity_namespace.rate["intensity_forecast"] | float) -%}
162+
{%- set metadata = dict(metadata.items(), carbon_intensity=carbon_intensity_namespace.rate["intensity_forecast"] | float) -%}
163+
{%- endif -%}
164+
165+
{%- if free_namespace.is_free -%}
166+
{%- set value = value * octopus_energy_free_electricity_weighting -%}
167+
{%- set metadata = dict(metadata.items(), is_free_session=free_namespace.is_free) -%}
168+
{%- endif -%}
169+
170+
{%- set new_rate = [{ 'start': start , 'end': end, 'value': value, 'metadata': metadata }] -%}
171+
172+
{%- set data.new_rates = data.new_rates + new_rate -%}
173+
{%- endfor -%}
174+
{{ { 'data': data.new_rates } }}
175+
target:
176+
entity_id: !input target_timeframe_data_source_sensor

0 commit comments

Comments
 (0)