> For the complete documentation index, see [llms.txt](https://docs.tricloudnexus.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.tricloudnexus.io/management-portal/designer/assets/tags/collect/configuration/mqtt.md).

# MQTT

## MQTT Tag Configuration (Read & Write)

This section explains how to configure Tags that leverage [**MQTT Data Connectors**](/management-portal/designer/assets/data-connectors/mqtt.md) to either **read (subscribe)** from topics on an MQTT broker or **write (publish)** data to topics on the broker.

Configuring MQTT Tags allows you to integrate live data from devices, sensors, or external systems into your Tricloud Nexus asset model, or push processed data from your Tags back out to other systems or [Unified Namespace](/platform-architecture/unified-namespace.md).

***

### Prerequisites

Before configuring a Tag to subscribe or publish to an MQTT broker, ensure that:

* An [**Asset Hierarchy**](/management-portal/designer/assets/asset-hierarchies.md) has been created with at least one [**Area**](/management-portal/designer/assets/asset-hierarchies/areas.md) and one [**Asset**](/management-portal/designer/assets/asset-hierarchies/assets.md) as a child of that Area.
* An [**MQTT Data Connector**](/management-portal/designer/assets/data-connectors/mqtt.md) with at least one Topic has been configured on the Area.
* You have access to the MQTT broker and know the topic structure.

***

## Creating a Tag to Read from an MQTT Broker

When creating a Tag that subscribes to MQTT data, decide **what part of the payload** should be stored as the Tag's measurement value. Some common scenarios include:

* **Store the entire payload** as a String measurement.
* **Extract a specific numeric reading** (e.g., temperature) to store as an Analog measurement.
* **Extract a status or state flag** to store as a Digital measurement (e.g., `true` for ON, `false` for OFF).

### Steps to Create a Read Tag

1. [**Create the Tag**](/management-portal/designer/assets/tags.md)
   * Choose the desired Tag type (**Analog**, **Digital**, or **String**).
   * Provide a descriptive name.
   * Set **Access** to **Read**.
2. **Select the Data Connector**

   * In the **Data Connection** dropdown, select the MQTT broker configured on the parent Area.
   * In the **Read Topic** dropdown, select the topic you want to subscribe to.\
     \&#xNAN;*(If no topics are available, configure them in the* [*MQTT Data Connector*](/management-portal/designer/assets/data-connectors/mqtt.md) *settings.)*

   <figure><img src="/files/vGWvBnvLAzQGMFhCPFM8" alt="" width="521"><figcaption><p>Select the Data Connection and Topic to Read from</p></figcaption></figure>
3. **Define the Value Expression**
   * Click the editor button next to the **Value Expression** field.
   * In the **Query Window**, use a JSONata expression to extract the desired value from the MQTT payload. Your expression must match the selected Tag type (see [MQTT Query Editor](/management-portal/designer/assets/tags/collect/configuration/mqtt/mqtt-query-editor.md) for details). In this example below, we just take use the entire payload as is, by using the `$` chharacter in the JsonAta Query editor textbox:

     <figure><img src="/files/tk9gdO3Fvr6mImRRpqW6" alt=""><figcaption><p>JsonAta Query Editor</p></figcaption></figure>
4. **Configure the Timestamp Source**\
   Choose how measurement timestamps are generated:
   * **Measurement generation timestamp** – use the UTC timestamp when the MQTT message was received.
   * **Retrieve timestamp from MQTT payload** – extract an ISO8601 timestamp from the payload using the [Query Editor](/management-portal/designer/assets/tags/collect/configuration/mqtt/mqtt-query-editor.md).\
     In the example below, we are reading the *orderCompletion* timestamp property of the Json Payload as a Local timestamp in the Europe/Copenhagen timezone, and converting that into UTC timestamp in ISO8601 format.\\

     <figure><img src="/files/AH019uiMxzfGak3EqX04" alt=""><figcaption><p>Reading local timestamp from message, and converting it into UTC for all measurements generated from the Tag</p></figcaption></figure>

***

## Creating a Tag to Write to an MQTT Broker

Write Tags allow you to publish measurement values from one Tag to an MQTT broker.

### Steps to Create a Write Tag

1. **Create the Tag**
   * Choose the desired Tag type (**Analog**, **Digital**, or **String**).
   * Provide a descriptive name.
   * Set **Access** to **Write**.
2. **Select the Source and Destination**
   * In **Source Tag**, select the Tag whose measurements should be published.
   * In **Destination Connection**, select the MQTT broker.
   * In **Write Topic**, select the topic to publish to.\
     \&#xNAN;*(If no topics are available, configure them in the MQTT Data Connector settings.)*
3. **Define the Value Expression**

   * By default, the published payload will contain the standard measurement JSON:

     ```json
     {
       "id": id,
       "name": name,
       "timeGenerated": timeGenerated,
       "startTimestamp": startTimestamp,
       "endTimestamp": endTimestamp,
       "quality": quality,
       "type": type,
       "value": value
     }
     ```

   <figure><img src="/files/j8whHeY5VfQCHGGo84Z4" alt=""><figcaption><p>Default Measurement Json format</p></figcaption></figure>

   * You can edit this Json structure by clicking the editor button next to the Value Expression field and adjusting it in the [Query Editor](/management-portal/designer/assets/tags/collect/configuration/mqtt/mqtt-query-editor.md).\
     In the example below the following JsonAta query is used\\

     ```json
     {
       "id": id,
       "name": name,  
       "timestamp": startTimestamp,
       "data": $eval(value)
     }
     ```

     \
     This query removes the *timeGenerated*, *startTimestamp*, *endTimestamp*, *quality* and *type* as properties to the publish Json mpayload for the published message. Additionally, it creates a new property called *data*, where the value of the measurement is set.\\

     <figure><img src="/files/ghnW7FrrmJlGGPp3P9so" alt=""><figcaption><p>Example of editing the Json sructure to publish on MQTT Topic</p></figcaption></figure>

***
