mirror of
				https://github.com/Akkudoktor-EOS/EOS.git
				synced 2025-11-04 08:46:20 +00:00 
			
		
		
		
	Fix2 electricity price prediction. (#296)
Normalize electricity price prediction to €/Wh. Provide electricity price prediction by €/kWh for convenience. Allow to configure electricity price charges by €/kWh. Also added error page to fastapi rest server to get rid of annoying unrelated fault messages during testing. Signed-off-by: Bobby Noelte <b0661n0e17e@gmail.com>
This commit is contained in:
		@@ -9,6 +9,6 @@ class ElecPriceCommonSettings(SettingsBaseModel):
 | 
			
		||||
    elecprice_provider: Optional[str] = Field(
 | 
			
		||||
        default=None, description="Electricity price provider id of provider to be used."
 | 
			
		||||
    )
 | 
			
		||||
    elecprice_charges: Optional[float] = Field(
 | 
			
		||||
    elecprice_charges_kwh: Optional[float] = Field(
 | 
			
		||||
        default=None, ge=0, description="Electricity price charges (€/kWh)."
 | 
			
		||||
    )
 | 
			
		||||
 
 | 
			
		||||
@@ -7,7 +7,7 @@ Notes:
 | 
			
		||||
from abc import abstractmethod
 | 
			
		||||
from typing import List, Optional
 | 
			
		||||
 | 
			
		||||
from pydantic import Field
 | 
			
		||||
from pydantic import Field, computed_field
 | 
			
		||||
 | 
			
		||||
from akkudoktoreos.prediction.predictionabc import PredictionProvider, PredictionRecord
 | 
			
		||||
from akkudoktoreos.utils.logutil import get_logger
 | 
			
		||||
@@ -23,10 +23,22 @@ class ElecPriceDataRecord(PredictionRecord):
 | 
			
		||||
 | 
			
		||||
    """
 | 
			
		||||
 | 
			
		||||
    elecprice_marketprice: Optional[float] = Field(
 | 
			
		||||
        None, description="Electricity market price (€/KWh)"
 | 
			
		||||
    elecprice_marketprice_wh: Optional[float] = Field(
 | 
			
		||||
        None, description="Electricity market price per Wh (€/Wh)"
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
    # Computed fields
 | 
			
		||||
    @computed_field  # type: ignore[prop-decorator]
 | 
			
		||||
    @property
 | 
			
		||||
    def elecprice_marketprice_kwh(self) -> Optional[float]:
 | 
			
		||||
        """Electricity market price per kWh (€/kWh).
 | 
			
		||||
 | 
			
		||||
        Convenience attribute calculated from `elecprice_marketprice_wh`.
 | 
			
		||||
        """
 | 
			
		||||
        if self.elecprice_marketprice_wh is None:
 | 
			
		||||
            return None
 | 
			
		||||
        return self.elecprice_marketprice_wh * 1000.0
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class ElecPriceProvider(PredictionProvider):
 | 
			
		||||
    """Abstract base class for electricity price providers.
 | 
			
		||||
 
 | 
			
		||||
@@ -134,7 +134,7 @@ class ElecPriceAkkudoktor(ElecPriceProvider):
 | 
			
		||||
        prices_of_hour = self.elecprice_8days[hour]
 | 
			
		||||
        if np.isnan(prices_of_hour).all():
 | 
			
		||||
            # No prediction prices available for this hour - use mean value of all prices
 | 
			
		||||
            price_weighted_mean = np.nanmean(self.elecprice_marketprice_8day)
 | 
			
		||||
            price_weighted_mean = np.nanmean(self.elecprice_marketprice_wh_8day)
 | 
			
		||||
        else:
 | 
			
		||||
            weights = self.elecprice_8days_weights_day_of_week[day_of_week]
 | 
			
		||||
            prices_of_hour_masked: NDArray[Shape["24"]] = np.ma.MaskedArray(
 | 
			
		||||
@@ -204,24 +204,28 @@ class ElecPriceAkkudoktor(ElecPriceProvider):
 | 
			
		||||
        elecprice_cache_file.seek(0)
 | 
			
		||||
        self.elecprice_8days = np.load(elecprice_cache_file)
 | 
			
		||||
 | 
			
		||||
        # Get elecprice_charges
 | 
			
		||||
        charges = self.config.elecprice_charges if self.config.elecprice_charges else 0.0
 | 
			
		||||
        # Get elecprice_charges_kwh_kwh
 | 
			
		||||
        charges_kwh = (
 | 
			
		||||
            self.config.elecprice_charges_kwh if self.config.elecprice_charges_kwh else 0.0
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
        for i in range(values_len):
 | 
			
		||||
            original_datetime = akkudoktor_data.values[i].start
 | 
			
		||||
            dt = to_datetime(original_datetime, in_timezone=self.config.timezone)
 | 
			
		||||
            akkudoktor_value = akkudoktor_data.values[i]
 | 
			
		||||
            price = akkudoktor_value.marketpriceEurocentPerKWh / 100 + charges
 | 
			
		||||
            price_wh = (
 | 
			
		||||
                akkudoktor_value.marketpriceEurocentPerKWh / (100 * 1000) + charges_kwh / 1000
 | 
			
		||||
            )
 | 
			
		||||
 | 
			
		||||
            if compare_datetimes(dt, self.start_datetime).lt:
 | 
			
		||||
                # forecast data is too old
 | 
			
		||||
                self.elecprice_8days[dt.hour, dt.day_of_week] = price
 | 
			
		||||
                self.elecprice_8days[dt.hour, dt.day_of_week] = price_wh
 | 
			
		||||
                continue
 | 
			
		||||
            self.elecprice_8days[dt.hour, 7] = price
 | 
			
		||||
            self.elecprice_8days[dt.hour, 7] = price_wh
 | 
			
		||||
 | 
			
		||||
            record = ElecPriceDataRecord(
 | 
			
		||||
                date_time=dt,
 | 
			
		||||
                elecprice_marketprice=price,
 | 
			
		||||
                elecprice_marketprice_wh=price_wh,
 | 
			
		||||
            )
 | 
			
		||||
            self.append(record)
 | 
			
		||||
 | 
			
		||||
@@ -242,7 +246,7 @@ class ElecPriceAkkudoktor(ElecPriceProvider):
 | 
			
		||||
 | 
			
		||||
            record = ElecPriceDataRecord(
 | 
			
		||||
                date_time=dt,
 | 
			
		||||
                elecprice_marketprice=value,
 | 
			
		||||
                elecprice_marketprice_wh=value,
 | 
			
		||||
            )
 | 
			
		||||
            self.insert(0, record)
 | 
			
		||||
        # Assure price ends at end_time
 | 
			
		||||
@@ -253,6 +257,6 @@ class ElecPriceAkkudoktor(ElecPriceProvider):
 | 
			
		||||
 | 
			
		||||
            record = ElecPriceDataRecord(
 | 
			
		||||
                date_time=dt,
 | 
			
		||||
                elecprice_marketprice=value,
 | 
			
		||||
                elecprice_marketprice_wh=value,
 | 
			
		||||
            )
 | 
			
		||||
            self.append(record)
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user