Event Subscription
OPC UA events (Alarms & Conditions) are subscribed to through subscribe_events. These are OPC UA protocol events and are not the same thing as the SDK's internal ua.events.*.
ua.subscribe_events(...)— OPC UA Protocol Events (Alarms / Conditions, Server-Side Push Notifications, This Section)ua.events.*— SDK Internal Events (Session / Subscription Lifecycle, Protocol-Independent)
Signature
def subscribe_events(self,
node_id: Optional[str] = None,
existing_subscription: Optional[Subscription] = None
) -> EventSubscription: ...
| Parameters | Default | Note |
|---|---|---|
| node_id | None (Server, i=2253) | Event source node; if not provided, use the default Server (i=2253) |
| existing_subscription | None | Reuse an existing subscription; otherwise, create a new one publishing_interval_ms=1000.0 container, and its lifecycle is managed by EventSubscription |
EventSubscription
| Members | Note |
|---|---|
| event_arrived | List[Callable[[EventArrivedEventArgs], None]] Event added to the callback list |
| subscribe(node_id, event_filter=None) | Advanced Topic: Resubscribing/Resending After Creating a Custom Object EventFilter—see EventFilter |
| attach_container_subscription(container, owns=True) | Attach the Subscription container; You must attach it first subscribe |
| dispose() | When this event is removed MI, the container is destroyed at the same time owns=True. |
Supports the context with manager (__enter__ / __exit__).No close() methods —— Release only dispose().
ua.subscribe_events() for dispose: If no argument is passed existing_subscription, the container belongs to EventSubscription, and the entire container subscription dispose() will be destroyed; existing_subscription if an argument is passed owns=False, dispose() only the caller's own event is deleted MI, and the container remains with the caller.
EventArrivedEventArgs
@dataclass
class EventArrivedEventArgs:
severity: int = 0
message: Optional[str] = None
source_name: Optional[str] = None
time: Optional[datetime] = None
event_type: Optional[str] = None
| Field | Type | Note |
|---|---|---|
| severity | int | Severity (uint16) |
| message | Optional[str] | Event Text |
| source_name | Optional[str] | Event Source Name |
| time | Optional[datetime] | Time of the event (UTC; None if a valid time cannot be retrieved) |
| event_type | Optional[str] | Event Type Identifier String (Provided by the server) |
The callback parameters are always these 5 fields.None args.fields, nor are there any EventId / ConditionId / custom fields — you'll need to actively ua.read(...) retrieve any additional information in the callback.
Usage
Default Subscription Server Node
import time
with ua.subscribe_events() as ev:
def on_event(e):
print(f"[{e.severity}] {e.source_name}: {e.message}")
print(f" EventType={e.event_type} Time={e.time}")
ev.event_arrived.append(on_event)
time.sleep(300) # 5 minutes
Subscribe to events from a specific node
with ua.subscribe_events("ns=2;s=Line1") as ev: # Line1 It must be EventNotifier
ev.event_arrived.append(lambda e: print(e.message))
time.sleep(60)
Reusing an Existing Subscription Container
with ua.create_subscription(500) as sub:
sub.add_node("ns=2;s=Counter") # Data Subscriptions
# Attach the Event to MI the same container
with ua.subscribe_events("ns=2;s=Line1", existing_subscription=sub) as ev:
ev.event_arrived.append(lambda e: print(e.message))
time.sleep(60)
# ev.dispose only removes the event MI; it does not destroy the sub.
EventNotifier
Only nodes with a non-0 EventNotifier attribute (12) can generate events. By default, Server(i=2253) acts as an EventNotifier. Whether a custom node can act as an event source depends on the server implementation.
EventFilter (Advanced)
To filter events by criteria such as EventType / Severity, use EventSubscription.subscribe(node_id, event_filter) and pass an EventFilter. In the current version the filter is not sent to the server, so all events are still pushed in full, with the same 5 fixed fields — see EventFilter. The convenience entry point ua.subscribe_events(...) does not accept a filter parameter.
Alarm Operations (Acknowledge / Confirm)
Upon receiving an AlarmConditionType event, the operator is typically required to acknowledge it. By call Acknowledgment Method for Calling Alarm Nodes:
condition_id From? / event_id Not included in the 5 fields of the event callback (See above), provided by the business side's own alarm model / server-side object tree. Use ua.acknowledge(condition_id, event_id, comment) / directly only when confirming or adding a note ua.add_comment(...); see Alarms and Conditions; The following call syntax is used to call other alarm methods.
from darra_opcua import OpcUaVariant
# method_node_id To browse exit the alarm state (BrowseName = "Acknowledge"),
# Not by concatenating the string f"{condition_id}.Acknowledge"
ack_node_id = next(
r.node_id for r in ua.browse(condition_id)
if r.browse_name == "Acknowledge")
results = ua.call(
object_node_id=condition_id,
method_node_id=ack_node_id,
inputs=[
OpcUaVariant().set_string(event_id), # 1st arg is EventId (ByteString)
OpcUaVariant().set_string("Operator confirmed"), # 2nd arg is Comment (LocalizedText)
])
# call Each element in the returned `List[OpcUaVariant]` holds a handle, which must be released.
for v in results:
with v:
pass
The input signature for Acknowledge is (EventId: ByteString, Comment: LocalizedText). Python only OpcUaVariant has set_boolean / set_sbyte / set_byte / set_int16 / set_uint16 / set_int32 / set_uint32 / set_int64 / set_float / set_double / set_string / set_datetime, None set_bytestring / set_localized_text — In the example above, set_string is used to cast the parameter to a String before passing it; whether the server accepts it depends on its own implementation. When strict typing is required, first confirm whether the server accepts Strings.