Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

* Add Readme information
* Added functionality to extract state durations and filter alerts.
* Energy metrics updated to METRIC_AC_ACTIVE_ENERGY_CONSUMED and METRIC_AC_ACTIVE_ENERGY_DELIVERED for independent tracking of consumption and production.

## Bug Fixes

Expand Down
56 changes: 38 additions & 18 deletions src/frequenz/reporting/_reporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,26 +98,46 @@ async def cumulative_energy(
production += last_value_production

else:
# Directly use energy values if using AC_ACTIVE_ENERGY
consumption = sum(
m2.value - m1.value
for m1, m2 in zip(metric_samples, metric_samples[1:])
if m2.value - m1.value > 0
)
production = sum(
m2.value - m1.value
for m1, m2 in zip(metric_samples, metric_samples[1:])
if m2.value - m1.value < 0
# Fetch energy consumption and production metrics separately
consumption_samples = [
sample
async for sample in client.list_microgrid_components_data(
microgrid_components=[(microgrid_id, [component_id])],
metrics=Metric.AC_ACTIVE_ENERGY_CONSUMED,
start_dt=start_time,
end_dt=end_time,
resampling_period=resampling_period,
)
]

production_samples = [
sample
async for sample in client.list_microgrid_components_data(
microgrid_components=[(microgrid_id, [component_id])],
metrics=Metric.AC_ACTIVE_ENERGY_DELIVERED,
start_dt=start_time,
end_dt=end_time,
resampling_period=resampling_period,
)
]

consumption = (
sum(
max(0, m2.value - m1.value)
for m1, m2 in zip(consumption_samples, consumption_samples[1:])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should check that the diff is positive, because the energy can reset in between. Same for production

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I use something like max(0, m2.value - m1.value) to set it to 0 in case there is a reset?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's an option, or add a if m2.value > m1.value in the next line (same as for the power case).

)
if len(consumption_samples) > 1
else float("nan")
)

if len(metric_samples) > 1:
last_value_diff = metric_samples[-1].value - metric_samples[-2].value
if last_value_diff > 0:
consumption += last_value_diff
elif last_value_diff < 0:
production += last_value_diff
else:
consumption = production = float("nan")
production = (
sum(
max(0, m2.value - m1.value)
for m1, m2 in zip(production_samples, production_samples[1:])
)
if len(production_samples) > 1
else float("nan")
)

return CumulativeEnergy(
start_time=start_time,
Expand Down
Loading