read_history_processed (Aggregate)
The most powerful HistoryRead mode, used in conjunction with standard aggregation functions. Given an interval, an aggregation function, and the width of each subinterval, the server returns the aggregation result for each subinterval.
Preliminary / Supporting
- To pull all original points, please use read_history (Raw).
- For interpolation at a specific point in time, please use read_history_at_time.
Signature
def read_history_processed(self,
node_id: str,
start_utc: datetime,
end_utc: datetime,
processing_interval_ms: float,
aggregate_node_id: str) -> List[OpcUaDataValue]: ...
| Parameters | Default | Note |
|---|---|---|
| node_id | — | Target NodeId; Empty string exception ValueError |
| start_utc | — | Start of range (UTC datetime) |
| end_utc | — | Range Stop (UTC datetime) |
| processing_interval_ms | — | Length of a single aggregation interval (milliseconds); <= 0 Discard ValueError |
| aggregate_node_id | — | Aggregate function NodeId, HistoryAggregate select from; return empty string if none ValueError |
Returns List[OpcUaDataValue]; each element is the result of an aggregation interval, and must be released with dv: with or dispose() after use.
HistoryAggregate Constant (Recommended)
from darra_opcua import HistoryAggregate
HistoryAggregate.AVERAGE # i=2342 Arithmetic mean
HistoryAggregate.MINIMUM # i=2346 Minimum value
HistoryAggregate.MAXIMUM # i=2347 Maximum value
HistoryAggregate.COUNT # i=2352 "Good" status points
HistoryAggregate.TOTAL # i=2348 Cumulative (algebraic sum)
HistoryAggregate.STANDARD_DEVIATION_SAMPLE # i=11427 Sample standard deviation
HistoryAggregate.TIME_AVERAGE # i=11285 Time-weighted average
HistoryAggregate.RANGE # i=2353 Max - Min
HistoryAggregate.DELTA # i=11286 End of Interval - Start of Interval
For a complete list, see OPC UA Part 13 §5.4 Aggregate Functions.
Usage
Hourly average of the 24-hour temperature
import datetime as dt
from darra_opcua import HistoryAggregate
today = dt.datetime.now(dt.timezone.utc).replace(
hour=0, minute=0, second=0, microsecond=0)
yesterday = today - dt.timedelta(days=1)
values = ua.read_history_processed(
node_id="ns=2;s=Temperature",
start_utc=yesterday,
end_utc=today,
processing_interval_ms=3_600_000, # 1 hour
aggregate_node_id=HistoryAggregate.AVERAGE)
# Returns 24 values OpcUaDataValue, each representing the average for that hour
for dv in values:
with dv:
print(f"{dv.source_timestamp}: {dv.value}")
Highest Temperature Each Day of the Week
import datetime as dt
from darra_opcua import HistoryAggregate
today = dt.datetime.now(dt.timezone.utc).replace(
hour=0, minute=0, second=0, microsecond=0)
maxes = ua.read_history_processed(
"ns=2;s=Temperature",
start_utc=today - dt.timedelta(days=7),
end_utc=today,
processing_interval_ms=86_400_000, # 1 day
aggregate_node_id=HistoryAggregate.MAXIMUM)
Total traffic in one hour
import datetime as dt
from darra_opcua import HistoryAggregate
now = dt.datetime.now(dt.timezone.utc)
totals = ua.read_history_processed(
"ns=2;s=FlowRate",
start_utc=now - dt.timedelta(hours=1),
end_utc=now,
processing_interval_ms=3_600_000, # 1 hour bucket
aggregate_node_id=HistoryAggregate.TOTAL)
Performance Advantages
- Avoid fetching millions of data points and processing them on the client side
- Aggregation is performed on the server, and only the results are transmitted. The data transfer volume is calculated based on the number of aggregation intervals, not the number of original data points.
- Standard Solutions for Cross-Day and Cross-Week Reports
Server Requirements
- Historian must implement this aggregate function (the supported functions are listed in the ServerCapabilities/AggregateFunctions column on the server).
- When aggregation functions are not supported, an exception is
read_history_processedthrownOpcUaException, and the status code is set to the server's return value; SDK theStatusCodeenumeration covers only commonly used codes, and codes outside the enumeration are fallback toStatusCode.Bad
Implementation Constraints
- and read_history (Raw) Each request is sent only once RPC, retrieving the entire aggregated range in a single call; for large ranges, the business side handles the slicing on its own.
- Method level: no cancellation / timeout parameters (a single RPC's duration is bounded by the session's
request_timeout_ms); to stop early you can only decline to process the results after receiving them