> For the complete documentation index, see [llms.txt](https://docs.akenza.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.akenza.io/reference/api-documentation/querying-device-data/querying-aggregations/querying-concurrent-occupancy.md).

# 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](https://docs.akenza.io/reference/api-documentation/querying-device-data/querying-aggregations.md). It illustrates how to build a complex multi-stage aggregation pipeline. See [Occupancy monitoring](https://docs.akenza.io/analytics-use-cases/occupancy-monitoring.md) 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?

{% stepper %}
{% step %}

### 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.

```json
"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.
{% endstep %}

{% step %}

### 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

```json
"sourceStage": {
    "accumulator": "MATCHED_COUNT"
}
```

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

<figure><img src="https://2165942204-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MMKXTFIN5ZlLOjBlfC4%2Fuploads%2Fgit-blob-b3c765e336790c6c23d5cbfc573ceb79308f49bd%2Fconcurrent-occupancy-source-stage-matched-count.png?alt=media" alt="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"><figcaption><p>Result of the source stage: MATCHED_COUNT per desk and hour</p></figcaption></figure>
{% endstep %}

{% step %}

### 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:

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

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

<figure><img src="https://2165942204-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MMKXTFIN5ZlLOjBlfC4%2Fuploads%2Fgit-blob-a9cfb1a32963a614dfa48cb2d3e456f6582c2ed6%2Fconcurrent-occupancy-devices-stage-free-desks.png?alt=media" alt="Number of free desks per hour: 1 at 12h, 0 at 13h, 4 at 14h and 2 at 15h"><figcaption><p>Result of the devices stage: number of free desks per hour</p></figcaption></figure>
{% endstep %}

{% step %}

### 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.

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

For 7 April, the result is 0, the value at 13h.
{% endstep %}

{% step %}

### 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:

```json
"groupByTimeThenDevices": false
```

{% endstep %}
{% endstepper %}

## Full request

```json
{
    "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.

```json
{
    "devicesQueryMode": "AGGREGATED_ACROSS_DEVICES",
    "results": [
        {
            "metric": {
                "measurementType": "akenza/spaces/occupied/boolean",
                "topic": "occupancy",
                "dataKey": "occupied",
                "akenzaDeviceIds": [
                    "028e1956dc47dce6",
                    "02c8cb2a4f0ea684",
                    "026b2e374ce9dfaa",
                    "02e04f39578836fb"
                ]
            },
            "dataPoints": [4, 4, 4, 4, 2, 1, 0],
            "timestamps": [
                "2026-03-31T22:00:00Z",
                "2026-04-01T22:00:00Z",
                "2026-04-02T22:00:00Z",
                "2026-04-03T22:00:00Z",
                "2026-04-04T22:00:00Z",
                "2026-04-05T22:00:00Z",
                "2026-04-06T22:00:00Z"
            ]
        }
    ]
}
```

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


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.akenza.io/reference/api-documentation/querying-device-data/querying-aggregations/querying-concurrent-occupancy.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
