2 Replies Latest reply on Jan 24, 2018 12:36 PM by Dan Fishman

    get last good value from  a date


      Can any body tell whats the function to use inside VB.NET to fetch a value between two dates and if no value then return last good value from the from date. thanks

        • Re: get last good value from  a date

          Hi Syed,


          Are you using PISDK or AFSDK for this? I'm going to assume the AFSDK since it's our recommendation.


          I'm also not sure I understand exactly what the request is. It sounds like you simply want the previous value starting at the "to" time and working backwards. For example, if you say "from 12:00 to 14:00", then you'll want the recorded value between those timestamps, but if there isn't any, you want to go to the previous value before 12. This could also be stated that the 12:00 is irrelevant and you just want the previous value from 14:00. I might be misunderstanding though.



          If this is what you're looking for, then I would suggest using the PIPoint.RecordedValue method with an AF retrieval mode of AtOrBefore.


          You could get a working test app by combining the example code from the PIPoint class' documentation, the RecordedValue method's documentation, and the AF retrieval mode enumeration set's documentation. Each one has a VB tab that you can select and it'll show you the visual basic syntax:


          If this is isn't what you're looking for, could you elaborate and maybe provide some examples?


          Edit: What I described was pretty straight forward so I wrote this test app

          Imports OSIsoft.AF
          Imports OSIsoft.AF.Asset
          Imports OSIsoft.AF.Time
          Imports OSIsoft.AF.PI
          Module Module1
              Sub Main()
                  Dim myPISystems As New PISystems
                  Dim myPISystem As PISystem = myPISystems.DefaultPISystem
                  Dim myPIServer As PIServer = PIServer.FindPIServer(myPISystem, "Citadel")
                  Dim myPIPoint As PIPoint = PIPoint.FindPIPoint(myPIServer, "Sinusoid")
                  Dim timeInput As String
                  Console.Write("Enter a timestamp: ")
                  timeInput = Console.ReadLine()
                  Dim myTimestamp As AFTime = New AFTime(timeInput)
                  Dim myValue As AFValue = myPIPoint.RecordedValue(myTimestamp, Data.AFRetrievalMode.AtOrBefore)
                  Console.WriteLine("The value at or before {0} is {1} at {2}", myTimestamp.LocalTime, myValue.Value, myValue.Timestamp.LocalTime)
              End Sub
          End Module


          Which has these outputs for sinusoid:



          1 of 1 people found this helpful
          • Re: get last good value from  a date
            Dan Fishman

            You could try to filter on the server for only good values, and bring back just one value.  If afValues.Count is zero, repeat with a larger time range, or the next range.  The below should give you the first good value within the time range.  Switch the startTime and endTimes depending on the direction you want to search.  This is an AF SDK sample:


            I decided to use RecordedValues assuming there are multiple values within the range and you have lots of bad data.


            Dim afValues = point.RecordedValues(New AFTimeRange(startTime, endtime), OSIsoft.AF.Data.AFBoundaryType.Inside, "BadVal('Cdt158')=0", False, 1)


            Also, if you decide to bring back all of the values, you could use a linq expression such as:

                    Dim afvalue = afValues.FirstOrDefault(Function(f) f.IsGood)

            Be sure to check that afvalue is not nothing.


            Not exactly sure what you are trying to do.



            2 of 2 people found this helpful