3 Replies Latest reply on Apr 3, 2013 2:47 AM by hanyong

    Understanding How to Use IPICalculation


      Hi everyone.


      So, I've been struggling for about a week now trying to really understand how to make IPICalculation do what I want it to do. I've downloaded all of the help files and studied them, but it's just not working.


      Here's the case:


      I have a tag called "System_2_Mode_Reason", which changes whenever the mode of a machine changes throughout the day. It could be "dumping", "contamination", "normal", etc...  There are maybe 20 different things that could be in there depending on what the operator chose from a dropdown list.


      What I am trying to do in Processbook is actually collect how many times per day that tag had "contamination" as a value. Maybe monday the tag was equal to "contamination" 4 times. Tuesday 0. Wednesday 20. Etc.....  I want to be able, in code, to use a defined time range - say one week - have an array of counts for each day, even if there were zero for the day.


      Here's the Problem:


      I thought I could accomplish this with IPICalculation by using an approach like this:



      PE_Expression = "IF 'SYSTEM_2_Mode_Reason' = ""Embedded"" THEN 1 ELSE 0"
      strTag = "SYSTEM_2_Mode_Reason"
      Set srv = Servers("k2ar1pixx")
      Set ipiCalc = srv
      Set pt = srv.PIPoints(strTag)
      Set myStartTime = New PITimeServer.DynamicTime
      Set myEndTime = New PITimeServer.DynamicTime
      myStartTime.InputString = Format(DateAdd("d", -7, Now()), "mm/dd/yyyy HH:mm:ss")
      myEndTime.InputString = Format(Now(), "mm/dd/yyyy HH:mm:ss")
      duration = "7d" ' 7 dayu calculation period
      ' calculate hourly total for the entire time period (180 days)
      Set nvsSum = ipiCalc.ExpressionSummaries(StartTime, EndTime, duration, PE_Expression, PISDK.ArchiveSummariesTypeConstants.asTotal, CalculationBasisConstants.cbEventWeighted)
      ' extract the pivalues for total from the result
      Set valsum = nvsSum("Total").Value

       I have "valsum" decalred as PIValues - so I would think after the above use of ExpressionSummaries, valsum would contain all of those daily values for each 1d interval. I actually verified that valsum count is 7 - but when I loop through "For Each ptValue in valsum", and then pull the value with ptValue.Value, first I get a 0, and then I get a type mismatch error the second time through the loop.


      If I use TimeWeighted, I get 7 good values, but they're all decimal values less than zero like 0.6434223, 0.3452232, etc --  not integer counts as you'd expect.


      It seems like it should be simple enough to count the number of times a certain tag equals a certain value every day, but I just can't get this to work.


      Anyone have any similar IPICalculation code you can share that does soemthing like this?



        • Re: Understanding How to Use IPICalculation

          Sorry - as you can see in the code I'm counting when the tag = "Embedded" not "Contaminated".

            • Re: Understanding How to Use IPICalculation

              Hi Ryan,


              A couple of suggestions to try here. Firstly, I would suggest using the IPIData2 interface for this. The ExpressionSummaries method of IPIcalculation will perform the summary calculations on an expression, not a specific PI tag. Secondly, if you are wanting to count the number of events for when the tag was equal to a certain value, try using PISDK.ArchiveSummariesTypeConstants.asCount instead. The Event Weighted calculation basis combined with this should then give you your count of events. This is the same as the behaviour you would get if you did this in PI DataLink using the Calculated Data function. The following code works for me to give this result: 

                  Dim srv As Server
                  Dim expression As String
                  Dim nvs As NamedValues
                  Dim returnvalues As PIValues
                  Dim starttime As New PITime
                  Dim endtime As New PITime
                  Dim interval As Variant
                  Dim pdata As PIData
                  Dim ipid2 As IPIData2
                  Dim pt As PIPoint
                  Set srv = Servers.DefaultServer
                  starttime.LocalDate = "2/04/2013"
                  endtime.LocalDate = Now()
                  expression = "('CDM158' = ""Manual"")"
                  Set pt = srv.PIPoints("CDM158")
                  Set pdata = pt.Data
                  Set ipid2 = pdata
                  Set nvs = ipid2.FilteredSummaries(starttime, endtime, interval, expression, asCount, cbEventWeighted)
                  Set returnvalues = nvs("Count").Value
                  MsgBox returnvalues(1).Value, vbOKOnly, "Filtered Summaries"

               This code is taken pretty much from the PI-SDK example for IPIData2.FilteredSummaries method. If I have missed the point of what you are trying to do, then let me know, otherwise, hope this helps.





                • Re: Understanding How to Use IPICalculation

                  Hi Ryan,


                  John's suggestion is good, and would have been something that I'd have suggested as well. 


                  Thanks John 


                  The aggregation or summary type total calculated with TimeWeighted option gives you a time-integral from the values rather than the sum of values. It is really meant to calculate total for PIPoints that stores rate data like if the PIPoint is water flow rate or fuel flow rate, the time-integral will give the total volume of water/fuel for that time period.