12 Replies Latest reply on Feb 1, 2018 3:08 PM by Oscar_Garcia

    [PI WEB API] Need help to build an expression

    Nicolas.Isambourg

      Dear Members of the PISquare community,

       

      With the following PI AF attributes:

      - Length

      - Coil Id

      - Measurement

       

      With PI Web API, I would like to get the "Measurement" value when the "Coil Number" is equal to a particular value and at the moment where Abs('Length' - 12500) is the smallest value.

      How can I do this ?

       

      Thanks,

      Best regards,

      Nicolas

        • Re: [PI WEB API] Need help to build an expression
          Eugene Lee

          Hi Nicolas,

           

          The definition of 'smallest' value is vague. You will need to have a time range to be able to define a smallest value.

           

          You can use a combination of

           

          FindEq: to find the timestamp when the "Coil Number" is equal to a particular value within a time range -> Timestamp1

          TagMin: to find the smallest value within a time range and then FindEq to find the timestamp of the smallest value -> Timestamp2

           

          if Timestamp1 and Timestamp2 are the same, then proceed to use that timestamp with TagVal to find the value of Measurement.

           

          if they are not the same, then abandon it.

           

          1 of 1 people found this helpful
            • Re: [PI WEB API] Need help to build an expression
              Nicolas.Isambourg

              Hello Eugene,

               

              thank you very much for replying to my topic.

              sorry I wasn't clear enough.

              Below are an example of the not filtered data between '*-2h' and '*':

               

              Timestamp                    Coil Id     Length     Measurement

              07/07/2017 07:51:06     12345     11250.7     25.6

              07/07/2017 07:51:07     12345     11260.8     96.3

              07/07/2017 07:51:08     12345     11310.1     50.4

              07/07/2017 07:51:09     06789     8510.7       96.3

              07/07/2017 07:51:10     06789     11250.1     85.3

              07/07/2017 07:51:11     06789     12354.4     74.1

              07/07/2017 07:51:12     06789     13541.2     36.5

              07/07/2017 07:51:13     574123    1483.4     85.4

              07/07/2017 07:51:14     574123     6325.3     41.1

               

              For example, I would like to have the "Measurement" value when the "Coil Id" is equal to "06789" and at the moment where Abs('Length' - 12500) is the smallest value.

              Here, the result is 74.1 at 07/07/2017 07:51:11

              So, the time range is given by the filter 'Coil Id' = "06789", between 07/07/2017 07:51:09 and 07/07/2017 07:51:12.

              Can I use the filter expression 'Coil Id' = "06789" in a TagMin function ?
              Can I apply the TagMin function to the expression Abs('Length' - 12500) ?

               

              Thanks,

              Best regards,

              Nicolas

                • Re: [PI WEB API] Need help to build an expression
                  pmartin

                  Hi Nicolas,

                   

                  You will need three calls to achieve this.  This means you can either use the Batch controller to merge them into one larger request or make three separate requests.

                   

                  The first request is to get the Web Id of the Measurements attribute.

                   

                  The second request will get the timestamp for the minimum value of Abs('Length' - 12500) while the Coil Id equal to 06789.

                  https://<server>/piwebapi/calculation/summary?webId=<WebID>&startTime=*-8h&summaryType=minimum&expression=if 'Coil Id' = 06789 then Abs('Length' - 12500) else 10000000

                  Let me break that down

                  webId=<WebID> - this WebId should be for the parent element that contains Coil ID, Length, and Measurement

                  summaryType=minimum - We're going to find the minimum value for the evaluated expression

                  startTime=*-8h - Look for the minimum in the last 8 hours

                  expression=if 'Coil Id' = 06789 then Abs('Length' - 12500) else 10000000

                       if the Coil Id is the one we are looking for, add Abs(...) to the list of values to be compared

                       If Coil ID is not the one we are looking for, add an arbitrarily large number to be compared

                            If you want this to be a bit more precise, you can use TagMax('Length', '*-8h', '*') - 12500 although I don't think it's strictly necessary

                   

                  The result of this request has the timestamp that you should evaluate Measurement at.

                   

                  The third request will use the RecordedAtTime action in the stream controller with the Measurement WebId from the first request and the time from the second request.

                  1 of 1 people found this helpful
                    • Re: [PI WEB API] Need help to build an expression
                      Nicolas.Isambourg

                      Hello Paul,

                       

                      thank you very much for helping me on this topic.

                      Your solution works perfectly. That's exactly what I wanted.

                       

                      Subsidiary question: Now, I wonder whether there is a more simple way to do what I need. I should have described my purpose.

                       

                      I need to find the 'length' recorded value nearest to 12500.

                       

                      Timestamp                    Coil Id     Length     Measurement

                      07/07/2017 07:51:10     06789     11250.1     85.3

                      07/07/2017 07:51:11     06789     12354.4     74.1

                      07/07/2017 07:51:12     06789     13541.2     36.5

                       

                      For example here, the value nearest to 12500 is

                      07/07/2017 07:51:11 06789 12354.4 74.1

                       

                      In this other example,

                      Timestamp                    Coil Id     Length     Measurement

                      07/07/2017 09:11:54     06789     12180.1     36.3

                      07/07/2017 09:11:55     06789     12320.4     12.4

                      07/07/2017 09:11:56     06789     12541.2     85.4

                      the value nearest to 12500 is

                      07/07/2017 09:11:56 06789 12541.2 85.4

                       

                      Do you consider there is a more simple way to get this instead of looking for the minimum of Abs('Length' - 12500) ?

                       

                      Thanks,

                      Best regards,

                      Nicolas

                  • Re: [PI WEB API] Need help to build an expression
                    Oscar_Garcia

                    Hi,

                    I just want confirm if is possible to use PE functions such as FindLT() in the PI WebAPI.
                    I basically want to get the last time a tag value (megawatts) was below the limit (e.g. 10 MW). If this is possible to do in the PI webAPI, what would be an example URL?

                     

                     

                    Thanks,

                    Oscar