Reporting API client for Python
The following platforms are officially supported (tested):
- Python: 3.11
- Operating System: Ubuntu Linux 20.04
- Architectures: amd64, arm64
If you want to know how to build this project and contribute to it, please check out the Contributing Guide.
Please also refer to examples for more detailed usage.
# Choose the version you want to install
VERSION=0.17.0
pip install frequenz-client-reporting==$VERSION
from datetime import datetime, timedelta
from frequenz.client.common.metric import Metric
from frequenz.client.reporting import ReportingApiClient
# Change server address
SERVER_URL = "grpc://replace-this-with-your-server-url:port"
API_KEY = open('api_key.txt').read().strip()
client = ReportingApiClient(server_url=SERVER_URL, key=API_KEY)
Besides the microgrid_id
, component_id
s, metrics
, start, and end time,
you can also set the sampling period for resampling using the resampling_period
parameter. For example, to resample data every 15 minutes, use
resampling_period=timedelta(minutes=15)
.
data = [
sample async for sample in
client.list_single_component_data(
microgrid_id=1,
component_id=100,
metrics=[Metric.AC_ACTIVE_POWER, Metric.AC_REACTIVE_POWER],
start_time=datetime.fromisoformat("2024-05-01T00:00:00"),
end_time=datetime.fromisoformat("2024-05-02T00:00:00"),
resampling_period=timedelta(seconds=1),
)
]
data = [
sample async for sample in
client.receive_single_sensor_data(
microgrid_id=1,
sensor_id=100,
metrics=[Metric.SENSOR_IRRADIANCE],
start_time=datetime.fromisoformat("2024-05-01T00:00:00"),
end_time=datetime.fromisoformat("2024-05-02T00:00:00"),
resampling_period=timedelta(seconds=1),
)
]
# Set the microgrid ID and the component IDs that belong to the microgrid
# Multiple microgrids and components can be queried at once
microgrid_id1 = 1
component_ids1 = [100, 101, 102]
microgrid_id2 = 2
component_ids2 = [200, 201, 202]
microgrid_components = [
(microgrid_id1, component_ids1),
(microgrid_id2, component_ids2),
]
data = [
sample async for sample in
client.list_microgrid_components_data(
microgrid_components=microgrid_components,
metrics=[Metric.AC_ACTIVE_POWER, Metric.AC_REACTIVE_POWER],
start_time=datetime.fromisoformat("2024-05-01T00:00:00"),
end_time=datetime.fromisoformat("2024-05-02T00:00:00"),
resampling_period=timedelta(seconds=1),
include_states=False, # Set to True to include state data
include_bounds=False, # Set to True to include metric bounds data
)
]
# Set the microgrid ID and the sensor IDs that belong to the microgrid
# Multiple microgrids and sensors can be queried at once
microgrid_id1 = 1
sensor_ids1 = [100, 101, 102]
microgrid_id2 = 2
sensor_ids2 = [200, 201, 202]
microgrid_sensors = [
(microgrid_id1, sensor_ids1),
(microgrid_id2, sensor_ids2),
]
data = [
sample async for sample in
client.receive_microgrid_sensors_data(
microgrid_sensors=microgrid_sensors,
metrics=[Metric.SENSOR_IRRADIANCE],
start_time=datetime.fromisoformat("2024-05-01T00:00:00"),
end_time=datetime.fromisoformat("2024-05-02T00:00:00"),
resampling_period=timedelta(seconds=1),
include_states=False, # Set to True to include state data
)
]
import pandas as pd
df = pd.DataFrame(data)
print(df)
The package contains a command-line tool that can be used to request microgrid component data from the reporting API.
reporting-cli \
--url localhost:4711 \
--key=$(<api_key.txt)
--mid 42 \
--cid 23 \
--metrics AC_ACTIVE_POWER AC_REACTIVE_POWER \
--start 2024-05-01T00:00:00 \
--end 2024-05-02T00:00:00 \
--format csv \
--states \
--bounds
In addition to the default CSV format the data can be output as individual samples or in dict
format.