So it was worth checking if the API rate data makes any sense, and by that my Home Assistant has the excellent Octopus Energy integration from BottleCapDave. Data is piped routinely to InfluxDB which can be queried using the influx CLI:
influx query 'from(bucket:"YOURBUCKET") |> range(start:-100d) |> filter(fn: (r) => r._measurement == "GBP/kWh" ) |> filter(fn: (r) => r["_field"] == "value")' > historic-rates.csv
(Your measurement field value may be different, so use another predicate filter to just extract the needed values.)
Checking this data against the actually billed data it seems that the rate presented in the Home Assistant API aligns to the subsequently billed rate in the relevant Octopus pdf bill. So it can be relied upon. Probably.
It would be possible to take the InfluxDB data and process it to find when the rate changes, then align that to the API output usage data in order to create a bill before Octopus send one. However, my laziness took over and thought it better just to have Home Assistant write a CSV file the columns of which may be offered to the API usage by time. So a recorder automation can write the two columns for subsequent spreadsheet monkeying:
alias: Octopus Import Rate Tracker
description: >-
Ideally this writes the sensor value to file every 30 minutes at the end of
the half hour time block
triggers:
- trigger: time_pattern
minutes: "29"
seconds: "59"
- trigger: time_pattern
minutes: "59"
seconds: "59"
conditions: []
actions:
- action: notify.send_message
metadata: {}
data:
message: >-
{{states.sensor.octopus_energy_electricity_METER_MPAN_current_rate.state}}
target:
entity_id: notify.file_WHATEVER-YOUR-FILE-ENTITY-IS
mode: single
You need the File integration to make this work, with " allowlist_external_dirs:" set up correctly in configuration.yaml. Set the File entity up to have a timestamp in the File integration entries list (Click on the 'Configure' button and ensure that the Option to include a Timestamp is selected.)
Over the last few hours this has worked fine, so probably will do the job.
To get the data from the API for a given time period my preferred ('lazy') approach is to open a terminal where the data is needed and make a request:
curl -H "Authorization: Basic ENCODED-API-TOKEN" "https://api.octopus.energy/v1/electricity-meter-points/<MPAN>/meters/<METER>/consumption/?period_from=2024-10-01T00:00:00&period_to=2024-10-31T23:30:00&order_by=period&page_size=1500" > octopus_import_Oct24.txt
Use your own encoded API string, MPAN and METER, filename in the above and it's easy to turn the output into a usable CSV (using sed / awk / find and replace / etc). Then align the columns in your two data sources (API call plus Home Assistant rate recorder) and make your own [hopefully accurate] bills :-)