|
| 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