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 376e5b4
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 1 deletion.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ 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 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 +103,9 @@ biomass_mwh
belgium_mwh
norway_mwh
total_generation_mwh
fossil_fuel_percentage_generation
renewable_percentage_generation
other_percentage_generation
grid_collection_time
```

Expand Down
34 changes: 34 additions & 0 deletions custom_components/national_grid/coordinators/national_grid.py
Original file line number Diff line number Diff line change
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 @@ -534,6 +537,33 @@ 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["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 +657,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
}
3 changes: 3 additions & 0 deletions custom_components/national_grid/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ 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
other_percentage_generation: int # Counts nuclear, biomass
grid_collection_time: datetime


Expand Down
21 changes: 21 additions & 0 deletions custom_components/national_grid/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,27 @@ 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.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 376e5b4

Please sign in to comment.