From adf69773ec364951fcb63aff2999c2293408b4b7 Mon Sep 17 00:00:00 2001 From: Jacques Rascagneres Date: Sat, 16 Dec 2023 04:37:15 +0000 Subject: [PATCH] Added thee day embedded solar + wind --- README.md | 48 +++++++++++++++++-- .../coordinators/national_grid.py | 45 +++++++++++++++-- custom_components/national_grid/manifest.json | 2 +- custom_components/national_grid/models.py | 2 + custom_components/national_grid/sensor.py | 18 +++++++ 5 files changed, 108 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 4b4fc15..d9c1e87 100644 --- a/README.md +++ b/README.md @@ -113,6 +113,20 @@ other_percentage_generation grid_collection_time ``` +### Day Ahead Solar Forecast +Thirty minute day ahead solar forecast + +Name - Solar Forecast\ +ID - sensor.national_grid_solar_forecast\ +State - Current forecast value\ +Attributes: +``` +forecast: + - start_time: ... + generation: ... +... +``` + ### Wind Forecast Entity Hourly forecast from 20:00 (UTC) on the current day to 20:00 (GMT) on day + 2 @@ -156,7 +170,7 @@ forecast: ... ``` -### Wind Forecast Seven To Fourteen Day Entity +### Wind Forecast Fourteen Day Entity Two-hourly long term wind forecast - From now to fourteen days ahead Name - Wind Forecast Fourteen Day\ @@ -170,6 +184,20 @@ forecast: ... ``` +### Embedded Wind Forecast Three Day +Thirty minute long term wind forecast - From now to three days ahead + +Name - Embedded Wind Forecast Three Day\ +ID - sensor.national_grid_embedded_wind_forecast_three_day\ +State - None\ +Attributes: +``` +forecast: + - start_time: ... + generation: ... +... +``` + ### Embedded Wind Forecast Fourteen Day Two-hourly long term wind forecast - From now to fourteen days ahead @@ -184,11 +212,25 @@ forecast: ... ``` -### Solar Forecast Seven To Fourteen Day Entity +### Embedded Solar Forecast Three Day +Thirty minute long term wind forecast - From now to three days ahead + +Name - Embedded Solar Forecast Three Day\ +ID - sensor.national_grid_embedded_solar_forecast_three_day\ +State - None\ +Attributes: +``` +forecast: + - start_time: ... + generation: ... +... +``` + +### Embedded 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\ +ID - sensor.national_grid_embedded_solar_forecast_fourteen_day\ State - None\ Attributes: ``` diff --git a/custom_components/national_grid/coordinators/national_grid.py b/custom_components/national_grid/coordinators/national_grid.py index bdbec5f..b5450af 100644 --- a/custom_components/national_grid/coordinators/national_grid.py +++ b/custom_components/national_grid/coordinators/national_grid.py @@ -124,7 +124,12 @@ 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) + ( + three_day_solar, + solar, + three_day_wind, + wind, + ) = get_long_term_embedded_wind_and_solar_forecast(today_full) return NationalGridData( sell_price=current_price, @@ -136,7 +141,9 @@ def get_data( fourteen_wind_forecast=fourteen_day, solar_forecast=solar_forecast, fourteen_embedded_solar=solar, + three_embedded_solar=three_day_solar, fourteen_embedded_wind=wind, + three_embedded_wind=three_day_wind, grid_generation=grid_generation, total_demand_mwh=total_demand_mwh, total_transfers_mwh=total_transfers_mwh, @@ -470,7 +477,12 @@ def get_long_term_wind_forecast_eso_data( def get_long_term_embedded_wind_and_solar_forecast( now: datetime, -) -> (NationalGridSolarForecast, NationalGridWindForecast): +) -> ( + NationalGridSolarForecast, + NationalGridSolarForecast, + NationalGridWindForecast, + 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) @@ -478,8 +490,12 @@ def get_long_term_embedded_wind_and_solar_forecast( nearest_30_minutes = now + (now.min.replace(tzinfo=now.tzinfo) - now) % timedelta( minutes=30 ) + in_three_days = nearest_30_minutes + timedelta(days=3) in_fourteen_days = nearest_30_minutes + timedelta(days=14) + three_day_solar_forecast = [] + three_day_wind_forecast = [] + solar_forecast = [] wind_forecast = [] @@ -503,6 +519,22 @@ def get_long_term_embedded_wind_and_solar_forecast( current_solar_forecast = solar_forecast_val current_wind_forecast = wind_forecast_val + if ( + formatted_datetime >= nearest_30_minutes + and formatted_datetime <= in_three_days + ): + three_day_solar_forecast.append( + NationalGridSolarForecastItem( + start_time=formatted_datetime, generation=solar_forecast_val + ) + ) + + three_day_wind_forecast.append( + NationalGridWindForecastItem( + start_time=formatted_datetime, generation=wind_forecast_val + ) + ) + if ( formatted_datetime >= nearest_30_minutes and formatted_datetime <= in_fourteen_days @@ -533,6 +565,13 @@ def get_long_term_embedded_wind_and_solar_forecast( ) ) + three_day_solar = NationalGridSolarForecast( + current_value=current_solar_forecast, forecast=three_day_solar_forecast + ) + three_day_wind = NationalGridWindForecast( + current_value=current_wind_forecast, forecast=three_day_wind_forecast + ) + solar = NationalGridSolarForecast( current_value=current_solar_forecast, forecast=solar_forecast ) @@ -540,7 +579,7 @@ def get_long_term_embedded_wind_and_solar_forecast( current_value=current_wind_forecast, forecast=wind_forecast ) - return (solar, wind) + return (three_day_solar, solar, three_day_wind, wind) def get_carbon_intensity(now_utc_full: datetime) -> int: diff --git a/custom_components/national_grid/manifest.json b/custom_components/national_grid/manifest.json index 108e285..248541f 100644 --- a/custom_components/national_grid/manifest.json +++ b/custom_components/national_grid/manifest.json @@ -7,6 +7,6 @@ "dependencies": [], "iot_class": "cloud_polling", "requirements": [], - "version": "0.0.32", + "version": "0.0.33", "config_flow": true } \ No newline at end of file diff --git a/custom_components/national_grid/models.py b/custom_components/national_grid/models.py index a813704..a3f204b 100644 --- a/custom_components/national_grid/models.py +++ b/custom_components/national_grid/models.py @@ -69,7 +69,9 @@ class NationalGridData(TypedDict): now_to_three_wind_forecast: NationalGridWindForecastLongTerm fourteen_wind_forecast: NationalGridWindForecastLongTerm solar_forecast: NationalGridSolarForecast + three_embedded_solar: NationalGridSolarForecast fourteen_embedded_solar: NationalGridSolarForecast + three_embedded_wind: NationalGridWindForecast fourteen_embedded_wind: NationalGridWindForecast grid_generation: NationalGridGeneration total_demand_mwh: int diff --git a/custom_components/national_grid/sensor.py b/custom_components/national_grid/sensor.py index 7132fb8..36be814 100644 --- a/custom_components/national_grid/sensor.py +++ b/custom_components/national_grid/sensor.py @@ -300,6 +300,15 @@ class NationalGridSensorEntityDescription(SensorEntityDescription): state_class=SensorStateClass.MEASUREMENT, extra_attributes_key="fourteen_embedded_solar", ), + NationalGridSensorEntityDescription( + key="three_embedded_solar.current_value", + name="Embedded Solar Forecast Three Day", + unique_id="three_day_embedded_solar", + native_unit_of_measurement="MW", + icon="mdi:solar-power-variant", + state_class=SensorStateClass.MEASUREMENT, + extra_attributes_key="three_embedded_solar", + ), NationalGridSensorEntityDescription( key="fourteen_embedded_wind.current_value", name="Embedded Wind Forecast Fourteen Day", @@ -309,6 +318,15 @@ class NationalGridSensorEntityDescription(SensorEntityDescription): state_class=SensorStateClass.MEASUREMENT, extra_attributes_key="fourteen_embedded_wind", ), + NationalGridSensorEntityDescription( + key="three_embedded_wind.current_value", + name="Embedded Wind Forecast Three Day", + unique_id="three_day_embedded_wind", + native_unit_of_measurement="MW", + icon="mdi:wind-turbine", + state_class=SensorStateClass.MEASUREMENT, + extra_attributes_key="three_embedded_wind", + ), NationalGridSensorEntityDescription( key=None, name="Grid Generation",