# All Things PI - Ask, Discuss, Connect

1 Post authored by: tbernier  # AF Analytics –Analyzing an open Batch/Event in Real Time

Posted by tbernier Apr 20, 2017

# In response to user voice :

Introduction :

Event frames are really useful to find out when some events happen for a specific time range.

However, it´s not possible to run an anaylsis in that event frame while the event frame is running.

Here is a workaround of how you can actually do it. The trick is to run the analysis under certain condition and link this analysis to an event frame.

Intuitively, we try to build an analysis based on an event frame but that is not possible if we want to run calculation during the event frame. We will see instead how to run an event frame based on a analysis.

That will allow us to do some calculations during an opened event frame.

Problem description:

Let´s say we want to do some calculation over a time range for a periodic signal.

But the time range is not fixed and the period of the signal can change over time.

In others words, we don’t know the explicit start and the end times of the calculation.

The only thing we know is that we want to run the calculation (to find the integral of the ‘sinusoid’ signal in our example) while the signal is increasing and stop the calculation when it’s decreasing.

Let’s plot a graph to summarize what we want to achieve: In our example, we will use instead the Sinusoid signal from the PI Interface for Random, Fractal, Station, Sinusoid Data: As the period of the signal is not always the same we can’t use a fixed time range.

We will have to retrieve the start and the end times using another tag.

In this example we have a variable Trigger2 linked to a PI Tag called ST2. (This PI Tag will be needed if we want to create later on an event frame.)

This variable is equal to 1 when the condition of the calculation is met, otherwise it’s equal to 0.

Here we simply define Trigger2 in that:

• If the previous value in the archive of the tag Sinusoid is lower than the current value of Sinusoid then the value of Trigger2 is equal to 1

• If not; Trigger2 is equal to 0  If we were working with a “real “signal, the only difference would have been the variable Trigger2. It would have to take into account the noise present on the signal. We should have ended up with something like that: Then comes the trick of this analysis:

We create a variable startdate. This variable has to be mapped to a PI Tag (Calcultime in the example) because we will need to store it in the archive. Do not forget in AF to set the Value Type of this tag to “DateTime”. Thanks to the analysis, we will store the actual timestamp of the Sinusoid tag if the trigger is equal to 0. If not, we won´t send any data to the PI Data Archive.

That way, after the Trigger2 variable jumps from 0 to 1 (in other words, when the Sinusoid signal starts increasing) we won´t update the timestamp in the archive.

The value stored in the archive is the beginning of our integral calculation.

Then, we can use this timestamp to run our calculation from Calcultime to now ‘*’. Now it’s time to put everything together in a table understand for a better understanding: While Trigger2 is set to 0 (when the Sinusoid value is not increasing) we store the current PI Time of the Sinusoid tag. The result in Calcultime and PI Time column are the same.

WhileTrigger2 is equal to 1, we don´t send the PI Time anymore to the archive so the value of Calcultime is the last value stored in the archive. This value corresponds to the beginning of our integral calculation Tot2.

Please note that the analysis scheduler is set on Event-Triggered. Indeed, we don’t know how long is the time range of the calculation of the integral so it´s important not to set it to periodic in that case.

Now we are running the integral of the signal from Calcultime to now. We can see the result Tot2 increasing in real time: About the precision of the calculation:

In our example, we decide to compare the previous event in the archive and the current value of the tag..

That way, the precision of the integral depends on the configuration of the Sinusoid tag (scan class, exception/compression parameters, etc)

Link an event frame to this calculation:

It´s possible to link that calculation to an event frame so we can easily find when the calculation was running or not.

To do that, you should create a PI Tag linked to the variable Trigger 2 in my example. Actually I already did that in the previous part but this should be done if you want to use the event frame. Then we can backfill the data and we can when the calculation was running in the last 2 days : Thanks to the event frame, we can easily find when the calculation was running or not using Coresight for instance : Please note that this method is quite heavy in configuration and required 2 tags at least per Analysis.

The possibility to run some calculations during an opened event frame will be added to the next release of Analytics (2017 R2)

By date: By tag: