Skip to content

Commit

Permalink
refactored the average
Browse files Browse the repository at this point in the history
  • Loading branch information
Pluimvee committed Oct 9, 2024
1 parent a452117 commit 22fff29
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 62 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__pycache__/
15 changes: 6 additions & 9 deletions custom_components/entsoe/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,19 +165,16 @@ def process_PT15M_points(self, period: Element, start_time: datetime):
# now calculate hourly averages based on available points
data = {}
last_position = max(positions.keys())
last_price = positions.get(0, 0)

for hour in range((last_position // 4) + 1):
sum_prices = 0
count = 0
for idx in range(hour * 4 + 1, hour * 4 + 5):
if idx in positions:
sum_prices += positions[idx]
count += 1

if count > 0: # only calculate average if there are prices
avg_price = sum_prices / count
time = start_time + timedelta(hours=hour)
data[time] = avg_price
last_price = positions.get(idx, last_price)
sum_prices += last_price

time = start_time + timedelta(hours=hour)
data[time] = round(sum_prices / 4, 2)

return data

Expand Down
72 changes: 72 additions & 0 deletions custom_components/entsoe/test/datasets/BE_15M_avg.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?xml version="1.0" encoding="utf-8"?>
<Publication_MarketDocument xmlns="urn:iec62325.351:tc57wg16:451-3:publicationdocument:7:3">
<mRID>64e2af3a87c2404cbea80edc067a1b6f</mRID>
<revisionNumber>1</revisionNumber>
<type>A44</type>
<sender_MarketParticipant.mRID codingScheme="A01">10X1001A1001A450</sender_MarketParticipant.mRID>
<sender_MarketParticipant.marketRole.type>A32</sender_MarketParticipant.marketRole.type>
<receiver_MarketParticipant.mRID codingScheme="A01">10X1001A1001A450</receiver_MarketParticipant.mRID>
<receiver_MarketParticipant.marketRole.type>A33</receiver_MarketParticipant.marketRole.type>
<createdDateTime>2024-10-07T14:36:40Z</createdDateTime>
<period.timeInterval>
<start>2024-10-05T22:00Z</start>
<end>2024-10-06T22:00Z</end>
</period.timeInterval>
<TimeSeries>
<mRID>1</mRID>
<auction.type>A01</auction.type>
<businessType>A62</businessType>
<in_Domain.mRID codingScheme="A01">10YBE----------2</in_Domain.mRID>
<out_Domain.mRID codingScheme="A01">10YBE----------2</out_Domain.mRID>
<contract_MarketAgreement.type>A01</contract_MarketAgreement.type>
<currency_Unit.name>EUR</currency_Unit.name>
<price_Measure_Unit.name>MWH</price_Measure_Unit.name>
<curveType>A03</curveType>
<Period>
<timeInterval>
<start>2024-10-05T22:00Z</start>
<end>2024-10-06T03:00Z</end>
</timeInterval>
<resolution>PT15M</resolution>
<Point>
<position>1</position>
<price.amount>55.35</price.amount>
</Point>
<Point>
<position>5</position>
<price.amount>44.22</price.amount>
</Point>
<Point>
<position>2</position>
<price.amount>40.32</price.amount>
</Point>
<Point>
<position>3</position>
<price.amount>31.86</price.amount>
</Point>
<Point>
<position>11</position>
<price.amount>28.37</price.amount>
</Point>
<Point>
<position>4</position>
<price.amount>28.71</price.amount>
</Point>
</Period>
<Period>
<timeInterval>
<start>2024-10-06T03:00Z</start>
<end>2024-10-06T05:00Z</end>
</timeInterval>
<resolution>PT60M</resolution>
<Point>
<position>1</position>
<price.amount>64.98</price.amount>
</Point>
<Point>
<position>3</position>
<price.amount>57.86</price.amount>
</Point>
</Period>
</TimeSeries>
</Publication_MarketDocument>
65 changes: 12 additions & 53 deletions custom_components/entsoe/test/test_api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def test_be_60m(self):
with open(".\\datasets\\BE_60M.xml") as f:
data = f.read()

self.maxDiff = None
self.assertDictEqual(
self.client.parse_price_document(data),
{
Expand Down Expand Up @@ -138,66 +139,24 @@ def test_be_60m_15m_mix(self):
},
)

def test_de_60m_15m_overlap(self):
with open("./datasets/DE_60M_15M_overlap.xml") as f:
def test_be_15M_avg(self):
with open("./datasets/BE_15M_avg.xml") as f:
data = f.read()

self.maxDiff = None
self.assertDictEqual(
self.client.parse_price_document(data),
{
# part 1 - 60M resolution
datetime.fromisoformat("2024-10-05T22:00:00Z"): 67.04,
datetime.fromisoformat("2024-10-05T23:00:00Z"): 63.97,
datetime.fromisoformat("2024-10-06T00:00:00Z"): 62.83,
datetime.fromisoformat("2024-10-06T01:00:00Z"): 63.35,
datetime.fromisoformat("2024-10-06T02:00:00Z"): 62.71,
datetime.fromisoformat("2024-10-06T03:00:00Z"): 63.97,
datetime.fromisoformat("2024-10-06T04:00:00Z"): 63.41,
datetime.fromisoformat("2024-10-06T05:00:00Z"): 72.81,
datetime.fromisoformat("2024-10-06T06:00:00Z"): 77.2,
datetime.fromisoformat("2024-10-06T07:00:00Z"): 66.06,
datetime.fromisoformat("2024-10-06T08:00:00Z"): 35.28,
datetime.fromisoformat("2024-10-06T09:00:00Z"): 16.68,
datetime.fromisoformat("2024-10-06T10:00:00Z"): 5.25,
datetime.fromisoformat("2024-10-06T11:00:00Z"): -0.01,
datetime.fromisoformat(
"2024-10-06T12:00:00Z"
): -0.01, # repeated value, not present in the dataset!
datetime.fromisoformat("2024-10-06T13:00:00Z"): 0.2,
datetime.fromisoformat("2024-10-06T14:00:00Z"): 59.6,
datetime.fromisoformat("2024-10-06T15:00:00Z"): 90.94,
datetime.fromisoformat("2024-10-06T16:00:00Z"): 106.3,
datetime.fromisoformat("2024-10-06T17:00:00Z"): 97.22,
datetime.fromisoformat("2024-10-06T18:00:00Z"): 72.98,
datetime.fromisoformat("2024-10-06T19:00:00Z"): 59.37,
datetime.fromisoformat("2024-10-06T20:00:00Z"): 58.69,
datetime.fromisoformat("2024-10-06T21:00:00Z"): 51.71,
# part 1 - 15M resolution
datetime.fromisoformat("2024-10-05T22:00:00Z"): 39.06, # average on 4
datetime.fromisoformat("2024-10-05T23:00:00Z"): 44.22, # average on 1
datetime.fromisoformat("2024-10-06T00:00:00Z"): 36.30, # average on 2
datetime.fromisoformat("2024-10-06T01:00:00Z"): 36.30, # extended till
datetime.fromisoformat("2024-10-06T02:00:00Z"): 36.30, # extended till
# part 2 - 60M resolution
datetime.fromisoformat("2024-10-06T22:00:00Z"): 34.58,
datetime.fromisoformat("2024-10-06T23:00:00Z"): 35.34,
datetime.fromisoformat("2024-10-07T00:00:00Z"): 33.25,
datetime.fromisoformat("2024-10-07T01:00:00Z"): 30.15,
datetime.fromisoformat("2024-10-07T02:00:00Z"): 36.09,
datetime.fromisoformat("2024-10-07T03:00:00Z"): 46.73,
datetime.fromisoformat("2024-10-07T04:00:00Z"): 67.59,
datetime.fromisoformat("2024-10-07T05:00:00Z"): 100.92,
datetime.fromisoformat("2024-10-07T06:00:00Z"): 108.32,
datetime.fromisoformat("2024-10-07T07:00:00Z"): 91.86,
datetime.fromisoformat("2024-10-07T08:00:00Z"): 66.09,
datetime.fromisoformat("2024-10-07T09:00:00Z"): 60.22,
datetime.fromisoformat("2024-10-07T10:00:00Z"): 54.11,
datetime.fromisoformat("2024-10-07T11:00:00Z"): 43.29,
datetime.fromisoformat("2024-10-07T12:00:00Z"): 55,
datetime.fromisoformat("2024-10-07T13:00:00Z"): 67.01,
datetime.fromisoformat("2024-10-07T14:00:00Z"): 97.9,
datetime.fromisoformat("2024-10-07T15:00:00Z"): 120.71,
datetime.fromisoformat("2024-10-07T16:00:00Z"): 237.65,
datetime.fromisoformat("2024-10-07T17:00:00Z"): 229.53,
datetime.fromisoformat("2024-10-07T18:00:00Z"): 121.98,
datetime.fromisoformat("2024-10-07T19:00:00Z"): 99.93,
datetime.fromisoformat("2024-10-07T20:00:00Z"): 91.91,
datetime.fromisoformat("2024-10-07T21:00:00Z"): 79.12,
datetime.fromisoformat("2024-10-06T03:00:00Z"): 64.98,
datetime.fromisoformat("2024-10-06T04:00:00Z"): 64.98, # extended
datetime.fromisoformat("2024-10-06T05:00:00Z"): 57.86,
},
)

Expand Down

0 comments on commit 22fff29

Please sign in to comment.