rogerthomas84 / php-octopus-mini-api
A working example of getting live consumption data from the Octopus API using PHP
Package info
github.com/rogerthomas84/php-octopus-mini-api
pkg:composer/rogerthomas84/php-octopus-mini-api
Requires
- php: >=8.4
- ext-json: *
- guzzlehttp/guzzle: ^7.0
Requires (Dev)
- district5/date: *
- phpunit/phpunit: ^9.6
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Description...
This is a simple library, with a basic implementation of the Octopus API, as well as a small part of their GraphQL API. It's original purpose was primarily to serve as a documentation of how to retrieve the live consumption data from the Octopus Energy GraphQL API, but has since been expanded to include some other basic functionality, such as retrieval of half-hourly consumption data, as well as account information.
In order to retrieve the live consumption data, you must have an Octopus Mini device, and (obviously) you need to have an account with Octopus Energy. If you have an Octopus account, but don't have a Mini device, you can request a free one from Octopus Energy here.
Much of this library has been written using the documentation available from Octopus:
- https://developer.octopus.energy/rest/
- https://developer.octopus.energy/rest/reference
- https://developer.octopus.energy/rest/guides/endpoints
- https://developer.octopus.energy/graphql/guides
For the full guide (authentication, current tariffs, standing charges and unit rates, consumption, live data and error handling), see USAGE.md.
Requirements...
- PHP 8.4 or later
- An Octopus Energy account, and your API key (from https://octopus.energy/dashboard/new/accounts/personal-details/api-access)
Usage...
composer require rogerthomas84/php-octopus-mini-api
$apiInstance = \Rt\OctopusAPI\OctopusApiSingleton::getInstance() ->setEmail(getenv('OCTOPUS_EMAIL')) // optional: the API key is used for GraphQL if no email/password ->setPassword(getenv('OCTOPUS_PASSWORD')) // optional ->setAccountNumber(getenv('OCTOPUS_ACCOUNT_NUMBER')) ->setApiKey(getenv('OCTOPUS_API_KEY')) ->setMpan(getenv('OCTOPUS_MPAN')) ->setSerialNumber(getenv('OCTOPUS_SERIAL_NUMBER')); // Optionally supply your own Guzzle client (timeouts, proxies, etc.) // $apiInstance->setHttpClient(new \GuzzleHttp\Client(['timeout' => 10])); $graphQl = $apiInstance->getOctopusGraphQL(); // $myToken = $graphQl->getToken(); // $meterDeviceId = $graphQl->getMeterDeviceId(); $consumption = $graphQl->getLiveConsumption(); // watts; negative when exporting // or you can pass the meterDeviceId directly: // $consumption = $graphQl->getLiveConsumption($meterDeviceId); // or get the full reading (readAt, demand, consumption, consumptionDelta, export): // $telemetry = $graphQl->getLiveTelemetry(); echo 'Current consumption is ' . $consumption . "W\n"; $myAccount = $apiInstance->getAccount(); echo 'You have ' . count($myAccount->getProperties()) . " properties on your account\n"; $myProperty = $myAccount->getProperties()[0]; echo 'The first property on your account is ' . $myProperty->getAddressLine1() . "\n"; $myMeter = $myProperty->getElectricityMeterPoints()[0]; echo 'The first electricity meter point on your property is ' . $myMeter->getMpan() . "\n"; // Find the agreement (tariff) that applies now $now = new DateTime(); $myAgreement = null; foreach ($myMeter->getAgreements() as $agreement) { if ($agreement->isValidForDateTime($now)) { $myAgreement = $agreement; } } $myTariffCode = $myAgreement->getTariffCode(); // e.g. E-1R-VAR-22-11-01-K echo 'My tariff code is ' . $myTariffCode . ', in region ' . $myAgreement->getRegionCode() . "\n"; $dateFrom = new DateTime('2024-01-01T00:00:00Z'); $dateTo = new DateTime('2024-01-02T00:00:00Z'); $electricity = $apiInstance->getElectricityService(); // Consumption in kWh. Every page is fetched automatically. $consumptionData = $electricity->getHalfHourReadings( $dateFrom, $dateTo, 25000, // page size (max 25,000) '-period', // or 'period', used for sorting null // or 'hour', 'day', 'week', 'month', 'quarter' to group the readings ); // Standing charges (p/day) and unit rates (p/kWh). Every page is fetched automatically. $standingCharges = $electricity->getStandingCharges($myTariffCode, $dateFrom, $dateTo); if ($myAgreement->isDualRegister()) { // Economy 7 style (E-2R-...) tariffs have day and night rates instead of standard rates $dayRates = $electricity->getDayUnitRates($myTariffCode, $dateFrom, $dateTo); $nightRates = $electricity->getNightUnitRates($myTariffCode, $dateFrom, $dateTo); } else { $unitRates = $electricity->getStandardUnitRates($myTariffCode, $dateFrom, $dateTo); }
Dates can be in any timezone; they're converted to UTC before being sent to the API.
Problems with the configuration or with the API's response (for example a failed GraphQL login) throw
\Rt\OctopusAPI\Exception\OctopusApiException. HTTP errors (e.g. a 404 for an unknown tariff) are raised as Guzzle
exceptions.
Testing...
The unit tests use mocked HTTP responses and need no credentials:
composer install
composer test:unit
The integration tests run against the live Octopus API. They're skipped unless the following environment variables are set:
OCTOPUS_EMAILOCTOPUS_PASSWORDOCTOPUS_ACCOUNT_NUMBEROCTOPUS_API_KEYOCTOPUS_MPANOCTOPUS_SERIAL_NUMBER
composer test # everything
composer test:integration # live tests only