3 Replies Latest reply on Apr 9, 2018 11:59 AM by rborges

    Is there a way to query into Pi archive from Python?

    starshman

      I'm trying to access data stored in our Pi database through a Python wrapper. Is this possible? I was able to make a connection utilizing PI and Python? PIthon! but I don't understand how I can use this to query into the database.

        • Re: Is there a way to query into Pi archive from Python?
          vpartusch

          Hi Steven,

           

          You should be able to pull data using the same functions that you would normally use in AFSDK, assuming you have added the pythonnet package and added the OSIsoft.AFSDK reference. This example code below connects to the default PI server, then gets the snapshot value and last 10 archive values for the "sinusoid" tag:

           

          import sys 

          sys.path.append('C:\\Program Files (x86)\\PIPC\AF\PublicAssemblies\\4.0\\') 

          import clr 

          clr.AddReference('OSIsoft.AFSDK')

           

          from OSIsoft.AF.PI import * 

          from OSIsoft.AF.Search import * 

          from OSIsoft.AF.Asset import * 

          from OSIsoft.AF.Data import * 

          from OSIsoft.AF.Time import * 

           

          myPIServers = PIServers() 

          myPIServer = myPIServers.DefaultPIServer

          myPIServer.Connect(False) 

           

          tag = PIPoint.FindPIPoint(myPIServer, "sinusoid") 

          lastData = tag.Snapshot()  

           

          print('Snapshot Value:\nTimestamp: {0} Value: {1}\n\n'.format(lastData.Timestamp, lastData.Value))

           

          startTime = AFTime.Now

          count = 10

          forward = False;

          boundary = AFBoundaryType.Inside

          filter = ""

          includeFiltered = True

           

          archiveData = tag.RecordedValuesByCount(startTime, count, forward, boundary, filter, includeFiltered)

           

          print('Last 10 Archive Values:\n')

          for item in archiveData :

              print('Timestamp: {0} Value: {1}'.format(item.Timestamp, item.Value))

           

           

          Another good option would be just to use PI Web API and retrieve your data with HTTP requests.

          1 of 1 people found this helpful
          • Re: Is there a way to query into Pi archive from Python?
            tramachandran

            The recommended method of accessing the PI System through a non .NET programming languages (like Python) is through client libraries via PI Web API.

            PI Web API client library for Python

             

            Another article on using PI AF SDK dlls through Python

            Python 3.6 and AFSDK example

            1 of 1 people found this helpful
            • Re: Is there a way to query into Pi archive from Python?
              rborges

              Hey Steven,

               

              Just to add to Thyag's and Vincent's replies, a couple years ago I wrote an article on that and since then I've been doing a lot of code on Python using AF SDK. If you have a specific question or problem, feel free to reach me.

              2 of 2 people found this helpful