---
title: state_agg() | Tiger Data Docs
description: Aggregate state data into a state aggregate for further analysis
---

Since [1.15.0](https://github.com/timescale/timescaledb-toolkit/releases/tag/1.15.0)

Aggregate state data into a state aggregate to track state transitions. Unlike [`compact_state_agg`](/docs/reference/toolkit/state-tracking/compact_state_agg/index.md), which only stores durations, `state_agg` also stores the timestamps of state transitions.

## Samples

Create a state aggregate to track the status of some devices.

```
SELECT state_agg(time, status) FROM devices;
```

## Arguments

The syntax is:

```
state_agg(
  ts TIMESTAMPTZ,
  value {TEXT | BIGINT}
) RETURNS StateAgg
```

| Name  | Type        | Default | Required | Description                                   |
| ----- | ----------- | ------- | -------- | --------------------------------------------- |
| ts    | TIMESTAMPTZ | -       | ✔        | Timestamps associated with each state reading |
| value | TEXT        | BIGINT  | -        | ✔                                             |

## Returns

| Column | Type     | Description                                                                                  |
| ------ | -------- | -------------------------------------------------------------------------------------------- |
| agg    | StateAgg | An object storing the periods spent in each state, including timestamps of state transitions |
