Hey!
So I recently moved over to Octopus Energy’s gas tracker tariff. The price for your gas is set at 00:00 each day based on wholesale pricing with a price cap for consumer protection, and it’s a flat rate for the whole day. Setting this correctly lets more accurately model my gas consumption costs using the stock Home Assistant Energy tooling.
The API for the day pricing on electricity is extensively documented, both by Octopus and third party authors. The Gas Tracker however required that I research on the Octopus Intelligent forum, and even then it took some final integration on my part.
I’m not going to claim this is the most elegant approach. I’ve broken my calculations into separate sensors where I felt something might have reuse value/I want to track it independently. There is probably a single sensor solution for this with the correct value template, but I can stand the extra two template sensors.
It consists of three parts —
- Day number Template Sensor - sourced from here: [Create sensor to show only today's day "number" (i.e. 2017-09-05, my sensor would output the "05")]
- platform: template
sensors:
today_number:
value_template: '{{now().strftime("%-d")}}'
I need this as the Octopus API for the returns JSON I can address as an array. Said array is base index 0 and contains only Tracker values for this month, be they calculated yet or not, in date order. If you’re interested, future values are pre-populated with last year’s data (we think).
- Octopus API REST sensor - Todays Tracker price
I hit the Octopus web API which returns Tracker data for my tariff. Finding your tariff code (in my example: G-1R-SILVER-22-07-22-H) is a challenge. The last letter is your DNO region code which you can lookup (I’ll provide links) and the tariff name was provided to me on one of my sign up emails or in my Octopus account dashboard. I’ll update this thread with ideas on that if you need them.
- platform: rest
resource: https://octopus.energy/api/v1/tracker/G-1R-SILVER-22-07-22-H/daily/current/0/0
method: GET
name: octopus_uncapped_gas_tracker_cost_by_API
value_template: "{{((value_json.periods[((states('sensor.today_number') | float | round(0)-1))].unit_rate)/100) | round (4) }}"
unit_of_measurement: 'GBP/kWh'
scan_interval: 3600
force_update: true
icon: hass:currency-gbp
This sensor returns the UNCAPPED rate for the day. It’s set to update pretty infrequently, as the way I’d expect someone to use this is to force an entity update using NodeRed at 00:05.
[Manually refresh rest sensor?]
[How to translate this to NodeRED?]
You’ll note I perform the minus 1 operation on the day number here, after converting it into a simple integer, as I may not always want to reference the day number as an array index. There’s also a divide by 100 function on the API return as it provides the units in pence and HASS elements expect it in GBP. The rounding is just for my own sanity.
- Capped rate Template Sensor
This one is essentially a clone sensor, copying the value in sensor (2) unless it’s greater than 16 pence, in which case it flattens it down to that value. You may be on the 22p capped tracker if you joined later than me, or the 10p capped tariff. Guess what you have to change?
#Octopus Tracker unit rate sensor
- platform: template
sensors:
octopus_gas_tracker_unit_rate_with_cap:
unique_id: sensor.octopus_gas_tracker_unit_rate_with_cap
friendly_name: "Octopus Tracker Unit Rate"
unit_of_measurement: 'GBP/kWh'
icon_template: hass:currency-gbp
value_template: >
{% if (states('sensor.octopus_uncapped_gas_tracker_cost_by_API') | float) < 0.16 %}
{{states('sensor.octopus_uncapped_gas_tracker_cost_by_API')}}
{% else %}
0.16
{% endif %}
I’m probably going to change the cap number and tariff code to be input helpers in Home Assistant, so I don’t have to dig into the code next year assuming the API operates the same way, but for the purposes of someone getting value out of this tutorial, I’ll keep it without those elements.
I hope this is of some use to someone. I’ll post a follow-up on getting your Gas Tracker product code and region code. Good primer lives in terms of general approach lives here: [Guy Lipman - Energy]
P.S. I’m not sure if this entire endeavour is made irrelevant by the Octopus Energy HASS integration BUT if you want something lighter weight or to explore first principles? I’m going to go upset myself now by way of installing suspected successor.