thegreenwebfoundation / co2.js

An npm module for accessing the green web API, and estimating the carbon emissions from using digital services
Other
385 stars 48 forks source link

Allow developers to access real-time grid intensity data through Electricity Maps #134

Open fershad opened 1 year ago

fershad commented 1 year ago

Currently, there is historical yearly average and marginal grid intensity data available in CO2.js. We would like to take this a step further by giving developers a way to use CO2.js to access real-time grid intensity data from Electricity Maps and CO2signal.

This would be done by effectively providing a wrapper function that calls the Electricity Maps or CO2signal API to fetch data. Developers would need to have their own API token for the service they wish to use.

Docs for both APIs can be found at the links below:

mrchrisadams commented 1 year ago

Electricity Maps isn't the only provider of information like this, but they do seem to offer the broadest global coverage.

I found out recently that in Germany, one of the major grid operators is exposing realtime information over an API too, without charging.

You can see the map version below: https://co2map.the-nest.io

And the API docs are here: https://portal.traxes.io/co2intensity-dev/documentation/co2itensity

fershad commented 1 year ago

Add CO2 Signal API to CO2.js

fershad commented 1 year ago

Add Electricity Maps API to CO2.js

mrchrisadams commented 10 months ago

There is now an API for Germany (or at least the northern part of Germany covered by the 50hz grid)

https://eco2grid.50hertz.com/calculation

API details below:

https://api-portal.eco2grid.com/

fershad commented 8 months ago

Thinking through how an API for this might look.

Scope

Limiting the scope to just working with the ElectricityMaps API free tier. The paid tier generates unique base URLs for each subscriber, which would make implementation a bit more complicated. Possibly something for a later release.

Spec

We currently allow for the Sustainable Web Design model to be passed an object with adjusted values for grid intensity and other variables. This is done through the perVisitTrace and perByteTrace functions.

const bytesSent = 1000 * 1000 * 1000; // 1GB expressed in bytes
const greenHost = false; // Is the data transferred from a green host?
const options = {
  dataReloadRatio: 0.6,
  firstVisitPercentage: 0.9,
  returnVisitPercentage: 0.1,
  gridIntensity: {
    device: 565.629,
    dataCenter: { country: "TWN" },
    networks: 442,
  },
};

estimatedCO2 = co2Emission.perVisitTrace(bytesSent, greenHost, options);

For grid intensity, the user has the option to adjust grid intensity for the device, network, or data center segments. This can be done by either:

To extend this to use the /carbon-intensity/latest endpoint from Electricity Maps, we could have the user pass in a different object with the following information:

{
    provider: "Electricity Maps", // required
    apiKey: "<YOUR_API_KEY_HERE>", // required
    zone: "<ISO Alpha 2 Country Code>"  // required
}

The zone should correspond to the Zones available in the Electricity Maps API for the tier provided. E.g. For the free tier, Zones are listed at: https://api-access.electricitymaps.com/free-tier/zones

With this, the example above would look like:

const bytesSent = 1000 * 1000 * 1000; // 1GB expressed in bytes
const greenHost = false; // Is the data transferred from a green host?
const options = {
  dataReloadRatio: 0.6,
  firstVisitPercentage: 0.9,
  returnVisitPercentage: 0.1,
  gridIntensity: {
    device: 565.629,
    dataCenter: {
        provider: "Electricity Maps",
        apiKey: "thisisafakeapikey",
        zone: "TW"
    },
    networks: 442,
  },
};

estimatedCO2 = co2Emission.perVisitTrace(bytesSent, greenHost, options);

With this, we can then make then make a fetch request to the https://api-access.electricitymaps.com/free-tier/carbon-intensity/latest endpoint to fetch the latest grid intensity for Taiwan & use that in the SWD calculations.

Consideration

fershad commented 7 months ago

After chatting with @rossf7 about this, based on his experience with thegreenwebfoundation/grid-intensity-go I'd like to make this a more modular approach.

This would separate the code logic for using an external data source from the CO2 calculation. In this way, we would also make it easier for folks to contribute other data sources (like the Germany one @mrchrisadams linked to above).

In this way developers can also use CO2.js as a means of connecting with an external data source, even if they don't want to use that data for a carbon calculations.

To make this possible, we'd have to create a new Class, which we can then expose to users. This can then be imported into a project.

fershad commented 7 months ago

Being worked on in https://github.com/thegreenwebfoundation/co2.js/tree/add-external-data-sources

Should have a PR ready for review in the next couple of days.