> ## Documentation Index
> Fetch the complete documentation index at: https://docs.cognite.com/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> Cognite Data Fusion (CDF) is an industrial DataOps platform. Prefer English docs unless the user asks for another locale.
> Follow /_llms/ index links recursively until you reach page URLs ending in .md. Fetch those Markdown twins instead of HTML.
> For REST APIs, default to calendar version 20230101 (stable). Use 20230101-beta or 20230101-alpha only if the user is on a preview API. Cite paths under /api-reference/concepts/<version>/ and the Cdf-Version header. Prefer the Python SDK (cognite-sdk) unless the user is in JavaScript/TypeScript.
> Start here by task: platform overview /cdf/index.md; data modeling /cdf/dm/index.md; data integration /cdf/integration/index.md; access /cdf/access/index.md; CDF Toolkit /cdf/deploy/cdf_toolkit/index.md; REST quickstart /dev/quickstart.md; Python SDK https://cognite-sdk-python.readthedocs-hosted.com/en/stable/; Atlas AI /cdf/atlas_ai/index.md; plan an Atlas AI agent /cdf/atlas_ai/concepts/atlas_ai_agent_planning.md; Flows apps /cdf/flows/index.md; MCP and IDE setup /dev/guides/ide_ai_integration.md.
> For implementation work, also load /skill.md.

# Ingesting state time series with hosted extractors

> Learn how to ingest state time series with a hosted extractor in Cognite Data Fusion (CDF). Create a state set and a state time series in data modeling, then map source messages to state data points.

