Exporting openHistorian data to Python

Is there any way to extract open Historian data into Python for better analytics using openHistorian.db instance?

Thanks Ritchie for providing the openHistorian Python API. Just a quick question do we need to configure snapDB separately as when I am running the code to connect to my localhost server where openHistorian is hosted I am getting this error : -

Connecting to openHistorian…
Failed to connect: Unexpected server response: 72

which is actually an exception thrown after this execution:

def readTest():

# Create historian connection (the root API object)

historian = historianConnection("localhost:8182")

instance: Optional[historianInstance] = None

try:

    print("Connecting to openHistorian...")

    historian.Connect()

Try leaving the port number out:

historian = historianConnection("localhost")

The openHistorian API, by default, listens on 38402 not 8182.

Thanks,
Ritchie

Hi Rithcie,
Thanks for your response. I implemented this too by opening the port 38402 on my system too but then too I am receiving this message:

Connecting to openHistorian…
Failed to connect: [WinError 10061] No connection could be made because the target machine actively refused it

Check the following:
(1) openHistorian service is running
(2) Windows firewall is turned off (or allow openHistorian.exe or ports as an exception)

Does the openHistorian Python API allow us to read statistics (STAT) values such as average latency?

I’ve tried running “readTest.py” and was able to get PPA type values, but was unable to read STAT values. Here is the code I modified for this:

records = metadata.GetMeasurementsBySignalType(SignalType.STAT, instance.Name)

I currently have 2 Historian instances (PPA, STAT) under “Manage Historian Instances”, both of which are local.

I noticed that in the “readTest.py” code when I try to change the index for initialInstance = historian.InstanceNames[0] to anything greater than 0, I receive the exception “Failed to connect: list index out of range”.

As far as I know we can only extract the PPA historian instance. Secondly STAT is not a signal type and hence it would not return any value.

No, statistics are archived in a separate local repository (e.g., C:\Program Files\openHistorian\Statistics) - however, there is a REST-based web-service for getting these values which should be easy to query from python (see below). FYI - examples are small subset of functions available, see IMetadataService.cs and ITimeSeriesDataService.cs for full REST method listing.

For metadata use: http://localhost:6055/ - examples:

  • Read all metadata in XML format:
    http://localhost:6055/historian/metadata/read/xml

  • Read all metadata in JSON format:
    http://localhost:6055/historian/metadata/read/json

  • Read metadata points from 10 to 100 in XML format:
    http://localhost:6055/historian/metadata/read/10-100/xml

  • Read metadata points from 10 to 100 in JSON format:
    http://localhost:6055/historian/metadata/read/10-100/json

  • Read metadata points 10, 11 and 20 in XML format:
    http://localhost:6055/historian/metadata/read/10;11;20/xml

  • Read metadata points 10, 11 and 20 in JSON format:
    http://localhost:6055/historian/metadata/read/10;11;20/json

For data use: http://localhost:6056/ - examples:

  • Read current value for data points from 10 to 100 in XML format:
    http://localhost:6056/historian/timeseriesdata/read/current/10-100/xml

  • Read current value for data points from 10 to 100 in JSON format:
    http://localhost:6056/historian/timeseriesdata/read/current/10-100/json

  • Read current value for data points 10, 11, and 20 in XML format:
    http://localhost:6056/historian/timeseriesdata/read/current/10;11;20/xml

  • Read current value for data points 10, 11, and 20 in JSON format:
    http://localhost:6056/historian/timeseriesdata/read/current/10;11;20/json

  • Read archived values for data points from 10 to 100 and specified start/end times in XML format:
    http://localhost:6056/historian/timeseriesdata/read/historic/10-100/2021-04-21%2023:50:00/2021-04-21%2023:51:00/xml

  • Read archived values for data points from 10 to 100 and specified start/end times in JSON format:
    http://localhost:6056/historian/timeseriesdata/read/historic/10-100/2021-04-21%2023:50:00/2021-04-21%2023:51:00/json

  • Read archived values for data points 10, 11, and 20 for specified start/end times in XML format:
    http://localhost:6056/historian/timeseriesdata/read/historic/10;11;20/2021-04-21%2023:50:00/2021-04-21%2023:51:00/xml

  • Read archived values for data points 10, 11, and 20 for specified start/end times in JSON format:
    http://localhost:6056/historian/timeseriesdata/read/historic/10;11;20/2021-04-21%2023:50:00/2021-04-21%2023:51:00/json

FYI - some history:

The STAT archive is an instance of the older openHistorian 1.0 that was deployed with the original openPDC (see GSF.Historian and LocalOutputAdapter) - fast writes, not as fast reads.

The PPA archive is an instance of the newer openHistorian 2.0 based on SNAPdb (see GSF.SortedTreeStore, openHistorian.Core and LocalOutputAdapter) - fast writes and fast reads.

Here was a good presentation explaining some of technical differences:

Thank you Ritchie, the information was very much helpful. I am able to get the data that I was looking for now.