Octopus Energy API

Hey!

Has anyone managed to integrate with the octopus energy API?

Aydan

1 Like

WhyAyden, did you get any progress on this? I too would love to be able to receive my cheap rates

1 Like

Hello, no I did not :frowning:
I ended up going back to HomeAssistant as it was more what i wanted.

Thanks for replying, if I get anywhere i will let you know.

Hello, did you manage to make any progress?

Ok so I have made progress sort of. It is a long winded way but I have a solution. I got chat gPT to write me an application that I have running on a raspberry pi that sends an update every minute to mqtt stating the charge time slots and also wether the current minute is in cheap rate mode or not. I then use the MQTT client in homey.

Wow, impressive lateral thinking!

I got a Homey Pro to avoid having to fiddle with workarounds, Raspberry Pi, etcā€¦ but first impressions is itā€™s still far from plug 'n play.

Maybe itā€™s possible to retreive data with homeyscript.
Can someone try this, or explain how I get a temporary API key (not a customer here)?

const myAPIKey = 'abc123' // change into personal API key here

const url = 'https://api.octopus.energy/v1/accounts/';
const res = await fetch(url, {
              headers : {
              'x-api-key': myAPIKey
              }
            });
            if (!res.ok) {
              throw new Error(res.status);
              return false;
            }

const json = await res.json();
//const value = json.value;
log (json);
return true;

I am currently out of the country until 1st September. I will be able to help then as I got chat GPT to do that but for me too. lol I have a copy of the code

Hi, Iā€™m an Octopus customer, and have an API key, but Iā€™m not a coder and am completely new to Homey Pro. Iā€™ve installed the Homeyscript app, but not sure what to do next.

Hi,

You can copy the code 1:1 by hitting the copy symbol:

Goto Homeyscript (https://my.homey.app/scripts)

Hit ā€œNew Homeyscriptā€, and paste the code in the top right screen, it should look similar to this:

Change the example API key in line 1 to your personal key;

Hit the blue Save button, and then the green ā€œRunā€ button next to it;

Now the results should show at the bottom screen, below ā€œConsoleā€

As you can see, I got error 401 returned, which means ā€˜unauthorisedā€™ (invalid API key)

I hope this gets you going.

Thanks for your very detailed instructionsā€¦ but unfortunately, I get exactly the same 401 error.

The URL https://api.octopus.energy/v1/accounts/ requires login credentials. So, do these need to be added to the script? (Sorry, I donā€™t really understand .json)

Yes, youā€™ll have to change the example API key abc123 in Line 1, into the API key you received:

Does that make any sense?
You can obtain your API key from your online dashboard (https://octopus.energy/dashboard/new/accounts/personal-details/api-access)

And, youā€™re welcome!

I am the same person that told you I have an API key.

I did use my own key, and when I got an error, I regenerated the key and tried again.

Sorry about that, forgot / did not read back the topic.
If you are sure your API key is valid, I donā€™t know what to test next.

One thing what might error: the single quotes ' ' are needed around your API key in line 1, donā€™t leave them out.
Make sure theyā€™re not fancy quotes (cursive). Only straight quotes are valid.

Yes I double-checked, the syntax is correct. Iā€™ve sent you a message.

Feel free to use thisā€¦ just add the info at the top. Iā€™ve still got some things Iā€™m working on but might help get you started.


// Make sure you update below within the ' ' quotes.
const apiKey = ''; // Your API key
const accountNumber = ''; // Your account number, something like A-1234567
const electricityMPAN = ''; // You can get this from developers section
const electricitySerialNumber = ''; // You can get this from developers section
const gasMPRN = ''; // // You can get this from developers section
const gasSerialNumber = ''; // You can get this from developers section

const baseUrl = 'https://api.octopus.energy/v1';

function createAuthHeader(apiKey) {
    const authString = `${apiKey}:`;
    const authBase64 = Buffer.from(authString).toString('base64');
    return `Basic ${authBase64}`;
}

async function fetchAccountDetails() {
    const url = `${baseUrl}/accounts/${accountNumber}/`;

    try {
        const response = await fetch(url, {
            headers: {
                Authorization: createAuthHeader(apiKey)
            }
        });

        if (!response.ok) {
            throw new Error(`Error fetching account details: ${response.statusText}`);
        }

        const data = await response.json();
        console.log('Account Details:', data);
        return data;
    } catch (error) {
        console.error('Error:', error);
    }
}

async function fetchElectricityMeterData() {
    const url = `${baseUrl}/electricity-meter-points/${electricityMPAN}/meters/${electricitySerialNumber}/consumption/`;

    try {
        const response = await fetch(url, {
            headers: {
                Authorization: createAuthHeader(apiKey)
            }
        });

        if (!response.ok) {
            throw new Error(`Error fetching electricity meter data: ${response.statusText}`);
        }

        const data = await response.json();
        console.log('Electricity Meter Data:', data);
        return data;
    } catch (error) {
        console.error('Error:', error);
    }
}

async function fetchGasMeterData() {
    const url = `${baseUrl}/gas-meter-points/${gasMPRN}/meters/${gasSerialNumber}/consumption/`;

    try {
        const response = await fetch(url, {
            headers: {
                Authorization: createAuthHeader(apiKey)
            }
        });

        if (!response.ok) {
            throw new Error(`Error fetching gas meter data: ${response.statusText}`);
        }

        const data = await response.json();
        console.log('Gas Meter Data:', data);
        return data;
    } catch (error) {
        console.error('Error:', error);
    }
}

async function fetchBillingDetails() {
    const url = `${baseUrl}/billing/charges/`;

    try {
        const response = await fetch(url, {
            headers: {
                Authorization: createAuthHeader(apiKey)
            }
        });

        if (!response.ok) {
            throw new Error(`Error fetching billing details: ${response.statusText}`);
        }

        const data = await response.json();
        console.log('Billing Details:', data);
        return data;
    } catch (error) {
        console.error('Error:', error);
    }
}

(async () => {
    await fetchAccountDetails();
//    await fetchElectricityMeterData();
//    await fetchGasMeterData();
//    await fetchBillingDetails();
})();

What will this do exactly ?

Just had solar and battery installed and just started using octopus

Havenā€™t yet connected the inverter to homey but depending on what this does my next step is to find a way to adjust behaviour of the system and house to tariff info.

Would try homey if I can find a system thatā€™s simple enough otherwise itā€™ll have to go on the HA

Thanks in advance.