Skip to content

Commit

Permalink
Grid generation percentages
Browse files Browse the repository at this point in the history
  • Loading branch information
JRascagneres committed Oct 30, 2023
1 parent ece172e commit f91db27
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 4 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ Then follow the same steps that are using to setup most integrations:
| National Grid Total Generation | sensor.total_generation_mwh | Total generation in MWh |
| National Grid Total Demand MWh | sensor.national_grid_total_demand_mwh | Total electricity demand in MWh. This is all generation with the inclusion of interconnectors and storage. |
| National Grid Total Transfers MWh | sensor.national_grid_total_transfers_mwh | Total electricity transfers in MWh. This is all the transfers which are interconnectors and storage. |
National Grid Grid Generation Fossil Fuel Percentage | sensor.national_grid_fossil_fuel_percentage_generation | Percentage of total grid generation that is generated from fossil fuel sources: Gas, Oil & Coal
National Grid Grid Generation Renewable Percentage | sensor.national_grid_renewable_percentage_generation | Percentage of total grid generation that is generated from renewable sources: Solar, Wind & Hydro
National Grid Grid Generation Low Carbon Percentage | sensor.national_grid_low_carbon_percentage_generation | Percentage of total grid generation that is generated from renewable & low carbon sources: Solar, Wind, Hydro & Nuclear
National Grid Grid Generation Other Percentage | sensor.national_grid_other_percentage_generation | Percentage of total grid generation that is generated from 'other' sources: Nuclear, Biomass & Unknown / Other


Note that the associated time sensors are important. Updates can lag by a few minutes and are in UTC so its possible that 'today' and 'tomorrow' aren't entirely accurate for a period of time.

Expand Down Expand Up @@ -99,6 +104,10 @@ biomass_mwh
belgium_mwh
norway_mwh
total_generation_mwh
fossil_fuel_percentage_generation
renewable_percentage_generation
low_carbon_percentage_generation
other_percentage_generation
grid_collection_time
```

Expand Down
50 changes: 47 additions & 3 deletions custom_components/national_grid/coordinators/national_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ def get_hourly_wind_forecast(now_utc: datetime) -> NationalGridWindForecast:
current_generation = int(item["generation"])

if current_generation == 0:
raise UnexpectedDataError
raise UnexpectedDataError("Hourly wind forecast 'current' is 0")

return NationalGridWindForecast(
forecast=wind_forecast, current_value=current_generation
Expand Down Expand Up @@ -256,7 +256,7 @@ def get_hourly_wind_forecast_earliest(now_utc: datetime) -> NationalGridWindFore
current_generation = int(item["generation"])

if current_generation == 0:
raise UnexpectedDataError
raise UnexpectedDataError("Earliest hourly wind forecast 'current' is 0")

return NationalGridWindForecast(
forecast=wind_forecast_earliest, current_value=current_generation
Expand Down Expand Up @@ -449,6 +449,9 @@ def get_generation(utc_now: datetime) -> NationalGridGeneration:
belgium_mwh=0,
norway_mwh=0,
total_generation_mwh=0,
fossil_fuel_percentage_generation=0,
renewable_percentage_generation=0,
other_percentage_generation=0,
grid_collection_time=latest_publish_time,
)

Expand Down Expand Up @@ -497,7 +500,7 @@ def get_generation(utc_now: datetime) -> NationalGridGeneration:
and national_grid_generation["nuclear_mwh"] == 0
and national_grid_generation["hydro_mwh"] == 0
):
raise UnexpectedDataError(url)
raise UnexpectedDataError("Getting generation returned numerous zero values")

return national_grid_generation

Expand Down Expand Up @@ -534,6 +537,43 @@ def get_generation_combined(api_key: str, now_utc_full: datetime, today_utc: str
+ grid_generation["other_mwh"]
)

grid_generation["fossil_fuel_percentage_generation"] = percentage_calc(
(
grid_generation["gas_mwh"]
+ grid_generation["oil_mwh"]
+ grid_generation["coal_mwh"]
),
grid_generation["total_generation_mwh"],
)

grid_generation["renewable_percentage_generation"] = percentage_calc(
(
grid_generation["solar_mwh"]
+ grid_generation["wind_mwh"]
+ grid_generation["hydro_mwh"]
),
grid_generation["total_generation_mwh"],
)

grid_generation["low_carbon_percentage_generation"] = percentage_calc(
(
grid_generation["solar_mwh"]
+ grid_generation["wind_mwh"]
+ grid_generation["hydro_mwh"]
+ grid_generation["nuclear_mwh"]
),
grid_generation["total_generation_mwh"],
)

grid_generation["other_percentage_generation"] = percentage_calc(
(
grid_generation["nuclear_mwh"]
+ grid_generation["biomass_mwh"]
+ grid_generation["other_mwh"]
),
grid_generation["total_generation_mwh"],
)

return grid_generation


Expand Down Expand Up @@ -627,3 +667,7 @@ def obtain_data_with_fallback(current_data, key, func, *args):
except Exception as e: # pylint: disable=broad-except
_LOGGER.exception("Failed to obtain data")
return get_data_if_exists(current_data, key)


def percentage_calc(int_sum, int_total):
return round(int_sum / int_total * 100, 2)
2 changes: 1 addition & 1 deletion custom_components/national_grid/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
"dependencies": [],
"iot_class": "cloud_polling",
"requirements": [],
"version": "0.0.27",
"version": "0.0.28",
"config_flow": true
}
4 changes: 4 additions & 0 deletions custom_components/national_grid/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ class NationalGridGeneration(TypedDict):
belgium_mwh: int # intnem ( Nemo )
norway_mwh: int # intnsl ( North Sea Link )
total_generation_mwh: int # total generation
fossil_fuel_percentage_generation: int # Counts gas, oil, coal
renewable_percentage_generation: int # Counts solar, wind, hydro
low_carbon_percentage_generation: int # Counts renewable, nuclear
other_percentage_generation: int # Counts nuclear, biomass
grid_collection_time: datetime


Expand Down
28 changes: 28 additions & 0 deletions custom_components/national_grid/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,34 @@ class NationalGridSensorEntityDescription(SensorEntityDescription):
icon="mdi:transmission-tower",
extra_attributes_key="grid_generation",
),
NationalGridSensorEntityDescription(
key="grid_generation.fossil_fuel_percentage_generation",
name="Grid Generation Fossil Fuel Percentage",
unique_id="fossil_fuel_percentage_generation",
native_unit_of_measurement="%",
icon="mdi:molecule-co2",
),
NationalGridSensorEntityDescription(
key="grid_generation.renewable_percentage_generation",
name="Grid Generation Renewable Percentage",
unique_id="renewable_percentage_generation",
native_unit_of_measurement="%",
icon="mdi:leaf",
),
NationalGridSensorEntityDescription(
key="grid_generation.low_carbon_percentage_generation",
name="Grid Generation Low Carbon Percentage",
unique_id="low_carbon_percentage_generation",
native_unit_of_measurement="%",
icon="mdi:leaf",
),
NationalGridSensorEntityDescription(
key="grid_generation.other_percentage_generation",
name="Grid Generation Other Percentage",
unique_id="other_percentage_generation",
native_unit_of_measurement="%",
icon="mdi:help",
),
)


Expand Down

0 comments on commit f91db27

Please sign in to comment.