Not so much a question as posting a solution for anyone looking to extract daily Tracker energy prices for electricity and gas from the Octopus Energy API. It follows on from my solution to get the Octopus half-hourly rates for the Agile tariff.
I have 8 items to represent the unit rates and standing charge for electricity and gas for today and tomorrow. The standing charges only vary when the rules of the tariff change so you might not want to retrieve those regularly.
Items
// Octopus Tracker Rates
Number GasUnit0 "Gas Unit Rate Today [%.4fp]"
Number GasStanding0 "Gas Standing Charge Today [%.4fp]"
Number ElecUnit0 "Elec Unit Rate Today [%.4fp]"
Number ElecStanding0 "Elec Standing Charge Today [%.4fp]"
Number GasUnit1 "Gas Unit Rate Tomorrow [%.4fp]"
Number GasStanding1 "Gas Standing Charge Tomorrow [%.4fp]"
Number ElecUnit1 "Elec Unit Rate Tomorrow [%.4fp]"
Number ElecStanding1 "Elec Standing Charge Tomorrow [%.4fp]"
Rule
rule TrackerGetRates
when
System started or //run the rule at startup
Time cron "0 15 16-20/1 1/1 * ? *" or //and hourly from 4:15pm to 8:15pm when the rates for the next day should be updated
Time cron "0 0 0 1/1 * ? *" // at midnight to move the rates to the right day
then
// Get today's rates
val LocalDateTime today = LocalDateTime.now(); //Today
var String dateString = today.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) //get today's date as a string to insert into the JsonPath transformation
// Get Gas Rate Today
val String TrackerJSONgas = sendHttpGetRequest("https://octopus.energy/api/v1/tracker/G-1R-SILVER-2017-1-B/daily/current/0/1/")
// logWarn ("rules.TrackerGetRates", "Gas Response: "+TrackerJSONgas)
val Number GasUnitToday = (Double::parseDouble(transform("JSONPATH", "$.periods[?(@.date == '"+dateString+"')].unit_rate", TrackerJSONgas))) //extract the rate and convert to a number
logWarn ("rules.TrackerGetRates", "Gas unit rate today: "+GasUnitToday.toString)
val GasStandingToday = (Double::parseDouble(transform("JSONPATH", "$.periods[?(@.date == '"+dateString+"')].standing_charge", TrackerJSONgas))) //extract the standing charge and convert to a number
logWarn ("rules.TrackerGetRates", "Gas Standing Charge today: "+GasStandingToday.toString)
// Get Electricity Rate Today
val String TrackerJSONelec = sendHttpGetRequest("https://octopus.energy/api/v1/tracker/E-1R-SILVER-2017-1-B/daily/current/0/1/")
// logWarn ("rules.TrackerGetRates", "Elec Response: "+TrackerJSONelec)
val ElecUnitToday = (Double::parseDouble(transform("JSONPATH", "$.periods[?(@.date == '"+dateString+"')].unit_rate", TrackerJSONelec))) //extract the rate and convert to a number
logWarn ("rules.TrackerGetRates", "Elec unit rate today: "+ElecUnitToday.toString)
val ElecStandingToday = (Double::parseDouble(transform("JSONPATH", "$.periods[?(@.date == '"+dateString+"')].standing_charge", TrackerJSONelec))) //extract the standing charge and convert to a number
logWarn ("rules.TrackerGetRates", "Elec Standing Charge today: "+ElecStandingToday.toString)
// Get tomorrow's rates
val LocalDateTime tomorrow = today.plusDays(1); //Plus 1 day
dateString = tomorrow.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) //get Tomorrow's date as a string to insert into the JsonPath transformation
// Get Gas Rate Tomorrow
val GasUnitTomorrow = (Double::parseDouble(transform("JSONPATH", "$.periods[?(@.date == '"+dateString+"')].unit_rate", TrackerJSONgas))) //extract the rate and convert to a number
logWarn ("rules.TrackerGetRates", "Gas unit rate Tomorrow: "+GasUnitTomorrow.toString)
val GasStandingTomorrow = (Double::parseDouble(transform("JSONPATH", "$.periods[?(@.date == '"+dateString+"')].standing_charge", TrackerJSONgas))) //extract the standing charge and convert to a number
logWarn ("rules.TrackerGetRates", "Gas Standing Charge tomorrow: "+GasStandingTomorrow.toString)
// Get Electricity Rate Tomorrow
val ElecUnitTomorrow = (Double::parseDouble(transform("JSONPATH", "$.periods[?(@.date == '"+dateString+"')].unit_rate", TrackerJSONelec))) //extract the rate and convert to a number
logWarn ("rules.TrackerGetRates", "Elec unit rate tomorrow: "+ElecUnitTomorrow.toString)
val ElecStandingTomorrow = (Double::parseDouble(transform("JSONPATH", "$.periods[?(@.date == '"+dateString+"')].standing_charge", TrackerJSONelec))) //extract the standing charge and convert to a number
logWarn ("rules.TrackerGetRates", "Elec Standing Charge tomorrow: "+ElecStandingTomorrow.toString)
// Update the items
GasUnit0.postUpdate(GasUnitToday)
GasStanding0.postUpdate(GasStandingToday)
ElecUnit0.postUpdate(ElecUnitToday)
ElecStanding0.postUpdate(ElecStandingToday)
GasUnit1.postUpdate(GasUnitTomorrow)
GasStanding1.postUpdate(GasStandingTomorrow)
ElecUnit1.postUpdate(ElecUnitTomorrow)
ElecStanding1.postUpdate(ElecStandingTomorrow)
end
As with extracting anything from the Octopus API, you need to use the right product code. The codes seem to be the same across the country except that you need to amend them for your region. You’re looking for the letter that precedes the /daily in the URL. In my case it’s B.
The easiest way to find the correct letter is using the API to find the grid supply point for your postcode e.g.
https://api.octopus.energy/v1/industry/grid-supply-points/?postcode=SE1