Use a **custom mapping** to ingest equipment states, such as `CLOSED`, `OPEN`, and `TRANSITIONING`, into a [state time series](https://docs.cognite.com/dev/concepts/resource_types/state_timeseries.md) in Cognite Data Fusion (CDF). Each state has a numeric code and a string label in a **state set**. Multiple time series can share the same state set.

> [!NOTE]
>
> This feature is in [public preview](https://docs.cognite.com/cdf/product_feature_status.md#public-preview) and may change.

The mapping uses `type: "datapoint"` with `value` set to a state object that contains `numericValue`, `stringValue`, or both. Built-in message formats don't write state data points, and the hosted extractor doesn't create state sets or state time series, so create them before you start the **job**.

State time series exist only in data modeling. This procedure doesn't convert an existing numeric or string time series.

## Before you start

* Set up a [hosted extractor source, destination, and job](https://docs.cognite.com/cdf/integration/guides/extraction/hosted_extractors.md).
* Create the spaces in this procedure, `state_definitions` and `industrial_assets`, or use spaces you already have. Keep the same space and external ID in every step.
* The user or client that creates the instances needs `datamodelinstances:WRITE` on those spaces. Verification needs `datamodelinstances:READ` and `timeseries:READ`. The destination credentials need `timeseries:READ`, `timeseries:WRITE`, and `datamodelinstances:WRITE`. See [Access capabilities](https://docs.cognite.com/cdf/integration/guides/extraction/hosted_extractors.md#access-capabilities).
* Choose an external ID that isn't already used by a numeric or string time series.

## Configure state ingestion

Follow these steps to ingest the state of valve `valve_001`. Replace the example spaces and external IDs consistently throughout the procedure.

**1. Create the state set**

Send this request to the data modeling API in your target project. The state set defines the valid codes and labels for the valve.

```http theme={"languages":{"custom":["/_languages/kuiper.json"]}}
POST /api/v1/projects/{project}/models/instances
Content-Type: application/json

{
  "items": [
    {
      "space": "state_definitions",
      "externalId": "valve_states",
      "instanceType": "node",
      "sources": [
        {
          "source": {
            "type": "view",
            "space": "cdf_cdm",
            "externalId": "CogniteStateSet",
            "version": "v1"
          },
          "properties": {
            "name": "Valve Position States",
            "states": [
              { "numericValue": 0, "stringValue": "CLOSED" },
              { "numericValue": 1, "stringValue": "OPEN" },
              { "numericValue": 2, "stringValue": "TRANSITIONING" }
            ]
          }
        }
      ]
    }
  ]
}
```

Wait for the request to succeed before creating the time series. A state set supports at most 100 states, with unique numeric codes and unique string labels. See [State constraints](https://docs.cognite.com/dev/concepts/resource_types/state_timeseries.md#state-constraints) for value limits.

**2. Create the state time series**

Create a `CogniteTimeSeries` instance with `type: "state"` and a `stateSet` relation to the `state_definitions/valve_states` state set.

```http theme={"languages":{"custom":["/_languages/kuiper.json"]}}
POST /api/v1/projects/{project}/models/instances
Content-Type: application/json

{
  "items": [
    {
      "space": "industrial_assets",
      "externalId": "valve_001_state",
      "instanceType": "node",
      "sources": [
        {
          "source": {
            "type": "view",
            "space": "cdf_cdm",
            "externalId": "CogniteTimeSeries",
            "version": "v1"
          },
          "properties": {
            "name": "Valve 001 Position",
            "type": "state",
            "stateSet": {
              "space": "state_definitions",
              "externalId": "valve_states"
            }
          }
        }
      ]
    }
  ]
}
```

Wait for the request to succeed before starting data point ingestion.

**3. Define the custom mapping**

Configure a custom mapping with JSON input. This example source message reports that the valve is open:

```json theme={"languages":{"custom":["/_languages/kuiper.json"]}}
{
  "tag": "valve_001_state",
  "timestamp": 1794484800000,
  "stateCode": 1,
  "stateLabel": "OPEN"
}
```

Use this mapping expression:

```kuiper theme={"languages":{"custom":["/_languages/kuiper.json"]}}
{
  "type": "datapoint",
  "space": "industrial_assets",
  "externalId": input.tag,
  "timestamp": input.timestamp,
  "value": {
    "numericValue": input.stateCode,
    "stringValue": input.stateLabel
  }
}
```

`space` and `externalId` identify the time series instance. `timestamp` is a Unix timestamp in milliseconds. For a state data point, set `value` to an object with `numericValue`, `stringValue`, or both. When both are present, the code and the label must correspond to the same state in the state set. The state object marks the output as a state data point. The mapping doesn't change the time series type in CDF.

The state object has these constraints:

* `numericValue` must be an integer that fits in 32 bits, from -2147483648 to 2147483647.
* `stringValue` must be a string.
* At least one of the two fields must be non-null.
* The object can't contain other fields.

If the source sends only a numeric code, omit `stringValue` from the state object. If the source sends only a label, omit `numericValue`.

Send state values only to state time series. If a batch for one time series mixes state values with numbers or strings, the extractor logs a warning and skips the values that don't match the time series type.

For mapping syntax and handling multiple values in a message, see [Custom message formats](https://docs.cognite.com/cdf/integration/guides/extraction/hosted_extractors/hosted_extractors_custom_mappings.md).

**4. Use the mapping in a job**

In the job configuration, set the message format to the custom mapping you defined. Use the source and destination from [Before you start](#before-you-start). Start the job, then send the example message from the source.

In [job status and metrics](https://docs.cognite.com/cdf/integration/guides/extraction/hosted_extractors.md#jobs), confirm that the job received the example message and that **Destination uploaded values** increased, with no transform or destination failures.

**5. Verify the ingested data point**

Retrieve the state data point by instance ID. This request covers the timestamp in the example message:

```http theme={"languages":{"custom":["/_languages/kuiper.json"]}}
POST /api/v1/projects/{project}/timeseries/data/list
Content-Type: application/json

{
  "items": [
    {
      "instanceId": {
        "space": "industrial_assets",
        "externalId": "valve_001_state"
      },
      "start": 1794484800000,
      "end": 1794488400000,
      "limit": 10
    }
  ]
}
```

Confirm that the response contains `numericValue: 1` and `stringValue: "OPEN"` at timestamp `1794484800000`.

If the data point is missing, check that the mapping uses the correct instance space and external ID. Confirm that the time series has `type: "state"` and references `state_definitions/valve_states`. Check job logs for mapping or destination errors:

* If the state time series doesn't exist, the extractor drops its data points and logs a warning. Other data points in the same batch are still written.
* If the target time series isn't a state time series, the extractor drops the state data points. Other data points in the same batch are still written.

## Send a state data point with Bad status

If the source can't provide a state value, set `value` to null and set `status` to a `Bad` status symbol. For example, the mapping output looks like this:

```json theme={"languages":{"custom":["/_languages/kuiper.json"]}}
{
  "type": "datapoint",
  "space": "industrial_assets",
  "externalId": "valve_001_state",
  "timestamp": 1794484860000,
  "value": null,
  "status": "Bad"
}
```

Don't use a state object with both fields set to null. The extractor rejects it. See [Data point status codes](https://docs.cognite.com/dev/concepts/reference/status_codes.md).

A null value doesn't show the type of the time series. The extractor recognizes a time series as a state time series after it has written at least one state data point to it. Until then, a batch with only null values for that time series is treated as numeric and can fail. Ingest a state data point with a value first.

## Maintain the state definitions

Add a code and label to the state set before you send a data point that uses them. CDF validates state data points against the state set during ingestion and rejects a data point whose code or label isn't in the set. If you change the label for a numeric code, queries return the new label for historical data points as well. If you remove a state, historical points keep the numeric code and can omit the label. See [State set management](https://docs.cognite.com/dev/concepts/resource_types/state_timeseries.md#state-set-management).

To analyze how long the valve stays in each state or how often it changes state, see [Retrieve state aggregates](https://docs.cognite.com/dev/concepts/resource_types/state_timeseries.md#retrieve-state-aggregates).
