Skip to content

Commit a075969

Browse files
Add helper function to calculate energy metric
Signed-off-by: Flora <[email protected]>
1 parent de6439d commit a075969

File tree

3 files changed

+103
-2
lines changed

3 files changed

+103
-2
lines changed

RELEASE_NOTES.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
## New Features
1212

13-
<!-- Here goes the main new features and examples or instructions on how to use them -->
13+
* Add cumulative_energy calculation to read out energy consumption and production over a specified time range.
1414

1515
## Bug Fixes
1616

pyproject.toml

+3-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ classifiers = [
2626
]
2727
requires-python = ">= 3.11, < 4"
2828
dependencies = [
29-
"typing-extensions >= 4.5.0, < 5",
29+
"typing-extensions >= 4.6.1, < 5",
30+
"frequenz-client-reporting>=0.8.0, < 1",
31+
"frequenz-client-common >= 0.2.0, < 0.3",
3032
]
3133
dynamic = ["version"]
3234

src/frequenz/reporting/_reporting.py

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# License: MIT
2+
# Copyright © 2024 Frequenz Energy-as-a-Service GmbH
3+
4+
"""A highlevel interface for the reporting API."""
5+
6+
from collections import namedtuple
7+
from datetime import datetime
8+
9+
from frequenz.client.common.metric import Metric
10+
from frequenz.client.reporting import ReportingApiClient
11+
12+
CumulativeEnergy = namedtuple(
13+
"CumulativeEnergy", ["start_time", "end_time", "consumption", "production"]
14+
)
15+
"""Type for cumulative energy consumption and production over a specified time."""
16+
17+
18+
# pylint: disable=too-many-arguments
19+
async def cumulative_energy(
20+
client: ReportingApiClient,
21+
microgrid_id: int,
22+
component_id: int,
23+
start_time: datetime,
24+
end_time: datetime,
25+
use_active_power: bool,
26+
resolution: int | None = None,
27+
) -> CumulativeEnergy:
28+
"""
29+
Calculate the cumulative energy consumption and production over a specified time range.
30+
31+
Args:
32+
client: The client used to fetch the metric samples from the Reporting API.
33+
microgrid_id: The ID of the microgrid.
34+
component_id: The ID of the component within the microgrid.
35+
start_time: The start date and time for the period.
36+
end_time: The end date and time for the period.
37+
use_active_power: If True, use the 'AC_ACTIVE_POWER' metric.
38+
If False, use the 'AC_ACTIVE_ENERGY' metric.
39+
resolution: The resampling resolution for the data, represented in seconds.
40+
If None, no resampling is applied.
41+
42+
Returns:
43+
EnergyMetric: A named tuple with start_time, end_time, consumption, and production
44+
in Wh.
45+
"""
46+
metric = Metric.AC_ACTIVE_POWER if use_active_power else Metric.AC_ACTIVE_ENERGY
47+
48+
metric_samples = [
49+
sample
50+
async for sample in client.list_microgrid_components_data(
51+
microgrid_components=[(microgrid_id, [component_id])],
52+
metrics=metric,
53+
start_dt=start_time,
54+
end_dt=end_time,
55+
resolution=resolution,
56+
)
57+
]
58+
59+
if metric_samples:
60+
if use_active_power:
61+
# Convert power to energy if using AC_ACTIVE_POWER
62+
consumption = (
63+
sum(
64+
m1.value * (m2.timestamp - m1.timestamp).total_seconds()
65+
for m1, m2 in zip(metric_samples, metric_samples[1:])
66+
if m1.value > 0
67+
)
68+
/ 3600.0
69+
) # Convert seconds to hours
70+
71+
production = (
72+
sum(
73+
m1.value * (m2.timestamp - m1.timestamp).total_seconds()
74+
for m1, m2 in zip(metric_samples, metric_samples[1:])
75+
if m1.value < 0
76+
)
77+
/ 3600.0
78+
)
79+
else:
80+
# Directly use energy values if using AC_ACTIVE_ENERGY
81+
consumption = sum(
82+
m2.value - m1.value
83+
for m1, m2 in zip(metric_samples, metric_samples[1:])
84+
if m1.value > 0
85+
)
86+
production = sum(
87+
m2.value - m1.value
88+
for m1, m2 in zip(metric_samples, metric_samples[1:])
89+
if m1.value < 0
90+
)
91+
else:
92+
consumption = production = 0.0
93+
94+
return CumulativeEnergy(
95+
start_time=start_time,
96+
end_time=end_time,
97+
consumption=consumption,
98+
production=production,
99+
)

0 commit comments

Comments
 (0)