5 Replies Latest reply on Oct 2, 2014 3:54 PM by kilgored

    Most efficient way to get latest good value for a tag


      What is the most efficient way, using PI-SDK, to get the latest GOOD archive event for a tag at or before a specified time?


      Bearing in mind that (a) there may not be any good event, but lots of bad ones, going back for weeks or years; (b) there may be no good events at all for the tag right back to the earliest event (presumably a Pt Created digital state).


      Alternatives I have looked at are:


      1. In a loop, keep calling PIPoint.Data.ArcValue with mode rtAtOrBefore.  If bad event (and not Pt Created) switch to mode rtBefore and keep calling ArcValue until a good event is found.


      2. In a loop, call PIPoint.Data.RecordedValues for some arbitrary interval (say a week) and look at all the events retrieved (starting with the latest) to find a good value.  If no values in interval, use ArcValue (rtBefore) to see if there are any more values that aren't Pt Created.  If no good value found, look at the interval before, and so on.


      Bearing in mind that 99% of the time, there will be a good value at or just before the time specified, so method 1 is going to be more efficient than method 2 which in the simplest case retrieves a lot more values.  But in that 1% of cases where the tag has had nothing but lots of bad values for a long time, then an awful lot of wire calls will be necessary and could hold up the application.


      What I want is a way to get the server to do the work of ignoring bad events, cutting down the number of wire calls in the worst case.  Can't see any way to do this though.

        • Re: Most efficient way to get latest good value for a tag

          Hello Jeremy,


          Have you checked why bad statuses are repeated over and over again in archives? Is this always the same bad status or is the status changing? If the same status is repeated and the source is an OSIsoft Interface, please check with the manual if there's a setting disabling repetitions.


          I have checked with the PI Performance Equation manual but couldn't find a function that returns the latest good event before or at a given Timestamp. You could use PctGood() e.g. using IPICalculation to check a period of time. Based on the percentage returned you can decide to either chose alternative 1 or 2.

            • Re: Most efficient way to get latest good value for a tag

              This is a generic application so unfortunately I have to work with whatever data is on the end user's server.


              How about using PIData.RecordedValuesByCount, direction dReverse, count = 1, filter expression = "NOT TagBad('tag')"?  Would that work?  What would happen in the case where the tag had lots of bad events but never any good ones? (By which I mean is the use of filter expressions quite inefficient compared with actually getting each event and checking its status)?