Enable Notifications Need help with APAMA or EPL in calculating daily average

What product/components do you use and which version/fix level are you on?

10.11.25

Is your question related to the free trial, or to a production (customer) instance?

production

What are you trying to achieve? Please describe it in detail.

My measurement data is flowing to a device in Cumulocity every minute. I need to create a new measurement in the same device which shows the daily average of my measurement (one data point everyday) . Can I use APAMA or EPL apps to do this logic? Any suggestion how to achieve this?

Do you get any error messages? Please provide a full error message screenshot and log file.

Have you installed all the latest fixes for the products and systems you are using?

1 Like

Hi @saif.hasn,

here is an example for an Analytics Builder Model calculating an average. In this case it is an average per minute.
That should help you get started.
Be aware that a model should always create data in a separate measurement-type, otherwise it is looping the data to itself.
Average.zip (944 Bytes)

Regards
Kai

1 Like

Hi Saif,

Yes that is absolutely possible.

If you wanted a ‘sliding window’ type metric where the average is updated on each new measurement, you can use streams and queries.

// Declare a global variable to hold the result of the average calculation

float avg;

// This stream calculates the average over a 24h sliding window

stream<Measurement> measurements := from m in all Measurement(source=source) within 86400.0 select mean(m.value):avg {};

// Then send the measurement once per day using cron notation (using midnight as an example)

on all at (0,0,*,*,*) {
    send Measurement(..., avg, ...) to Measurement.SEND_CHANNEL;
}

Alternatively, if you aren’t bothered about the exact time of day that the average is output as a measurement, you can make use of batched windows to evaluate and output the average every 24 hours after startup. This has the advantage of not requiring a global variable and being a shorter and more elegant solution while lacking the fine grained control of the one above.

from m in all Measurement(source=source)  within 86400.0 every 86400.0 select mean(m.value)  : avg {
    send Measurement(..., avg, ...) to Measurement.SEND_CHANNEL;
}

Some doc links with more detail:
This page in the Cumulocity docs shows a similar example for calculating the mean of the last hour.
This page in the Apama documentation describes how to batch events in EPL.
This page in the Apama documentation shows how you can use time based triggers like the cron one above.
This page in the Apama documentation describes how to define time based windows in stream queries.

3 Likes