Here is the code to add to the sensor.yaml file - you just need to edit the Region, MPRN,MPAN and serials:
## octopus go rest template ##
- platform: rest
name: Go rates today
resource_template: >-
{% set region = 'H' %}
{% set from = as_timestamp(as_timestamp(now())|timestamp_custom('%Y-%m-%d') + 'T00:00:00') %}
{% set to = from + 24 * 60 * 60 %}
{% set format = '%Y-%m-%dT%H:%M:%SZ' %}
https://api.octopus.energy/v1/products/GO-21-05-13/electricity-tariffs/E-1R-GO-21-05-13-{{ region }}/standard-unit-rates/?period_from={{ from|timestamp_custom('%Y-%m-%dT%H:%M:%SZ', False) }}&period_to={{ to|timestamp_custom('%Y-%m-%dT%H:%M:%SZ', False) }}
value_template: >-
{% set from = ((as_timestamp(utcnow()) / 1800)|round(0, 'floor') | int * 1800)|timestamp_custom('%Y-%m-%dT%H:%M:%SZ', False) %}
{% if value_json.results is defined and value_json.results|count > 0 %}
{{ as_timestamp(value_json.results[-1].valid_from)|timestamp_custom('%Y-%m-%d') }}
{% else %}
Unknown
{% endif %}
json_attributes:
- "results"
- platform: rest
name: Go rates off peak
resource_template: >-
{% set region = 'H' %}
{% set from = as_timestamp(as_timestamp(now())|timestamp_custom('%Y-%m-%d') + 'T00:30:00') %}
{% set to = from + 4 %}
{% set format = '%Y-%m-%dT%H:%M:%SZ' %}
https://api.octopus.energy/v1/products/GO-21-05-13/electricity-tariffs/E-1R-GO-21-05-13-{{ region }}/standard-unit-rates/?period_from={{ from|timestamp_custom('%Y-%m-%dT%H:%M:%SZ', False) }}&period_to={{ to|timestamp_custom('%Y-%m-%dT%H:%M:%SZ', False) }}
value_template: >-
{% set from = ((as_timestamp(utcnow()) / 1800)|round(0, 'floor') | int * 1800)|timestamp_custom('%Y-%m-%dT%H:%M:%SZ', False) %}
{% if value_json.results is defined and value_json.results|count > 0 %}
{{ as_timestamp(value_json.results[-1].valid_from)|timestamp_custom('%Y-%m-%d') }}
{% else %}
Unknown
{% endif %}
json_attributes:
- "results"
- platform: template
sensors:
go_current_rate:
friendly_name: Go current rate
icon_template: mdi:flash
value_template: >-
{% set from = ((as_timestamp(utcnow()) / 1800)|round(0, 'floor') | int * 1800)|timestamp_custom('%Y-%m-%dT%H:%M:%SZ', False) %}
{{ ((state_attr('sensor.go_rates_today', 'results')|selectattr('valid_from', 'eq', from)|first).value_inc_vat)|round(2) }}
unit_of_measurement: 'p/kWh'
attribute_templates:
valid_from: >-
{% set from = ((as_timestamp(utcnow()) / 1800)|round(0, 'floor') | int * 1800)|timestamp_custom('%Y-%m-%dT%H:%M:%SZ', False) %}
{{ (state_attr('sensor.go_rates_today', 'results')|selectattr('valid_from', 'eq', from)|first).valid_from }}
valid_to: >-
{% set from = ((as_timestamp(utcnow()) / 1800)|round(0, 'floor')|int * 1800)|timestamp_custom('%Y-%m-%dT%H:%M:%SZ', False) %}
{{ (state_attr('sensor.go_rates_today', 'results')|selectattr('valid_from', 'eq', from)|first).valid_to }}
- platform: rest
name: Gas consumption
resource_template: >-
{% set mprn = '00000000000' %}
{% set serial = 'ABC000000000' %}
{% set date = as_timestamp(now() - timedelta(days = 1))|timestamp_custom('%Y-%m-%d') %}
https://api.octopus.energy/v1/gas-meter-points/{{ mprn }}/meters/{{ serial }}/consumption/?period_from={{ date + 'T00:00:00' }}&period_to={{ date + 'T23:59:59' }}
headers:
Authorization: !secret octopus_auth
value_template: "{{ value_json.results|sum(attribute='consumption')|round(3) }}"
unit_of_measurement: 'kWh'
device_class: energy
json_attributes:
- "results"
- platform: template
sensors:
gas_cost:
friendly_name: Gas cost
icon_template: mdi:cash-multiple
value_template: >-
{% set unit_price = 3.95 %}
{% set standing_charge = 23.85 %}
{% set calorific_value = 39.1 %}
{% set usage = state_attr('sensor.gas_consumption', 'results') %}
{% if usage is defined and usage|count == 48 %}
{{ ((states('sensor.gas_consumption') | float * 1.02264 * calorific_value / 3.6) * unit_price / 100 + standing_charge / 100)|round(2) }}
{% else %}
Unknown
{% endif %}
device_class: energy
unit_of_measurement: '£'
- platform: rest
name: Electricity consumption
resource_template: >-
{% set mpan = '0000000000000' %}
{% set serial = '00A000000' %}
{% set date = as_timestamp(now() - timedelta(days = 1))|timestamp_custom('%Y-%m-%d') %}
https://api.octopus.energy/v1/electricity-meter-points/{{ mpan }}/meters/{{ serial }}/consumption/?period_from={{ date + 'T00:00:00' }}&period_to={{ date + 'T23:59:59' }}
headers:
Authorization: !secret octopus_auth
value_template: "{{ value_json.results|sum(attribute='consumption')|round(3) }}"
unit_of_measurement: 'kWh'
device_class: energy
json_attributes:
- "results"
- platform: rest
name: Electricity consumption midnight
resource_template: >-
{% set mpan = '0000000000000' %}
{% set serial = '00A000000' %}
{% set date = as_timestamp(now() - timedelta(days = 1))|timestamp_custom('%Y-%m-%d') %}
https://api.octopus.energy/v1/electricity-meter-points/{{ mpan }}/meters/{{ serial }}/consumption/?period_from={{ date + 'T00:00:00' }}&period_to={{ date + 'T00:29:59' }}
headers:
Authorization: !secret octopus_auth
value_template: "{{ value_json.results|sum(attribute='consumption')|round(3) }}"
unit_of_measurement: 'kWh'
device_class: energy
json_attributes:
- "results"
- platform: rest
name: Electricity consumption off peak
resource_template: >-
{% set mpan = '0000000000000' %}
{% set serial = '00A000000' %}
{% set date = as_timestamp(now() - timedelta(days = 1))|timestamp_custom('%Y-%m-%d') %}
https://api.octopus.energy/v1/electricity-meter-points/{{ mpan }}/meters/{{ serial }}/consumption/?period_from={{ date + 'T00:30:00' }}&period_to={{ date + 'T04:29:59' }}
headers:
Authorization: !secret octopus_auth
value_template: "{{ value_json.results|sum(attribute='consumption')|round(3) }}"
unit_of_measurement: 'kWh'
device_class: energy
json_attributes:
- "results"
- platform: rest
name: Electricity consumption graveyard
resource_template: >-
{% set mpan = '0000000000000' %}
{% set serial = '00A000000' %}
{% set date = as_timestamp(now() - timedelta(days = 1))|timestamp_custom('%Y-%m-%d') %}
https://api.octopus.energy/v1/electricity-meter-points/{{ mpan }}/meters/{{ serial }}/consumption/?period_from={{ date + 'T04:30:00' }}&period_to={{ date + 'T08:29:59' }}
headers:
Authorization: !secret octopus_auth
value_template: "{{ value_json.results|sum(attribute='consumption')|round(3) }}"
unit_of_measurement: 'kWh'
device_class: energy
json_attributes:
- "results"
- platform: rest
name: Electricity consumption morning
resource_template: >-
{% set mpan = '0000000000000' %}
{% set serial = '00A000000' %}
{% set date = as_timestamp(now() - timedelta(days = 1))|timestamp_custom('%Y-%m-%d') %}
https://api.octopus.energy/v1/electricity-meter-points/{{ mpan }}/meters/{{ serial }}/consumption/?period_from={{ date + 'T08:30:00' }}&period_to={{ date + 'T12:29:59' }}
headers:
Authorization: !secret octopus_auth
value_template: "{{ value_json.results|sum(attribute='consumption')|round(3) }}"
unit_of_measurement: 'kWh'
device_class: energy
json_attributes:
- "results"
- platform: rest
name: Electricity consumption afternoon
resource_template: >-
{% set mpan = '0000000000000' %}
{% set serial = '00A000000' %}
{% set date = as_timestamp(now() - timedelta(days = 1))|timestamp_custom('%Y-%m-%d') %}
https://api.octopus.energy/v1/electricity-meter-points/{{ mpan }}/meters/{{ serial }}/consumption/?period_from={{ date + 'T12:30:00' }}&period_to={{ date + 'T16:29:59' }}
headers:
Authorization: !secret octopus_auth
value_template: "{{ value_json.results|sum(attribute='consumption')|round(3) }}"
unit_of_measurement: 'kWh'
device_class: energy
json_attributes:
- "results"
- platform: rest
name: Electricity consumption evening
resource_template: >-
{% set mpan = '0000000000000' %}
{% set serial = '00A000000' %}
{% set date = as_timestamp(now() - timedelta(days = 1))|timestamp_custom('%Y-%m-%d') %}
https://api.octopus.energy/v1/electricity-meter-points/{{ mpan }}/meters/{{ serial }}/consumption/?period_from={{ date + 'T16:30:00' }}&period_to={{ date + 'T20:29:59' }}
headers:
Authorization: !secret octopus_auth
value_template: "{{ value_json.results|sum(attribute='consumption')|round(3) }}"
unit_of_measurement: 'kWh'
device_class: energy
json_attributes:
- "results"
- platform: rest
name: Electricity consumption night
resource_template: >-
{% set mpan = '0000000000000' %}
{% set serial = '00A000000' %}
{% set date = as_timestamp(now() - timedelta(days = 1))|timestamp_custom('%Y-%m-%d') %}
https://api.octopus.energy/v1/electricity-meter-points/{{ mpan }}/meters/{{ serial }}/consumption/?period_from={{ date + 'T20:30:00' }}&period_to={{ date + 'T23:59:59' }}
headers:
Authorization: !secret octopus_auth
value_template: "{{ value_json.results|sum(attribute='consumption')|round(3) }}"
unit_of_measurement: 'kWh'
device_class: energy
json_attributes:
- "results"
- platform: template
sensors:
electricity_cost:
friendly_name: Electricity cost
icon_template: mdi:cash-multiple
value_template: >-
{% set standing_charge = 25 %}
{% set usage = state_attr('sensor.electricity_consumption', 'results') %}
{% if usage is defined and usage|count == 48 %}
{% set ns = namespace(total=0) %}
{% for p in range(0, 48) %}
{% set time = as_timestamp(usage[p].interval_start)|timestamp_custom('%H:%M:%S') %}
{% set unit_price = 15.59 %}
{% if '00:30:00' <= time < '04:30:00' %}
{% set unit_price = 5 %}
{% endif %}
{% set ns.total = ns.total + (usage[p].consumption * unit_price / 100) %}
{% endfor %}
{{ (ns.total + standing_charge / 100)|round(2) }}
{% else %}
Unknown
{% endif %}
unit_of_measurement: '£'
device_class: monetary
- platform: template
sensors:
electricity_cost_off_peak:
friendly_name: Electricity cost off peak
icon_template: mdi:cash-multiple
value_template: >-
{% set standing_charge = 0 %}
{% set usage = state_attr('sensor.electricity_consumption_off_peak', 'results') %}
{% if usage is defined and usage|count == 8 %}
{% set ns = namespace(total=0) %}
{% for p in range(0, 8) %}
{% set time = as_timestamp(usage[p].interval_start)|timestamp_custom('%H:%M:%S') %}
{% set unit_price = 5 %}
{% set ns.total = ns.total + (usage[p].consumption * unit_price / 100) %}
{% endfor %}
{{ (ns.total + standing_charge / 100)|round(2) }}
{% else %}
Unknown
{% endif %}
unit_of_measurement: '£'
device_class: monetary
Then to get these working with “Energy” you will need to add the following to your customize.yaml file:
sensor.gas_cost:
unit_of_measurement: GBP
device_class: monetary
state_class: total
last_reset: '1970-01-01T00:00:00+00:00'
sensor.gas_consumption:
unit_of_measurement: kWh
device_class: energy
state_class: total_increasing
last_reset: '1970-01-01T00:00:00+00:00'
sensor.electricity_consumption_night:
unit_of_measurement: kWh
device_class: energy
state_class: total_increasing
last_reset: '1970-01-01T00:00:00+00:00'
sensor.electricity_consumption_evening:
unit_of_measurement: kWh
device_class: energy
state_class: total_increasing
last_reset: '1970-01-01T00:00:00+00:00'
sensor.electricity_consumption_afternoon:
unit_of_measurement: kWh
device_class: energy
state_class: total_increasing
last_reset: '1970-01-01T00:00:00+00:00'
sensor.electricity_consumption_morning:
unit_of_measurement: kWh
device_class: energy
state_class: total_increasing
last_reset: '1970-01-01T00:00:00+00:00'
sensor.electricity_consumption_graveyard:
unit_of_measurement: kWh
device_class: energy
state_class: total_increasing
last_reset: '1970-01-01T00:00:00+00:00'
sensor.electricity_consumption_off_peak:
unit_of_measurement: kWh
device_class: energy
state_class: total_increasing
last_reset: '1970-01-01T00:00:00+00:00'
sensor.electricity_consumption_midnight:
unit_of_measurement: kWh
device_class: energy
state_class: total_increasing
last_reset: '1970-01-01T00:00:00+00:00'
sensor.electricity_cost_off_peak:
unit_of_measurement: GBP
device_class: monetary
state_class: total
last_reset: '1970-01-01T00:00:00+00:00'
For the pricing I only use the energy sensor for offpeak to apply the daily charges:
For the rest I just use the static values:
Finally you’ll need to add the octopus auth to the secrets.yaml file:
octopus_auth: 'Basic base64encodedapikey'
Note: you can simply use the following line in a linux terminal to obtain your base64 encoded api key without using any online utility:
echo 'your_api_key' | base64
Then you can simply calculate your daily usage using the following:
- platform: template
sensors:
total_energy_cost:
friendly_name: "Energy Cost - Total"
value_template: "{{ states('sensor.electricity_cost') | float + states('sensor.electricity_cost_off_peak') | float + states('sensor.gas_cost') | float}}"
unit_of_measurement: '£'
device_class: monetary
- platform: template
sensors:
total_energy_consumption:
friendly_name: "Energy Consumption - Total"
value_template: "{{ states('sensor.electricity_consumption') | float + states('sensor.electricity_consumption_off_peak') | float + states('sensor.gas_consumption') | float}}"
unit_of_measurement: 'kWh'
device_class: energy
The finally add the following to your configuration.yaml file if you don’t have it in there already (thanks to @BookOfGreg for pointing this out):
homeassistant:
customize: !include customize.yaml
sensor: !include sensor.yaml
Hope this helps