Thursday, October 14, 2021

Using Octopus Energy data in Home Assistant

We have a SMETS2 smart energy meter installed at our property. Energy consumption data is updated at a central location(DCC) using a mobile network. This data is then accessed by my energy provider the next day and my energy consumption is calculated.

My current energy provider is Octopus Energy who kindly provide an API to fetch your power consumption data they obtain through the smart-meter and DCC. I have been looking forward to using this data to download this consumption data into Home Assistant.

There are several ways in which this is done. The instructions below describe how I access the data from my account at Octopus Energy.

Step 1:

Obtain the curl commands required to access the json output of the consumption data from the following URL. Note that the commands below are copied over from my account with my details blanked out and hence will not work cut-pasted as is.

For your Electricity data, the curl command is of the form

curl -u "sk_live_KEYKEYKEYKEYKEYKEY:" "https://api.octopus.energy/v1/electricity-meter-points/11111111111111/meters/2222222222/consumption/"

For Gas usage, it is of the form

curl -u "sk_live_KEYKEYKEYKEYKEYKEY:" "https://api.octopus.energy/v1/gas-meter-points/33333333333/meters/E44444444444444/consumption/"

You will next need to modify the URLs in the curl commands above to obtain the consolidated data for the day by appending the string "?group_by=day" to the urls. 

So the commands above now modified are

curl -u "sk_live_KEYKEYKEYKEYKEYKEY:" "https://api.octopus.energy/v1/electricity-meter-points/11111111111111/meters/2222222222/consumption/?group_by=day"

curl -u "sk_live_KEYKEYKEYKEYKEYKEY:" "https://api.octopus.energy/v1/gas-meter-points/33333333333/meters/E44444444444444/consumption/?group_by=day"

for electricity and gas respectively. Make a note of these commands. You can test these immediately on your desktop which has curl available.

Step 2:

You now need to modify the sensors configuration for your homeassistant. My homeassistant configuration.yaml file has the following line

sensor: !include sensor.yaml

which lets me split out the sensors configuration to a separate sensors.yaml file. This means you will need to modify the configuration below by making a slight change in the indentation if you wish to add the configuration to configuration.yaml instead.

I first create two command line sensors to fetch the data from Octopus energy using the curl commands we obtained in Step 1.

- platform: command_line
  name: oe_electricity
  scan_interval: 86400
  value_template: '{{ value_json.count }}'
  json_attributes:
    - results
  command: >-
    curl -u "sk_live_KEYKEYKEYKEYKEYKEY:" "https://api.octopus.energy/v1/electricity-meter-points/11111111111111/meters/2222222222/consumption/?group_by=day"

- platform: command_line
  name: oe_gas
  scan_interval: 86400
  value_template: '{{ value_json.count }}'
  json_attributes:
    - results
  command: >-
    url -u "sk_live_KEYKEYKEYKEYKEYKEY:" "https://api.octopus.energy/v1/gas-meter-points/33333333333/meters/E44444444444444/consumption/?group_by=day"
These create two sensors sensor.oe_electricity and sensor.oe_gas which contain the json with the results from the output returned.

These have scan intervals of 1 day so that we do not end up hammering the Octopus Energy website with our requests. This data only changes once a day.

We then create two more sensors which read from that returned data.

- platform: template
  sensors:
    oe_electricity_yesterday:
      friendly_name: "Electricity Usage Yesterday"
      unit_of_measurement: 'kWh'
      value_template: "{{ state_attr('sensor.oe_electricity', 'results')[1]['consumption'] }}"
      device_class: energy
    oe_gas_yesterday:
      friendly_name: "Gas Usage Yesterday"
      unit_of_measurement: 'm³'
      value_template: "{{ state_attr('sensor.oe_gas', 'results')[1]['consumption'] }}"
      device_class: gas
This in turn creates 2 more sensors, sensor.oe_electricity_yesterday and sensor.oe_gas_yesterday with the data for the energy consumption from the previous day.

Restart Home Assistant to make sure the changes are effective and the sensors are available.

Step 3:

Add an automation to fetch the data at mid-day every day. From past experience, the electricity data seems to be updated at 0830 and the gas data updated at 1100 hours. I therefore run an automation at 1200 hours everyday to fetch the data for the previous day.I use the "Call service" -> "Home Assistant Core Integration: Update entity" to fetch the latest data. The yaml which describes this automation is as follows.

alias: 'Energy: Update OE readings'
description: ''
trigger:
  - platform: time
    at: '12:00'
condition: []
action:
  - service: homeassistant.update_entity
    target:
      entity_id:
        - sensor.oe_gas
        - sensor.oe_electricity
mode: single
Step 4:

Create Lovelace cards.

The yaml which describes this card is

type: entities
entities:
  - entity: sensor.oe_electricity_yesterday
  - entity: sensor.oe_gas_yesterday



The yaml for this card is

type: vertical-stack
cards:
  - type: sensor
    entity: sensor.oe_electricity_yesterday
    graph: line
    hours_to_show: 168
    name: Electricity Usage for the week
  - type: sensor
    entity: sensor.oe_gas_yesterday
    graph: line
    hours_to_show: 168
    name: Gas usage for the week
    detail: 1
There is a second option available to fetch data directly off the smart meter. This involves the purchase of a "Consumer Access Device" which can directly talk to the smart meter. I have already received such an instrument and will be investigating that next.


Setting a frost alarm on Home assistant

One of the issues with winter is the possibility of ice covering your windscreen which needs to be cleared before you can drive. Clearing ou...