Octopus Energy Agile Tariff

I’ve managed to get the octopus energy agile pricing working directly via a curl command as i was having issues with IFTTT delays

image

You may have to edit the API URL to retrieve the agile pricing for you region. Those values can be found on the octopus developers page. YOU DO NOT NEED YOUR API KEY FOR THIS TO WORK

sensor code is;

sensor:
  - platform: command_line
    name: electricity_cost
    scan_interval: 86400
    command: curl \"https://api.octopus.energy/v1/products/AGILE-18-02-21/electricity-tariffs/E-1R-AGILE-18-02-21-F/standard-unit-rates/?period_from={{ now().year }}-{{ now().month }}-{{ now().day }}T{{ now().hour }}:{% if now().minute <30 %}00{% else %}30{% endif %}&period_to={{ now().year }}-{{ now().month }}-{% if (now().hour == 23) and (now().minute >=30) %}{{ now().day+1 }}{% else %}{{ now().day }}{% endif %}T{% if (now().hour == 23) and (now().minute >=30) %}00{% elif now().minute >=30 %}{{ now().hour+1}}{% else %}{{ now().hour}}{% endif %}:{% if now().minute >=30 %}00{% else %}30{% endif %}\"
    value_template: '{{ (value_json.results[0].value_inc_vat) | round(2) }}'
    unit_of_measurement: 'p/KWH'

I also added some automation to periodically update the pricing as to not spam the Octopus API

- id: '1578085069925'
  alias: Update Octopus Agile Price
  description: ''
  trigger:
  - hours: '*'
    minutes: '30'
    platform: time_pattern
    seconds: '0'
  - hours: '*'
    minutes: '0'
    platform: time_pattern
    seconds: '0'
  condition: []
  action:
  - data:
      entity_id: sensor.electricity_cost
    service: homeassistant.update_entity

Hope someone finds this useful!

15 Likes

Thanks for this! First day with Hassio and mainly want to make use of the Octopus Agile API, not quite as easy as I thought!

I have copied your ‘sensor code’ into configuration.yaml which seems to work… is that correct?

Where does the ‘automation’ go?

And how did you create the nice semicircular graph?

Many thanks!

Yes then the automation bit goes in automation.yaml. The automation bit is to only call the api on the hour and on the half hour.

To display in in the format i have it in you need to add a gauge to your home page and the select the sensor youve just created.

Thanks, that’s great!

Really getting into this now and seems a great way to make full use of Agile.

BTW my main use is to make ‘smart’ my very old Economy 7 heaters (electrician installing contactors controlled by a Sonoff which I will flash with Tasmota) avoiding cloud based issues. Going to try and use some Zigbee temperature sensors, your Agile pricing sensor and person tracking to only heat when really needed and when the price is low enough…

J

1 Like

Interesting, thanks! I have just been looking at the tariff and thought that using HA to automate things based on the tariff would be interesting. Eg. switch on the dishwasher or tumble drier at the cheapest point overnight (yes, I appreciate the fire hazards, perhaps I need a smart smoke detector too!)

They generate the following day’s tariff daily “usually” around 4pm, so it might be possible to schedule a single download and cache it I think?

I’m currently with Bulb, but after having a SMETS2 meter installed I find I can’t actually use their best smart tariff, and they don’t have any sort of detailed usage API available, so I might jump ship…

Yes, eventually want to be a bit smarter, but at the moment just switching below/above thresholds. Want to work out how to calculate lowest cost periods* for similar reasons to you @Gareth79, and maybe start controlling my immersion heater too.

  • You can do this using Octopus’ IFTTT triggers already but want to move away from IFTTT as find it unreliable

@dmgreen have you, or seen someone, look ahead at the cost to schedule automations?

I, like @Gareth79, am looking to, for example “turn on X when cost is at lowest average price for 2 hours tomorrow”

Don’t want to reinvent the wheel if you have seen it elsewhere!

J

This is awesome, thank you. Worked first time.

I’m hoping to build up a HA display with all my energy stuff on there; Agile, Current usage / Solar generation from emoncms / emonpi.

Will save me hand coding more python like when I created this. :slight_smile:

40292752413_7564688184_z

If anyone is interested, I also created a website that shows all the historical pricing from Agile (import and outgoing) and Go tariffs.

Thinking out loud, i’d like to see the whole says pricing as a graph on my display. I’m pretty new to HA, so not sure how to even go about that?

Just fell foul of this… electricity is spelt wrong in the automation’s section.

So you get a ’ not found’ error when it tries to update on the hour/half hour.

Easy fix though, thanks again for pulling this one together. Love it.

PS. I also had to remove the ‘description’ entry line otherwise config check moaned. Maybe something to do with newer HA versions? Don’t know.

