Skip to content

Commit b5c4a17

Browse files
committed
blueprint: Added blueprint which combines Octopus Energy rates and Carbon Intensity (1 hour dev time)
1 parent a2472d4 commit b5c4a17

File tree

4 files changed

+225
-32
lines changed

4 files changed

+225
-32
lines changed

_docs/blueprints.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ This blueprint will provide the data source for the UK Carbon Intensity as provi
2222

2323
This blueprint will provide the data source for Octopus Energy rates as provided by the [Octopus Energy](https://github.com/BottlecapDave/HomeAssistant-OctopusEnergy) integration.
2424

25+
!!! warning
26+
27+
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.
28+
29+
### Octopus Energy and Carbon Intensity
30+
31+
[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)
32+
33+
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.
34+
2535
!!! warning
2636

2737
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: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
blueprint:
22
name: Target Timeframes - Carbon Intensity source
3-
description: Configures a targe timeframe data source from Carbon Intensity
3+
description: Configures a target timeframe data source from Carbon Intensity
44
domain: automation
55
author: BottlecapDave
66
input:
@@ -51,17 +51,17 @@ action:
5151
data: >
5252
{% set all_rates = [] %}
5353
{% if state_attr(carbon_intensity_current_day_rates, 'rates') != None %}
54-
{% set all_rates = all_rates + state_attr(carbon_intensity_current_day_rates, 'rates') %}
54+
{% set all_rates = all_rates + state_attr(carbon_intensity_current_day_rates, 'rates') %}
5555
{% endif %}
5656
{% if state_attr(carbon_intensity_next_day_rates, 'rates') != None %}
57-
{% set all_rates = all_rates + state_attr(carbon_intensity_next_day_rates, 'rates') %}
57+
{% set all_rates = all_rates + state_attr(carbon_intensity_next_day_rates, 'rates') %}
5858
{% endif %}
5959
{% set data = namespace(new_rates=[]) %}
6060
{% 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"] } }] %}
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"] } }] %}
6565
{% endfor %}
6666
{{ { 'data': data.new_rates } | to_json }}
6767
target:

_docs/blueprints/target_timeframes_octopus_energy.yaml

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
blueprint:
22
name: Target Timeframes - Octopus Energy source
3-
description: Configures a targe timeframe data source from Octopus Energy
3+
description: Configures a target timeframe data source from Octopus Energy
44
domain: automation
55
author: BottlecapDave
66
input:
@@ -71,51 +71,55 @@ variables:
7171
mode: queued
7272
max: 4
7373
triggers:
74+
- platform: state
75+
entity_id: !input octopus_energy_previous_day_rates
7476
- platform: state
7577
entity_id: !input octopus_energy_current_day_rates
7678
- platform: state
7779
entity_id: !input octopus_energy_next_day_rates
80+
- platform: state
81+
entity_id: !input octopus_energy_free_electricity
7882
condition: []
7983
action:
8084
- action: target_timeframes.update_target_timeframe_data_source
8185
data: >
8286
{% set all_rates = [] %}
8387
{% if state_attr(octopus_energy_previous_day_rates, 'rates') != None %}
84-
{% set all_rates = all_rates + state_attr(octopus_energy_previous_day_rates, 'rates') %}
88+
{% set all_rates = all_rates + state_attr(octopus_energy_previous_day_rates, 'rates') %}
8589
{% endif %}
8690
{% if state_attr(octopus_energy_current_day_rates, 'rates') != None %}
87-
{% set all_rates = all_rates + state_attr(octopus_energy_current_day_rates, 'rates') %}
91+
{% set all_rates = all_rates + state_attr(octopus_energy_current_day_rates, 'rates') %}
8892
{% endif %}
8993
{% if state_attr(octopus_energy_next_day_rates, 'rates') != None %}
90-
{% set all_rates = all_rates + state_attr(octopus_energy_next_day_rates, 'rates') %}
94+
{% set all_rates = all_rates + state_attr(octopus_energy_next_day_rates, 'rates') %}
9195
{% endif %}
9296
{% set free_electricity_rates = [] %}
93-
{# {% if state_attr(octopus_energy_free_electricity, 'rates') != None %}
94-
{% set free_electricity_rates = state_attr(octopus_energy_free_electricity, 'events') %}
95-
{% endif %} #}
97+
{% if state_attr(octopus_energy_free_electricity, 'rates') != None %}
98+
{% set free_electricity_rates = state_attr(octopus_energy_free_electricity, 'events') %}
99+
{% endif %}
96100
{% set data = namespace(new_rates=[]) %}
97101
{% for rate in all_rates %}
98-
{% set start = rate["start"] | as_timestamp | timestamp_utc %}
99-
{% set end = rate["end"] | as_timestamp | timestamp_utc %}
100-
{% set value = rate["value_inc_vat"] | float %}
101-
{% set free_namespace = namespace(is_free=False) %}
102-
{% for free_session in free_electricity_rates %}
103-
{% set free_start = free_session["start"] | as_timestamp | timestamp_utc %}
104-
{% set free_end = free_session["end"] | as_timestamp | timestamp_utc %}
105-
{% if free_start <= start and free_end > start %}
106-
{% set free_namespace.is_free = True %}
107-
{% endif %}
108-
{% endfor %}
102+
{% set start = rate["start"] | as_timestamp | timestamp_utc %}
103+
{% set end = rate["end"] | as_timestamp | timestamp_utc %}
104+
{% set value = rate["value_inc_vat"] | float %}
105+
{% set free_namespace = namespace(is_free=False) %}
106+
{% for free_session in free_electricity_rates %}
107+
{% set free_start = free_session["start"] | as_timestamp | timestamp_utc %}
108+
{% set free_end = free_session["end"] | as_timestamp | timestamp_utc %}
109+
{% if free_start <= start and free_end > start %}
110+
{% set free_namespace.is_free = True %}
111+
{% endif %}
112+
{% endfor %}
109113
110-
{% set metadata = { "is_capped": rate["is_capped"] } %}
111-
{% if free_namespace.is_free %}
112-
{% set value = value * octopus_energy_free_electricity_weighting %}
113-
{% set metadata = dict(metadata.items(), rate=rate["value_inc_vat"], is_free_session=free_namespace.is_free) %}
114-
{% endif %}
114+
{% set metadata = { "is_capped": rate["is_capped"] } %}
115+
{% if free_namespace.is_free %}
116+
{% set value = value * octopus_energy_free_electricity_weighting %}
117+
{% set metadata = dict(metadata.items(), rate=rate["value_inc_vat"], is_free_session=free_namespace.is_free) %}
118+
{% endif %}
115119
116-
{% set new_rate = [{ 'start': start , 'end': end, 'value': value, 'metadata': metadata }] %}
120+
{% set new_rate = [{ 'start': start , 'end': end, 'value': value, 'metadata': metadata }] %}
117121
118-
{% set data.new_rates = data.new_rates + new_rate %}
122+
{% set data.new_rates = data.new_rates + new_rate %}
119123
{% endfor %}
120124
{{ { 'data': data.new_rates } }}
121125
target:
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
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
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
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
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
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 events
59+
description: The free electricity event sensor supplied by Octopus Energy
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
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
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+
{% if carbon_intensity_namespace.rate %}
160+
{% set value = value * (carbon_intensity_namespace.rate["intensity_forecast"] | float) %}
161+
{% endif %}
162+
163+
{% set metadata = { "rate": rate["value_inc_vat"], "is_capped": rate["is_capped"] } %}
164+
{% if free_namespace.is_free %}
165+
{% set value = value * octopus_energy_free_electricity_weighting %}
166+
{% set metadata = dict(metadata.items(), is_free_session=free_namespace.is_free) %}
167+
{% endif %}
168+
169+
{% if carbon_intensity_namespace.rate %}
170+
{% set metadata = dict(metadata.items(), generation_mix=carbon_intensity_namespace.rate["generation_mix"]) %}
171+
{% endif %}
172+
173+
{% set new_rate = [{ 'start': start , 'end': end, 'value': value, 'metadata': metadata }] %}
174+
175+
{% set data.new_rates = data.new_rates + new_rate %}
176+
{% endfor %}
177+
{{ { 'data': data.new_rates } }}
178+
target:
179+
entity_id: !input target_timeframe_data_source_sensor

0 commit comments

Comments
 (0)