# Hex Payload Decoder

### Overview

The **Hex Payload Decoder** allows you to **convert incoming hex string payloads into valid JSON data** for non-LoRaWAN data sources.

{% hint style="info" %}
The **Hex Payload Decoder** is only applicable for **non-LoRaWAN data sources**, such as [MQTT](/data-sources/mqtt.md), [HTTP](/data-sources/http.md), or [Onomondo](/data-sources/no-code-integrations/onomondo.md). LoRaWAN devices use [**Device Templates**](/platform/functions/decoder-function/lorawan-device-template-decoder.md) or [**Custom Decoder**](/platform/functions/decoder-function/lorawan-custom-decoder.md) for decoding.
{% endhint %}

### How It Works

{% @mermaid/diagram content="graph LR;
A\[Non-LoRaWAN Device Sends Hex Data] --> B{Decoder Function Available?};

B -- No --> C\[Data Cannot Be Processed];
B -- Yes --> F\[Convert Hex to JSON];

F --> G\[Store Decoded Data in Qubitro];

%% Style Adjustments
style A fill:#d9d9d9,stroke:#000,color:#000;
style B fill:#f2f2f2,stroke:#000,color:#000;
style C fill:#f2f2f2,stroke:#000,color:#000;
style F fill:#CBC3E3,stroke:#000,color:#000;  %% Purple from previous diagrams
style G fill:#CBC3E3,stroke:#000,color:#000;  %% Purple from previous diagrams" %}

{% hint style="warning" %}
If a [Transformation Function](https://qubitro.gitbook.io/qubitro-documentation/platform/functions/transformation-function) is enabled, data will be processed before storage. The decoder applies first, and then any defined transformation function modifies the data before it is stored in Qubitro.
{% endhint %}

### Tips

* **Avoid** unnecessary logic or external dependencies that could slow down execution.
* **Use** arithmetic operations and bit shifts to convert hex values into readable fields.
* Keep payload formatters **simple** and lightweight.

{% hint style="success" %}
Need help reviewing your custom formatter? Our team can assist in checking your code for compatibility. Reach out via the [Support Page](https://qubitro.gitbook.io/qubitro-documentation/help-center).
{% endhint %}

### Required Input Format

Data must be published as JSON with the following structure:

```json
{ "payload_hex": "hex_value" }
```

{% hint style="warning" %}
**The key must be `"payload_hex"`** in the incoming JSON payload. Any other key name will not be recognized by the system.
{% endhint %}

### Function Schema

A decoder function processes **incoming hex payloads** and returns a structured JSON object.

#### Function Parameters

The decoder function receives a **single parameter: `input`**, which contains:

* **bytes** → A byte array representing the raw payload received.

```javascript
function decoder(input) {
  // Your decoding logic here
}
```

#### Return Values

{% hint style="warning" %}
To ensure compatibility with Qubitro’s no-code suite, the output should be a key-value JSON object.
{% endhint %}

### Example

{% stepper %}
{% step %}
**Incoming payload**

```json
{ "payload_hex": "7B2274656D7065726174757265223A32352C2268756D6964697479223A36302C227072657373757265223A313031332C2262617474657279223A39307D" }
```

{% endstep %}

{% step %}
**Formatter code**

```javascript
function decoder(input) {
    
    // Extract values from the input bytes array

    // First byte represents temperature
    let temperature = input.bytes[0];  

    // Second byte represents humidity
    let humidity = input.bytes[1];     

    // Third & Fourth bytes represent pressure (16-bit value)
    let pressure = (input.bytes[2] << 8) | input.bytes[3]; 

    // Fifth byte represents battery level
    let battery = input.bytes[4];      

    // Return an object containing the decoded values
    return { temperature, humidity, pressure, battery };
}
```

{% endstep %}

{% step %}
**Decoded payload**

```json
{
  "temperature": 123,
  "humidity": 34,
  "pressure": 29797,
  "battery": 109
}
```

{% endstep %}
{% endstepper %}

### Creating a Custom Decoder Function

{% stepper %}
{% step %}
**Select the Function Type**

1. Open the **device details page** in the Qubitro Portal.
2. Click the **Functions** tab.
3. Click **Create Function** and select **Decoder Function**.
4. Choose **Hex Payload Decoder** as the formatter type.
   {% endstep %}

{% step %}
**Write the Decoder Code**

1. Enter your custom decoder function in the editor.
2. Adjust the function to match your **device’s payload format**.
   {% endstep %}

{% step %}
**Save and Apply the Function**

1. Click **Save** to apply the decoder function.
   {% endstep %}
   {% endstepper %}

{% hint style="warning" %}
Once saved, **Qubitro will automatically apply the decoder** to all incoming data for the assigned device.
{% endhint %}


---

# 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.qubitro.com/platform/functions/decoder-function/hex-payload-decoder.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.
