Need to calculate average of measurement in java microservice

we have option to calculate measurement average in Epl, as
using com.apama.aggregates.avg;
using com.apama.aggregates.last;
using com.apama.cumulocity.Measurement;

monitor HourlyAvgMeasurementDeviceContext {

event AverageByDevice {
string source;
float avgValue;
string unit;

action onload() {
// Subscribe to Measurement.CHANNEL to receive all measurements

from m in all Measurement(type="c8y_TemperatureMeasurement") within (3600.0) 
  group by m.source select
      last(m.measurements["c8y_TemperatureMeasurement"]["T"].unit)) as avgdata {
        send Measurement("", "c8y_AverageTemperatureMeasurement", avgdata.source, currentTime,
              "T": MeasurementValue(avgdata.avgValue, avgdata.unit, new dictionary<string,any>)
          }, new dictionary<string,any>) to Measurement.CREATE_CHANNEL;


i want to achieve the same thing in java microservice using the cumulocity sdk , i just want
the implementation of avg() and last() in java, please help with the code snippet

It’s pretty simple.

  1. Query the measurement data with a well defined filter (type, dateFrom, dateTo)
  2. Calculate the average and last value
  3. Create a new measurement with the series data you have calculated
  4. If you want to have this regularly, perform step 1-3 in a scheduler each day, hour etc.

I don’t think anyone will do the coding part for you (this community is not ChatGPT :grimacing:)

1 Like

@Stefan_Witschel ,

Thanks for your reply,

if i call measurementApi from the service, i will get recent response for that device, and if i enter the range also it will give me recent response.

but i wanted to know whether i can get the aggregated response for partciular measuremet for mentined range, So that i can avrg them,

So , if i could get how avg() fucntion is implemented in epl, it would solve my issue, in that sense , i asked whether we have avg() logic in java.

If you add a dateRange with dateFrom & dateTo you will retrieve multiple measurements not only the recent one.

No API supports the retrieval of an average value unfortunately.
I don’t think this API is implemented in the SDK but you can give it a try: Cumulocity IoT - OpenAPI Specification
It allows to you define the aggregation but it returns only min max values, not the average.

So I guess you have to implement/calculate that on your own as mentioned above.


Thanks stefan


Here’s a microservice that I wrote years ago which still works and should do what you want: SoftwareAG/cumulocity-measurement-aggregator: A simple microservice that perform aggregations of several measurements an time series from a static or dynamic group of devices. (
Basically it can aggregate the measurements of devices in static group or dynamic group.
It will start by doing a horizontal avg based on the give time interval, then it will perform a vertical avg on all the pre-aggregated measurements.
I’m still using it to calculate average signal quality for groups of devices (RSSI, SNR and noise).

1 Like