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?
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.
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=truewas sent three times during the hour: at 10h15, 10h35, 10h55 => then MATCHED_COUNT returns 3OR if
occupancy.occupied=falsewas 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:

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:

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.
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:
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?