I’m about to sign up for the tariff myself so I’ll be looking at this sort of thing myself. A graph with the future pricing and a red/amber/green highlight would be useful, plus perhaps a countdown to the next time period where the price is below a certain threshold. Given the price is quite seasonal, it would probably need to be based on a statistical function of the day’s pricing itself, you couldn’t say “when the price drops below 8p” or similar.

Thanks, updated the spelling

1 Like

Hi Gareth

No plans to do that currently as all of mine are based around being cheaper than running a gas boiler (so a set price).

Cannot fault Octopus, customer service excellent, and with Agile, my average price for electric (now I control my Economy 7 heaters) is already at 7p/kWh and I am hoping to reduce this further using Home Assitant Automations. If you need a referral link for Octopus, this is mine - https://share.octopus.energy/ruby-gnat-30 - £50 off your first bill.

@dmgreen Hi Dave,

Was wondering if you think it would be possible to amend the curl to pull out say the next 4 hours of pricing? ie, 8 x 30 min slots?

now + 30 mins
now + 60 mins
now + 120 mins
now + 180 mins
etc

So that we could somehow put them in a list to display on screen (upcoming pricing)… so you can plan over the next few hours.

Basically what i’m saying is… to show the wife that 4pm is coming and its going to get expensive. :smile:

Just used your sign up link. I will not miss npower. Also looking forward to some HA integration.

1 Like

Great news, I am really enjoying making the most of the cheap periods of electricity. Here is my ‘Heating on timer’ and heating off… Warm in the morning and ready for when I get home from work. I find my Economy7 heaters take about 2 hours from start of charge to give out full heat. I am using a Conbee Zigbee stick and Xiaomi Aqara Temperature sensors for my temperature monitoring and a Sonoff Zigbee Switch to control the Contactor I have had installed at the consumer unit to turn the heaters on and off

  alias: Heating timer
  description: ''
  trigger:
  - hours: '*'
    minutes: '30'
    platform: time_pattern
    seconds: '10'
  - hours: '*'
    minutes: '0'
    platform: time_pattern
    seconds: '10'
  condition:
  - below: '9'
    condition: numeric_state
    entity_id: sensor.electricity_cost
  - condition: or
    conditions:
    - below: '19.5'
      condition: numeric_state
      entity_id: sensor.lounge_sensor
    - below: '19.5'
      condition: numeric_state
      entity_id: sensor.upstairs_sensor
  - condition: or
    conditions:
    - after: 02:00:00
      before: 05:00:00
      condition: time
    - after: '14:00:00'
      before: '22:00:00'
      condition: time
  action:
  - device_id: d99ad6ee9bb5458a9ada3827b9894fe1
    domain: light
    entity_id: light.heating_switch
    type: turn_on
  alias: Heating off
  description: ''
  trigger:
  - above: '20'
    entity_id: sensor.upstairs_sensor
    platform: numeric_state
  - above: '9'
    entity_id: sensor.electricity_cost
    platform: numeric_state
  - at: 05:00:00
    platform: time
  - at: '22:00:00'
    platform: time
  - above: '20'
    entity_id: sensor.lounge_sensor
    platform: numeric_state
  condition: []
  action:
  - device_id: d99ad6ee9bb5458a9ada3827b9894fe1
    domain: light
    entity_id: light.heating_switch
    type: turn_off 
1 Like

I’ll raise you a rest sensor :grinning:

sensor:
  - platform: rest
    name: Octopus
    resource_template: >-
      https://api.octopus.energy/v1/products/AGILE-18-02-21/electricity-tariffs/E-1R-AGILE-18-02-21-N/standard-unit-rates/?period_from=
      {% set ts_now = ((as_timestamp(now())/1800)|round(0,'floor')|int * 1800) %}
      {{ ts_now |timestamp_custom ('%Y-%m-%dT%H:%M:%SZ') }}&period_to=
      {{ (ts_now + 24*60*60) |timestamp_custom ('%Y-%m-%dT%H:%M:%SZ') }}
    unit_of_measurement: 'p/kWh'
    value_template: '{{ (value_json.results[-1].value_inc_vat) | round(2) }}'
    json_attributes:
      - "results"

2 differences.

  • I just get 24 hours of data starting with the current half hour period
  • I use the last element of the array. Octopus return the data in reverse order so [0] is the furthest away value not the current one.

You will see you get all the results - I need to work out how to use that to get the minimum value over the period which could be used in an automation.

For me, templates are just trial and error!

Hi Brian,

Is this helpful?

order_by ( string, optional )

Ordering of results returned. Default is that results are returned in reverse order from latest available figure. Valid values: * ‘period’, to give results ordered forward. * ‘-period’, (default), to give results ordered from most recent backwards.

https://developer.octopus.energy/docs/api/

Yes - I’d missed that in the original URL!

Thanks.

[edit]
Actually, I think that only works for consumption as the default is to give from most recent.