Python · recipe
Subscribe to alarms and events in Python
Subscribe to the server's event notifier and select the fields you need. Filtering server-side keeps the noise off the wire, and ConditionClassId is what makes a downtime reason portable between machines from different vendors.
| Stack | asyncua (FreeOpcUa/opcua-asyncio) |
|---|---|
| Version | 2.0.1 |
The code
import asyncio
from asyncua import Client, ua
class Handler:
def event_notification(self, event):
print(f"[{event.Severity}] {event.SourceName}: {event.Message.Text}")
async def main():
async with Client(url="opc.tcp://milo.digitalpetri.com:62541/milo") as client:
handler = Handler()
sub = await client.create_subscription(500, handler)
server_node = client.get_node("i=2253") # the Server object is the event notifier
await sub.subscribe_events(server_node)
await asyncio.sleep(10)
await sub.delete()
asyncio.run(main())
What it prints
[500] MachineTool_1: Tool life below threshold
[800] MachineTool_1: Tool breakage detected
[100] Server: Session activated
Notes
- The Server object (i=2253) is the standard event notifier. Some servers expose additional notifiers on individual machines: browse for the EventNotifier attribute.
- Pass an explicit list of event types or fields to subscribe_events to narrow what you receive. The default selection is broad.
- Call ConditionRefresh after a reconnect, or your client will believe the plant is healthy while alarms are still active.
- For OEE work, select ConditionClassId. MachineTool's condition classes give you a standardised downtime taxonomy that survives a change of machine vendor.
What breaks first
The event filter is rejected because an operand does not resolve
Every field in the select clause must exist on the event type you are filtering. Browse the event type first and select only fields it declares.
BadFilterOperandInvalidEvery field in the select clause must exist on the event type you are filtering. Browse the event type first and select only fields it declares.
Other recipes in this stack
Concepts on this page
Last verified 7 September 2026 against OPC UA 1.05.07. OPC UA Connect is an educational resource and is not affiliated with the OPC Foundation.