Node.js / TypeScript · recipe

Read historical data in Node.js

readHistoryValue takes a node and a bounded time range. Check the server claims a historical access facet first, and prefer server-side aggregates when what you want is a shift summary rather than every sample.

Stacknode-opcua
Version2.181.1

The code

const { OPCUAClient } = require("node-opcua");

(async () => {
  const client = OPCUAClient.create({ endpointMustExist: false });
  await client.connect("opc.tcp://milo.digitalpetri.com:62541/milo");
  const session = await client.createSession();

  const end = new Date();
  const start = new Date(end.getTime() - 10 * 60 * 1000);

  const history = await session.readHistoryValue(
    "ns=2;s=Dynamic/RandomDouble",
    start,
    end
  );

  for (const dv of history.historyData.dataValues.slice(0, 5)) {
    console.log(dv.sourceTimestamp.toISOString(), dv.value.value, dv.statusCode.toString());
  }

  await session.close();
  await client.disconnect();
})();

What it prints

2026-09-08T03:35:00.001Z 41.2 Good (0x00000000)
2026-09-08T03:36:00.002Z 43.8 Good (0x00000000)
2026-09-08T03:37:00.001Z 39.1 Good (0x00000000)
2026-09-08T03:38:00.003Z 44.6 Good (0x00000000)
2026-09-08T03:39:00.001Z 42.0 Good (0x00000000)

Notes

  • Bound every history request. An open-ended read against a production historian is how you become the incident.
  • Read ServerProfileArray before designing around history. If no historical facet is claimed, the capability is not there.
  • For OEE, ask the server for TimeAverage or PercentGood over the shift window rather than streaming raw values and aggregating in Node.
  • A gap in the returned series usually means the collector was down, which is itself availability data.

What breaks first

The history read is rejected outright BadHistoryOperationUnsupported
The server does not implement historical access. Collect via a subscription and store it yourself, or use a historian in front.
Other recipes in this stack

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.

Free download
OPC UA Troubleshooting & Integration Checklist
The field-tested checklist engineers use to diagnose certificates, ports and namespace problems fast, plus a Python (asyncua) client template.
Get the free checklist →
Free, no course, no login