10 Replies Latest reply on Jul 2, 2014 9:01 PM by lpagemorin

    Hourly Average that starts each hour as an AF attribute

    Kapil.Rakh

      I need to get hourly average for a tag starting from each hour. At the start of an hour, the hourly average is reset and new hourly average calculation begins every minute until the hour ends.

       

      This can be done using PI Totalizer but I wanted to do this using AF (without creating a PI tag).

       

      My approach was to use the "Time Range" value retrieval method.

       

      5483.PI-Point-Data-Reference.png

       

      And the Relative time will be = -1x  where x is the number of minutes at the time client request the value. In most case, it will be the current minute (i.e. client requesting the average at current time).

       

      Off course x will change every minute so will be the average.

       

      How to achieve this in AF?

        • Re: Hourly Average that starts each hour as an AF attribute
          Kapil.Rakh

          Correction: Relative Time is -xm so it will have values like -1m, -2m etc

            • Re: Hourly Average that starts each hour as an AF attribute
              Marcos Vainer Loeff

              Hello Kapil,

               

               PI AF SDK/PI System Explorer 2.5 (2012) lets the user retrieve interpolated values from an attribute. There are two options to do so:

              • PI System Explorer --> Right click on your attribute and select “Time Series Data…”. Click on the “Sampled” ribbon and then select the start time, end time and interval. The results will be shown after clicking on the “Refresh” button.
              • PI AF SDK --> The same values can be retrieved programmatically using the method InterpolatedValues. Please refer to the code snippet below:

               

               
                          AFAttribute myAttribute = AFObject.FindObject(@"\\MARC-PI2014\Tests\MyElement|RegularSinusoid") as AFAttribute;
                          AFValues myValues = myAttribute.Data.InterpolatedValues(new AFTimeRange(new AFTime("*-1d"), new AFTime("*")), new AFTimeSpan(new TimeSpan(1, 0, 0)), null, "", false);
                          foreach (AFValue myValue in myValues)
                          {
                              Console.WriteLine(myValue.Value + "\t" + myValue.Timestamp.LocalTime.ToString());
                          }
                          Console.WriteLine("Finish");
                          Console.ReadKey();
              

               

               

              Hope this helps!!

                • Re: Hourly Average that starts each hour as an AF attribute
                  Kapil.Rakh

                  Marcos,

                   

                  Thanks for reply. I actually wanted to show this in a ProcessBook and may be also in Excel . I thought I should try AF before creating a totalizer or a custom solution (like you've shown).

                    • Re: Hourly Average that starts each hour as an AF attribute
                      Marcos Vainer Loeff

                      Hello Kapil,

                       

                      Actually the previous code snippet just get the interpolated values of an attribute. To show the hourly average, please refer to the code snippet below:

                       
                       AFAttribute myRegularAttribute = AFObject.FindObject(@"\\MARC-PI2014\Tests\MyElement|RegularSinusoid") as AFAttribute;
                       IDictionary<AFSummaryTypes, AFValues> mySummaries = myRegularAttribute.Data.Summaries(new AFTimeRange("*-1d", "*"), AFTimeSpan.Parse("1h"), AFSummaryTypes.Average, AFCalculationBasis.EventWeighted, AFTimestampCalculation.Auto);
                                  foreach (AFValue myValue in mySummaries[AFSummaryTypes.Average])
                                  {
                                      Console.WriteLine(myValue.Value + "\t" + myValue.Timestamp.LocalTime.ToString());
                                  }
                      

                       

                       

                      Concerning the AF Attribute settings, if you want to get hourly average, just use "-1h" for the Relative Time. Is this what you are looking for?

                       

                      I have tested using the configuration string = "\\MARC-PI2014\SINUSOID;TimeMethod=TimeRange;RelativeTime=-1h;TimeRangeMethod=Average;TimeRangeMinPercentGood=0" and it works fine for me as it shows the hourly average of the sinusoid tag.

                       

                      Hope this helps you!

                        • Re: Hourly Average that starts each hour as an AF attribute
                          Kapil.Rakh

                          Marco

                           

                          I want hourly average but if I just use "-1h" , it will give me average for last 60 minutes.

                           

                          I need the average of the current hour .

                           

                          For example,

                           

                          So if I asked for average at 12:31 (current time) then I should get average from 12:00 to 12:31.

                           

                          But if I use -1h, I get average from 11:31 to 12:31 which is not what I want.

                           

                          So if I ask average at 12:31, I need to somehow use relative time of -31m.

                            • Re: Hourly Average that starts each hour as an AF attribute
                              skwan

                              Kapil:

                               

                              You made it clear you didn't want to use a PI tag or use PI Totalizer.  Can you tell me why you do not want to do that?  Is the totalized result not important (or worthwhile) to historize?

                                • Re: Hourly Average that starts each hour as an AF attribute
                                  Kapil.Rakh

                                  I just wanted to see if this can be done without creating a totalizer (off course totalizer result would be more accurate). This measurement is kind of like on the fly calculation to see the hourly average.

                                    • Re: Hourly Average that starts each hour as an AF attribute
                                      Marcos Vainer Loeff

                                      Hi Kapil,

                                       

                                      If you don't want to use to create a new PI Points, I guess there are 3 alternatives:

                                      • Create an attribute for each different "Relative time"  you want to use.
                                      • Change programmatically the "Relative time" of the attribute every time you want to chance this value.
                                      • Develop a custom client applicatication using the code snippet I have shown you previously.
                                      Nevertheless, I still think that creating PI Points (Totalizers) is the easiest way to solve your problem.
                                      Hope this helps you!!
                                        • Re: Hourly Average that starts each hour as an AF attribute
                                          Kapil.Rakh

                                          Marcos,

                                           

                                          I agree. I will create a totalizer point for this.

                                           

                                          Thanks

                                            • Re: Hourly Average that starts each hour as an AF attribute
                                              lpagemorin

                                              Marcos, if I may add to this thread, using -1h as the Relative Time you can achieve what you want. However, it must be done in the visualization tool directly, like PI ProcessBook or PI DataLink. It all depends on the Start and End times you are using to display the values on a Trend or on the spreadsheet. For example, I tried setting a Trend Start and End times in PI ProcessBook to 't' and 't+1d' and it seems to be working, that is the 1-hour intervals will be right on the top of the hour since the End time is itself on the top of the hour. Then, if you are putting the End time into the future, the last hourly average calculation that will be displayed will show the up-to-date result for the current hour.

                                               

                                              The other piece you need to make this work, apart from setting the End time appropriately, is to have the hourly average attribute displayed with Step=True. In order to do this, since there is currently no option in the PI Point data reference for it, you can create a children (or parent) attribute to the hourly average one. You'll need to configure that attribute to be a Formula and have its configuration string similar to this: (H=Hourly Average;;stepped=True).

                                               

                                              Regards,