# Datastreams

Blynk.Cloud MQTT API uses Names to reference Datastreams.

{% hint style="warning" %}
Changing datastream names in the template settings can make your existing devices misbehave. If this happens, you can either revert your changes or update your device firmware Over the Air to conform to the new datastream structure.
{% endhint %}

## Send data to Blynk

Publish topic **ds/`DATASTREAM`**, payload: value in plain text (i.e. `123`, `hello` or `3.1415926`)

* `DATASTREAM` - the datastream name

{% hint style="info" %}
For multivalue (array-like) values, the individual items in the payload are separated using a `0x00` byte, i.e: `First\u0000Second\u0000Third`. The separator is a `NUL` character, that is also represented as `\u0000` in Unicode.
{% endhint %}

```bash
mosquitto_pub -h blynk.cloud -p 8883 -u device -P '{DEVICE_TOKEN}' -t 'ds/Temperature' -m '21.3'
```

## Send batched data to Blynk

Publish topic **batch\_ds**, payload: JSON-encoded object with datastream name as the key and datastream value as the value. The JSON value type must match with the datastream type. Use string for datastream with String data type, number for datastream with Integer, Double or Enum value type, `true` or `false` for datastream with Boolean data type and array of two numbers for datastream with Location data type (use longitude as the first array element and latitude as the second). Example:

```json
{
    "Name": "Sample Batch Uplink",
    "Temperature": 10.3,
    "Location": [30.523333, 50.450001],
    "Device On": true
}
```

```bash
mosquitto_pub -h blynk.cloud -p 8883 -u device -P '{DEVICE_TOKEN}' -t 'batch_ds' -m '{"Temperature": 23.1, "Humidity": 72}'
```

## Send timestamped batch to Blynk

Sometimes a device collects data in its own memory and needs to send multiple timestamped data points at once to reduce the number of TCP messages.

For these purposes, use the **ds/`DATASTREAM`/timestamped\_batch** topic. Send a comma-separated array of timestamps and corresponding values.

Example payload:

```json
[
  [1772639468034, 34.4],
  [1772639468341, 25.4],
  [1772639468974, 83.1]
]
```

The timestamp should be sent in milliseconds and no older than 1 month. Blynk will also accept points with a timestamp "in the future" within the next 1 month.

```bash
mosquitto_pub -h blynk.cloud -p 8883 -u device -P '{DEVICE_TOKEN}' -t 'ds/Temperature/timestamped_batch' -m '[[1772639468034, 22.5], [1772639468341, 23.0]]'
```

## Erase datastream value

Publish topic **ds/`DATASTREAM`/erase**, payload: empty

* `DATASTREAM` - the datastream name

```bash
mosquitto_pub -h blynk.cloud -p 8883 -u device -P '{DEVICE_TOKEN}' -t 'ds/Temperature/erase' -n
```

## Get data updates from Blynk

Subscribe to topic: **downlink/ds/`DATASTREAM`**

* `DATASTREAM` - the datastream name

{% hint style="success" %}
Usually, you'll want to subscribe to a widcard topic like **downlink/#** or **downlink/ds/#**.
{% endhint %}

```bash
mosquitto_sub -h blynk.cloud -p 8883 -u device -P '{DEVICE_TOKEN}' -t 'downlink/ds/MeasurementTimeout'
```

## Request the latest value from Blynk

Publish topic **get/ds**, payload: datastream names separated by comma (i.e. `Brightness,Color`)

The device will receive requested datastream values on **downlink/ds/`DATASTREAM`** topic.

```bash
mosquitto_pub -h blynk.cloud -p 8883 -u device -P '{DEVICE_TOKEN}' -t 'get/ds' -m 'Brightness,Color'
```

## Request current values for all datastreams

Publish topic **get/ds/all**, payload: empty

The device will receive requested datastream values on **downlink/ds/`DATASTREAM`** topic.

{% hint style="info" %}
For this to work, open `Datastream settings` and enable `Advanced Settings -> Sync with latest server value`.
{% endhint %}

```bash
mosquitto_pub -h blynk.cloud -p 8883 -u device -P '{DEVICE_TOKEN}' -t 'get/ds/all' -n
```


---

# Agent Instructions: Querying This Documentation

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

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.blynk.io/en/blynk.cloud-mqtt-api/device-mqtt-api/datastreams.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
