6 Replies Latest reply on Jan 20, 2019 11:00 PM by Mattpdrexel

    Python and PI AF SDK - Syntax for Calculated Tags


      I'm a new PI AF SDK user interested in being able to create PI calculations within Python.


      For example, I have a calculation that I can easily generate within PI Process Books using the following expression:

      (stmeng_psatt(avg('s1.t2087a', 's1.t2088a', 's1.t2089a'))/14.696*29.92)


      The calculation basically takes the average of 3 temperatures and computes the saturation pressure.


      I'd like to be able to create a function in Python that performs this same calculation for me using the built in steam tables from PI.


      The PI AF SDK indicates that there is a AFCalculation method available (AFCalculation.CalculateAtTimes Method), but I am struggling interpreting the syntax so that it works for Python. 

      See below with my first crack at it.


      I am unsure how to pass the string expression into the function.


      Any suggestions would be helpful.

        • Re: Python and PI AF SDK - Syntax for Calculated Tags

          See below for example 'code' - in this case, I'm trying to use the AFCalculation method.


          x = '''(stmeng_psatt(avg('s1.t2087a', 's1.t2088a', 's1.t2089a'))/14.696*29.92)''




          TypeError  Traceback (most recent call last)

          <ipython-input-97-f00fec52bdc1> in <module>()

            1 x = '''(stmeng_psatt(avg('s1.t2087a', 's1.t2088a', 's1.t2089a'))/14.696*29.92)'''

            2 timerange = AFTimeRange(t_i, t_f)

          ----> 3 AFCalculation.CalculateAtIntervals(piServer,x,'*')


          TypeError: No method matches given arguments for CalculateAtIntervals

            • Re: Python and PI AF SDK - Syntax for Calculated Tags

              Hi Matthew


              Syntax error is due to value x. CalculateAtInterval takes 3 arguments ( object= PI or AF, string=PI Server , AFTime=List)


              String argument i.e. PE should contain PI Performance equation to do the calculation. stmeng_psatt is not known PE for the method.




                • Re: Python and PI AF SDK - Syntax for Calculated Tags



                  Thanks for the response.


                  Does anyone have an example of a string argument that would be accepted by AFCalculation.CalculateAtIntervals?

                  Is there an equivalent expression for saturation pressure within PI AF SDK using the PI steam tables?


                  I tried passing a few simple examples, but I can't get anything to work.

                    • Re: Python and PI AF SDK - Syntax for Calculated Tags

                      Hi Matthew,


                      AFCalculation.CalculateAtIntervals expects 4 arguments and not just 3 as suggest by Lal Babu Shaik. Never mind Lal

                      A single argument is definitely too few, the equation looks rather complex for a first approach and yes, I am also spotting some syntax errors. I assume stmeng_psatt is intended to be Steam_PsatT, and I really suggest taking care to enter the equation case sensitive.


                      The one thing that I don't like about the Python editors which I've seen so far is the missing Intellisense but this wouldn't help here anyhow because the expression is a string. Taking care to apply syntax case sensate helps to easier spot syntax errors, no matter if case sensitivity is required or not.


                      Using the Analysis plug-in of PI System Explorer can be pretty useful in creating proper expression strings. Thanks to the Analysis team for implementing Intellisense!!


                      My approach usually is to go from simple to more complex and success proofs this is a good approach. AFCalculation Class has C# and VB.Net sample code which is way more complex than what you are asking meaning that the first challenge is to spot the required pieces. I admit that I have used other resources like Rafael Borges' famous Pithon blog, when creating the following Python script. Can you please see if it is working for you before we move next?


                      import sys  
                      sys.path.append('C:\\Program Files (x86)\\PIPC\\AF\\PublicAssemblies\\4.0\\')  
                      import clr  
                      from OSIsoft.AF import *
                      from OSIsoft.AF.PI import *  
                      from OSIsoft.AF.Asset import *  
                      from OSIsoft.AF.Data import *  
                      from OSIsoft.AF.Time import *
                      piServer = PIServers().DefaultPIServer
                      piExpression = "'sinusoid' + 'sinusoidu'"
                      timerange = AFTimeRange("Y", "T")
                      timespan = AFTimeSpan.Parse("1 hour")
                      values = AFCalculation.CalculateAtIntervals(piServer, piExpression, timerange, timespan)
                      for val in values:
                          print('{0} at {1}'.format(val.Value, val.Timestamp))
                      3 of 3 people found this helpful
                • Re: Python and PI AF SDK - Syntax for Calculated Tags

                  Hi Matthew, just as an aside -

                  The generally preferred method of accessing the PI System through non .NET programming languages (like Python) is via PI Web API.

                  Scripts in Python, which uses CLR and AFSDK.dll, can become difficult to debug/troubleshoot in larger programs. Also with other languages (like Python) you will not be able to take full advantage of the features inherently available

                  through the .NET Framework.

                  1 of 1 people found this helpful