This repository contains the source code for National Grid: Live.
This project tracks overall energy provision and consumption for the UK, providing real-time and historical data on:
- Electricity generation by type: coal, CCGT (combined cycle gas turbine), OCGT (open cycle gas turbine), nuclear, oil, wind, hydro, pumped storage, biomass, battery, and other sources
- Embedded generation: solar and wind power connected to the local distribution network (rather than the national transmission network)
- Interconnector imports/exports: IFA, Moyle, BritNed, EWIC, Nemo, IFA2, NSL, ElecLink, Viking, and Greenlink
- Electricity pricing: wholesale electricity prices from the APX spot market
- Carbon emissions: carbon intensity of electricity generation in grams of CO₂ per kilowatt-hour
- Direct gas consumption by sector: domestic (heating, cooking), industrial, and commercial
- Gas consumption is tracked separately from gas-fired electricity generation (CCGT/OCGT) to avoid double-counting
The application updates every 5 minutes via a cron job running update.php, fetching the latest data from multiple sources and aggregating it across different time periods.
The development environment uses Docker.
Copy .env.example to .env and edit the values as appropriate. At a minimum, DATABASE_PASSWORD must be given a value.
To start the containers:
docker compose up --detach
Once the containers are running, you can view the site at http://localhost:9714/. Port 9714 was chosen due to its slight resemblance to the word ‘grid’.
To run the update script:
docker compose exec php php /var/grid/update.php
To stop the containers:
docker compose down
The production environment does not use Docker, instead running directly on the server. PHP 8.3 and a recent version MariaDB or MySQL are required.
Copy .env.example to .env and edit the values as appropriate. At a minimum, DATABASE_PASSWORD must be given a value. DATABASE_HOSTNAME should be changed to localhost if the database is running on the same server.
Upload .env, update.php, and the classes and public directories to the server.
Create a database and a user with SELECT, INSERT, UPDATE, and DELETE privileges, and import grid.sql into the database.
The database stores time-series data in 5 tables with different granularities:
past_five_minutes- 5-minute electricity generation datapast_half_hours- 30-minute data (electricity generation, embedded generation, emissions, pricing, gas consumption)past_days- Daily aggregated datapast_weeks- Weekly aggregated datapast_years- Yearly aggregated data
Each table includes columns for:
- Electricity generation by type (coal, CCGT, OCGT, nuclear, oil, wind, hydro, pumped storage, biomass, battery, other)
- Embedded generation (solar and wind)
- Interconnector transfers (IFA, Moyle, BritNed, EWIC, Nemo, IFA2, NSL, ElecLink, Viking, Greenlink)
- Gas consumption by sector (domestic, industrial, commercial)
- Pricing and emissions data (where applicable)
Configure the server to serve the contents of the public directory. Note that this directory contains only static files, so the web server does not need to support PHP.
Set up a cron job to execute the update.php script (using the PHP CLI SAPI) every five minutes. The cron job must run as a user with write access to public/favicon.svg and public/index.html.
The script outputs details of the update process to standard output, and details of errors to standard error. An error with an individual data source does not abort the rest of the update process.
The CSS refers to proza-light.woff2 and proza-regular.woff2. These are commercial fonts, so are not included in this repository. Licences for Proza can be purchased from Bureau Roffa. Alternatively, the simplified free version Proza Libre can be used instead.
National Grid: Live uses Cloudflare’s content delivery network. Visit counts will be retrieved from Cloudflare if the CLOUDFLARE_API_TOKEN and CLOUDFLARE_ZONE_ID environment variables are set to non-empty strings. The Cloudflare API token must be configured to provide Analytics Read access for the zone.
PHP classes can be found in the classes directory. The Database class directly within this directory is responsible for all database access. The other classes are divided into three namespaces:
The Data namespace contains classes for reading data from the various data sources, as documented further below.
The State namespace contains classes representing the data needed to output the user interface. The State class is the overall container; an instance of this class is returned by the getState() method of a Database instance.
The UI namespace contains classes that output the user interface. The UI class has overall responsibility for outputting the HTML, while the Favicon class outputs the dynamically-updated favicon.
This API, developed by Elexon, reports power generation connected to the national transmission network, interconnector imports and exports, and pricing.
Data is available in JSON format at 30-minute or 5-minute granularity.
PHP classes: Generation, Pricing
This API, developed by the National Energy System Operator, estimates power generation from embedded solar and wind (generation connected to the local distribution network rather than the national transmission network).
Data is available in CSV format at 30-minute granularity. Estimates may be retrospectively updated.
PHP class: Demand
This API, developed by the National Energy System Operator and the University Of Oxford Department Of Computer Science, estimates the carbon intensity of electricity generation in grams of carbon dioxide per kilowatt-hour.
Data is available in JSON format at 30-minute granularity. Estimates may be retrospectively updated.
PHP class: Emissions
Gas consumption data infrastructure has been added to the database schema and backend. Comprehensive research completed 2025-10-30:
Key Finding: Real-time operational gas demand data by sector is NOT currently available through public APIs. Unlike electricity data, gas consumption data is not published in real-time with the same granularity and format.
Data Sources Evaluated:
-
NESO Data Portal (https://api.neso.energy/)
- ✅ API accessible and validated
- ❌ Only FES (Future Energy Scenarios) forecast data available
- 4 gas-related datasets found, all scenario/projection data (not operational)
- No equivalent to electricity "Demand Data Update"
-
- ✅ Accessible via web interface
- ❌ No public API for automated access
- Requires manual download or screen scraping
- May contain total system demand (sector breakdown uncertain)
-
- ✅ Most comprehensive sector breakdown (Domestic, Industrial, Commercial)
- ❌ Monthly/quarterly updates with significant lag
- Useful for historical analysis and sector ratio estimates
- Not suitable for real-time tracking
Current Status:
- Database schema ready with
domestic_gas,industrial_gas,commercial_gascolumns - Backend infrastructure implemented and tested
- UI prepared to display gas consumption
- Data source: Awaiting availability of real-time API data
Implementation Approach:
- Infrastructure remains active (Option A: Placeholder)
- Gas section displays with clear messaging about data availability
- Database columns default to 0.00 (no errors)
- Ready for immediate activation when data becomes available
Next Steps:
- Monitor NESO and National Gas for API developments
- Consider contacting providers about publishing operational data
- Explore estimated values approach using total demand + sector ratios
Research Documentation: See GAS_DATA_RESEARCH.md for complete research findings, technical details, and recommendations.
Gas consumption is tracked separately from gas-fired electricity generation (CCGT/OCGT) to prevent double-counting.
PHP class: GasConsumption (infrastructure complete, awaiting data source)
Battery storage data isn’t yet shown. The Elexon Insights Solution only reports on discharging of battery storage systems. Without charging being reported, this would lead to double-counting of generation.
I’m not currently planning any other major changes. I believe it’s better for a project like this to have a limited scope and a concise interface serving the general public than to attempt to offer specialised analysis for energy industry experts.