For the complete documentation index, see llms.txt. This page is also available as Markdown.

Querying concurrent occupancy

Query concurrent desk occupancy with the akenza REST API, using a step-by-step example of a multi-stage aggregation.

Concurrent occupancy counts how many desks are occupied at the same time. This page shows how to compute it with the aggregation API. It illustrates how to build a complex multi-stage aggregation pipeline. See Occupancy monitoring for how concurrent occupancy compares to time-based occupancy.

Example: minimum daily spare capacity

This example answers the question "What was the minimum daily spare capacity of desks over the last 7 days?". In other words: for each of the last 7 days, what was the lowest number of free desks at any hour of that day?

1

Select the data with match

The example queries four desk sensors by device ID, over one week. The occupancy value is in the occupancy topic, under the occupied data key.

"match": {
    "devices": {
        "deviceIds": [
            "028e1956dc47dce6",
            "02c8cb2a4f0ea684",
            "026b2e374ce9dfaa",
            "02e04f39578836fb"
        ]
    },
    "timestamps": {
        "from": "2026-03-31T22:00:00.000Z",
        "to": "2026-04-07T21:59:59.000Z"
    },
    "dataPointSelector": {
        "dataKey": "occupied",
        "topic": "occupancy"
    }
}

You can also select devices by tag instead of listing device IDs.

The time range covers 1 to 7 April 2026 in Central European Summer Time (UTC+2). Midnight local time is 22:00 UTC on the previous day.

2

Extract the reported occupancy boolean with sourceStage

The calculation is based on whether the sensor sent at least one occupancy.occupied=true sample within the hour. This can be observed by counting the number of sample = true sent during the hour, using the accumulator MATCHED_COUNT

  • e.g. if occupancy.occupied=true was sent three times during the hour: at 10h15, 10h35, 10h55 => then MATCHED_COUNT returns 3

  • OR if occupancy.occupied=false was sent several times => MATCHED_COUNT returns 0

"sourceStage": {
    "accumulator": "MATCHED_COUNT"
}

This stage returns one value per desk and hour, hence we can represent it as a table:

Table of desks A to D from 12h to 15h. Each cell shows whether the desk is occupied or free and its MATCHED_COUNT for that hour
Result of the source stage: MATCHED_COUNT per desk and hour
3

Count free desks with devicesStage

Next, count how many desks were free in each hour. A desk counts as free when it never reports occupancy.occupied=true for an hour (meaning MATCHED_COUNT=0). Use the COUNT_IF accumulator with a filter:

"devicesStage": {
    "accumulator": "COUNT_IF",
    "filter": {
        "predicate": "EQUALS",
        "value": 0
    }
}

This stage returns one value per hour for the whole group of desks:

Number of free desks per hour: 1 at 12h, 0 at 13h, 4 at 14h and 2 at 15h
Result of the devices stage: number of free desks per hour
4

Take the daily minimum with timeStage

Finally, we have to aggregate the hourly counts over time. The original question was "What was the minimum daily spare capacity of desks over the last 7 days?". Hence we will use the MIN accumulator with a daily interval.

"timeStage": {
    "accumulator": "MIN",
    "interval": "P1D"
}

For 7 April, the result is 0, the value at 13h.

5

Aggregate across devices first

This query aggregates across devices first and over time second. akenza does the opposite by default, so set the flag explicitly:

"groupByTimeThenDevices": false

Full request

{
    "match": {
        "devices": {
            "deviceIds": [
                "028e1956dc47dce6",
                "02c8cb2a4f0ea684",
                "026b2e374ce9dfaa",
                "02e04f39578836fb"
            ]
        },
        "timestamps": {
            "from": "2026-03-31T22:00:00.000Z",
            "to": "2026-04-07T21:59:59.000Z"
        },
        "dataPointSelector": {
            "dataKey": "occupied",
            "topic": "occupancy"
        }
    },
    "aggregation": {
        "sourceStage": {
            "accumulator": "MATCHED_COUNT"
        },
        "devicesStage": {
            "accumulator": "COUNT_IF",
            "filter": {
                "predicate": "EQUALS",
                "value": 0
            }
        },
        "timeStage": {
            "accumulator": "MIN",
            "interval": "P1D"
        },
        "groupByTimeThenDevices": false
    }
}

Example response

The response covers the full week, not only the hours shown in the figures above.

From a business perspective, this can be interpreted as follows:

Day
Minimum free desks
Meaning

April 1-4

4

All 4 desks were free in every hour of the day.

5 April

2

In the busiest hour, only 2 desks were free.

6 April

1

In the busiest hour, only 1 desk was free.

7 April

0

In at least one hour, no desk was free.

Last updated

Was this helpful?