Experiments with the free marginal carbon intensity from Wattime

Wattime have a free marginal intensity API. This is interesting because it shows how dirty the next few hours of power usage are likely to be.

import os
import requests
# register_url = 'https://api2.watttime.org/v2/register'
# params = {
#             'username': os.getenv("WATT_TIME_USERNAME"),
#             'password': os.getenv("WATT_TIME_PASSWORD"),
#             'email': os.getenv("DEVELOPER_ACCOUNT_EMAIL"),
#             'org': os.getenv("DEVELOPER_ORG")
#          }
# rsp = requests.post(register_url, json=params)
# print(rsp.text)

OK, that worked. How about getting a marginal reading for our current location?

Assume these are our coordinates, based on running this snippet run in a browser console (if you're on Firefox, hit cmd+alt+i to bring up a javascript console you can type into).

# run this in a browser console
navigator.geolocation.getCurrentPosition((position) => {
  	lat: position.coords.latitude,
    lng: position.coords.longitude

You should get back a value like this listing below:

// Object { lat: 52.49397468012254, lng: 13.43946456542592 }

You can then use the values to execute an API request:

import os
import requests
from requests.auth import HTTPBasicAuth
# first, fetch a token to use for all future API requests
login_url = 'https://api2.watttime.org/v2/login'
creds = HTTPBasicAuth(os.getenv("WATT_TIME_USERNAME"), os.getenv("WATT_TIME_PASSWORD"))
token = requests.get(login_url, auth=creds).json()['token']
# then use the token for our requests
index_url = 'https://api2.watttime.org/index'
headers = {'Authorization': f"Bearer {token}"}
# the lat lng values from our browser lookup. This is converted to a country or grid region
params = {'latitude': 52.49397468000891, 'longitude': 13.439464565019538}
rsp=requests.get(index_url, headers=headers, params=params)

That does this percent mean? Its a number to show how relatively dirty the power is right now. Read the API docs for more about the Wattime Marginal Operating Emissions index:

the reported index value is given as a percentage between the lowest and highest MOER values observed in the past two weeks in the given grid region. A higher index indicates a higher MOE value (i.e. higher carbon intensity).

Because you can do a /lng look up, this means we make code in client side javascript, or even in browser extensions do different things based on how 'dirty' the energy where the user is. It also means you could package up a default provider in the grid intensity exporter we worked on last year, to make any k8s or nomad cluster carbon aware.

Coverage is best Western Europe and North America.

You can see a list of all the supported regions by making a request to the following endpoint:

import json
response = requests.get("https://api2.watttime.org/v2/ba-access?all=true", headers=headers, params=params)
# TODO: we might want to make this a CSV output for readability.
with open('./results/all-regions.json', 'w') as json_file:

if you're in another part of the world, we'd love to chat about expanding coverage - drop us a line chris@thegreenwebfoundation.org, or tag @mrchrisadams in a tweet with a link to this nextjournal notebook.

Runtimes (1)