Skip to main content

update_history

Write data to the server-side Historian (rather than the "real-time values" from the current read/write operations):

High-Sensitivity Operations

HistoryUpdate: These are disabled by default on most production servers and must be explicitly enabled by an administrator, along with compliance auditing. See the server requirements for details.

Signature​

def update_history(self,
node_id: str,
update_type: HistoryUpdateType,
values: List[OpcUaDataValue]) -> List[StatusCode]: ...
ParametersDefaultNote
node_id—Target historical point NodeId; throws an empty string ValueError
update_type—HistoryUpdateType Enumeration Values
values—List OpcUaDataValue to be written to; throws None an exception ValueError; returns the empty list directly []

Returns List[StatusCode] a value that corresponds one-to-one with the sequence values, where each element is the result of writing that value. If the entire operation fails RPC (the server rejects the entire request), an exception is thrown OpcUaException. Method-levelNo cancellation / timeout parameters, The method is synchronous and blocks the calling thread RPC until it returns ( RPC the duration of a single call is constrained request_timeout_ms by the session).

HistoryUpdateType​

from darra_opcua import HistoryUpdateType
ValueMeaning
HistoryUpdateType.INSERTInsert only if the timestamp does not exist
HistoryUpdateType.REPLACEReplace only if the timestamp exists
HistoryUpdateType.UPDATEInsert + Replace (either option is acceptable; default is recommended)
HistoryUpdateType.REMOVEDelete this timestamp (only use the source_timestamp field in values; ignore the value field)

Usage​

Backfill Historical Data​

OpcUaDataValue Currently, the path is returned by reading (read e.g., /, / read_many, etc read_history .), and it is not yet possible to construct it directly based on a value plus a timestamp. Here values are some examples update_history of this:

import datetime as dt
from darra_opcua import HistoryUpdateType

# dv_list Can only come from read results (do not dispose of these elements prematurely)
dv_list = ua.read_history(
"ns=2;s=Temperature",
dt.datetime.now(dt.timezone.utc) - dt.timedelta(hours=1),
dt.datetime.now(dt.timezone.utc))

results = ua.update_history(
"ns=2;s=Temperature",
HistoryUpdateType.UPDATE,
values=dv_list)

for st in results:
print(st.name)

Insert only (do not overwrite existing entries)​

from darra_opcua import HistoryUpdateType

ua.update_history("ns=2;s=Temperature", HistoryUpdateType.INSERT, dv_list)

Delete at a Specific Time​

from darra_opcua import HistoryUpdateType

# REMOVE Mode: Each OpcUaDataValue in "values" determines source_timestamp the deletion target
ua.update_history("ns=2;s=Temperature", HistoryUpdateType.REMOVE, dv_list)

Permission Requirements​

  • The bit HistoryWrite (bit 3 / 0x08) of AccessLevel the node must be set.
  • Users must have HistoryUpdate permissions
  • This feature is disabled by default on most production servers and must be explicitly enabled by an administrator.

Error Handling​

Let's look at these two types of failure separately:

  1. Complete Failure — The server rejects the entire request, update_history throws an exception OpcUaException, and returns a status code in the range of ex.status_code.
  2. Failed to match values — Overall successful, but some values could not be written; the corresponding entries List[StatusCode] in the returned array are not Good.

The StatusCode enumeration covers only the commonly used codes; codes outside the enumeration degrade to StatusCode.Bad. The common per-value results of HistoryUpdate (INSERT hitting an existing timestamp / REPLACE hitting a nonexistent timestamp) are exactly codes outside the enumeration; in the return list they all appear as StatusCode.Bad, so they can only be told apart by inferring from update_type, not from the status code names.

Next Step​