Skip to content

Commit

Permalink
Added long term wind and solar forecasts
Browse files Browse the repository at this point in the history
  • Loading branch information
JRascagneres committed Dec 14, 2023
1 parent af398c4 commit 7bb2b3d
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 46 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,34 @@ forecast:
...
```

### Embedded Wind Forecast Fourteen Day
Two-hourly long term wind forecast - From now to fourteen days ahead

Name - Embedded Wind Forecast Fourteen Day\
ID - sensor.national_grid_embedded_wind_forecast_fourteen_day\
State - None\
Attributes:
```
forecast:
- start_time: ...
generation: ...
...
```

### Solar Forecast Seven To Fourteen Day Entity
Two-hourly long term solar forecast - From now to fourteen days ahead

Name - Embedded Solar Forecast Fourteen Day\
ID - sensor.national_grid_fourteen_day_solar_forecast\
State - None\
Attributes:
```
forecast:
- start_time: ...
generation: ...
...
```

## Uses / Examples
This section outlines some graphs / views I have personally created with the data provided by the integration.

Expand Down
79 changes: 79 additions & 0 deletions custom_components/national_grid/coordinators/national_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ def get_data(

three_day, fourteen_day = get_long_term_wind_forecast_eso_data(today_full)

solar, wind = get_long_term_embedded_wind_and_solar_forecast(today_full)

return NationalGridData(
sell_price=current_price,
carbon_intensity=carbon_intensity,
Expand All @@ -133,6 +135,8 @@ def get_data(
now_to_three_wind_forecast=three_day,
fourteen_wind_forecast=fourteen_day,
solar_forecast=solar_forecast,
fourteen_embedded_solar=solar,
fourteen_embedded_wind=wind,
grid_generation=grid_generation,
total_demand_mwh=total_demand_mwh,
total_transfers_mwh=total_transfers_mwh,
Expand Down Expand Up @@ -464,6 +468,81 @@ def get_long_term_wind_forecast_eso_data(
return (three_day, fourteen_day)


def get_long_term_embedded_wind_and_solar_forecast(
now: datetime,
) -> (NationalGridSolarForecast, NationalGridWindForecast):
url = "https://api.nationalgrideso.com/api/3/action/datastore_search?resource_id=db6c038f-98af-4570-ab60-24d71ebd0ae5&limit=32000"
response = requests.get(url, timeout=20)
data = json.loads(response.content)

nearest_30_minutes = now + (now.min.replace(tzinfo=now.tzinfo) - now) % timedelta(
minutes=30
)
in_fourteen_days = nearest_30_minutes + timedelta(days=14)

solar_forecast = []
wind_forecast = []

current_solar_forecast = 0
current_wind_forecast = 0

all_records = data["result"]["records"]
for record in all_records:
formatted_datetime = datetime.strptime(
record["SETTLEMENT_DATE"], "%Y-%m-%dT%H:%M:%S"
).replace(tzinfo=tz.UTC)

formatted_datetime = formatted_datetime + timedelta(
minutes=30 * (int(record["SETTLEMENT_PERIOD"]) - 1)
)

solar_forecast_val = int(record["EMBEDDED_SOLAR_FORECAST"])
wind_forecast_val = int(record["EMBEDDED_WIND_FORECAST"])

if formatted_datetime == nearest_30_minutes:
current_solar_forecast = solar_forecast_val
current_wind_forecast = wind_forecast_val

if (
formatted_datetime >= nearest_30_minutes
and formatted_datetime <= in_fourteen_days
and (
hour_minute_check(formatted_datetime, 0, 0)
or hour_minute_check(formatted_datetime, 2, 0)
or hour_minute_check(formatted_datetime, 4, 0)
or hour_minute_check(formatted_datetime, 6, 0)
or hour_minute_check(formatted_datetime, 8, 0)
or hour_minute_check(formatted_datetime, 10, 0)
or hour_minute_check(formatted_datetime, 12, 0)
or hour_minute_check(formatted_datetime, 14, 0)
or hour_minute_check(formatted_datetime, 16, 0)
or hour_minute_check(formatted_datetime, 18, 0)
or hour_minute_check(formatted_datetime, 20, 0)
or hour_minute_check(formatted_datetime, 22, 0)
)
):
solar_forecast.append(
NationalGridSolarForecastItem(
start_time=formatted_datetime, generation=solar_forecast_val
)
)

wind_forecast.append(
NationalGridWindForecastItem(
start_time=formatted_datetime, generation=wind_forecast_val
)
)

solar = NationalGridSolarForecast(
current_value=current_solar_forecast, forecast=solar_forecast
)
wind = NationalGridWindForecast(
current_value=current_wind_forecast, forecast=wind_forecast
)

return (solar, wind)


def get_carbon_intensity(now_utc_full: datetime) -> int:
formatted_datetime = now_utc_full.strftime("%Y-%m-%dT%H:%MZ")
url = (
Expand Down
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.31",
"version": "0.0.32",
"config_flow": true
}
2 changes: 2 additions & 0 deletions custom_components/national_grid/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ class NationalGridData(TypedDict):
now_to_three_wind_forecast: NationalGridWindForecastLongTerm
fourteen_wind_forecast: NationalGridWindForecastLongTerm
solar_forecast: NationalGridSolarForecast
fourteen_embedded_solar: NationalGridSolarForecast
fourteen_embedded_wind: NationalGridWindForecast
grid_generation: NationalGridGeneration
total_demand_mwh: int
total_transfers_mwh: int
Loading

0 comments on commit 7bb2b3d

Please sign in to comment.