10 Replies Latest reply on Nov 17, 2011 1:07 PM by appanar

    calculate rate of change

      I am trying to calculate in PI Processbook the rate of change for a PI tag in minutes using the last value and either the second last value or the last 30min average. I then want to use the information to calculate how long in minutes it would take the PI tag to reach the limit. The limit is specified using a dataset. I would like to calculate the rate of change for all types of PI tags i.e. Flows, levels,pressures, temperatures etc. I want to display the value on the trend symbol for the panel man to take action. e.g. "Action required within", 5, "mins" Is there a macro that can be used in PI processbook to do this? Regards, Ram Appanah
        • Re: calculate rate of change

          Ram, you will have to write this logic in VBA.

            • Re: calculate rate of change
              Ahmad Fattahi

              Or alternatively, you can create new tags/PI ACE calculations/PEs that do this for you outside PI ProcessBook and then trend them or show them in PI ProcessBook.

              • Re: calculate rate of change

                I am used to coding in EXCEL. I am not sure how to code the rate of change in PI Processbook.

                  • Re: calculate rate of change

                    Have you written VBA in MS Excel or only formulas in cells?

                      • Re: calculate rate of change

                        If you have two value symbols on your display, one for the raw tag and one calculating the 30m average, you can use the DataUpdate event to do the rate of change calculation:

                        Private Sub Value1_DataUpdate()
                            Dim RateOfChange As Double
                            RateOfChange = (Value1.GetValue(vrDate1, vrStatus1) - Value2.GetValue(vrDate2, vrStatus2)) / 30
                            ' your action here
                        End Sub


                          • Re: calculate rate of change

                            Thanks for the code.


                            Looking at your example  I know that value 1 gets the last value. How does value2 get  the average for the last 30 mins.

                              • Re: calculate rate of change

                                Value2 is based on a dataset. I have created a dataset that calculates every 30m and is using teh value of the tag. Then I used teh average column of the dataset. If you want to avoid the dataset, you would have to use the PISDK to get the 30m average.

                                  • Re: calculate rate of change

                                    Here is a code snippet for using the PISDK to retrieve the 30m average.


                                    First we get the value from the value object - that is our current value:

                                        Dim Val1, Val2 As Double
                                        Dim vrDate1, vrDate2, vrStatus1, vrStatus2 As Variant
                                        Val1 = Value1.GetValue(vrDate1, vrStatus1)

                                    Next we need to Calculate the dates if the display is not on current time. Otherwise we will get a bogus value when looking at history:

                                        ' Calculate the correct display times
                                        Dim dtStart, dtEnd As Date
                                        dtEnd = vrDate1
                                        dtStart = DateAdd("n", -30, dtEnd)

                                     Ready to go to the PISDK:

                                        ' Get Tagname and Servername as string from value object
                                        Dim strTagname, strServername As String
                                        strTagname = Value1.GetTagName(1)
                                        strServername = Mid(strTagname, 3, InStr(3, strTagname, "\") - 3)
                                        strTagname = Mid(strTagname, Len(strServername) + 4, Len(strTagname) - Len(strServername) - 3)
                                        ' Get the PI Server object
                                        Dim srvServer As PISDK.Server
                                        Set srvServer = PISDK.Servers(strServername)
                                        ' Get the PI Point object
                                        Dim ppPIPoint As PISDK.PIPoint
                                        Set ppPIPoint = srvServer.PIPoints(strTagname)

                                    and do the calculation:

                                        ' Get the time weighted average
                                        Dim nvsNVS As NamedValues
                                        Set nvsNVS = ppPIPoint.Data.Summaries(dtStart, dtEnd, btAuto, asAverage, 1, cbTimeWeighted)
                                        Dim nvNV As NamedValue
                                        Set nvNV = nvsNVS("Average")
                                        ' We should have only 1 value
                                        Val2 = nvNV.Value(1)
                                        Dim RateOfChange As Double
                                        RateOfChange = (Val1 - Val2) / 30