Python · recipe
Call an OPC UA method in Python
Call the method on its parent object, passing input arguments positionally. Output arguments come back as a list. A method that fails returns a status code rather than raising in the transport, so check the result.
| Stack | asyncua (FreeOpcUa/opcua-asyncio) |
|---|---|
| Version | 2.0.1 |
The code
import asyncio
from asyncua import Client, ua
URL = "opc.tcp://milo.digitalpetri.com:62541/milo"
async def main():
async with Client(url=URL) as client:
obj = client.get_node("ns=2;s=Methods")
method = await obj.get_child(["2:sqrt(x)"])
result = await obj.call_method(method, ua.Variant(16.0, ua.VariantType.Double))
print("sqrt(16) =", result)
asyncio.run(main())
What it prints
sqrt(16) = 4.0
Notes
- Call on the parent object and pass the method node, not the other way round. This trips people up coming from other RPC models.
- Check the Executable attribute if a call is refused, a method can exist and be temporarily not callable.
- Input argument types must match the method's declared InputArguments. Build explicit Variants.
- A single output argument comes back as a bare value; several come back as a list.
- CLI equivalent: `uacall -u <url> -n "ns=2;s=Methods" -m "2:sqrt(x)" 16.0`.
What breaks first
The call is refused because the argument list does not match the method definition
Read the method's InputArguments property to get the exact count, order and types, then build Variants to match.
BadArgumentsMissingRead the method's InputArguments property to get the exact count, order and types, then build Variants to match.
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.