# LoRaWAN Custom Decoder

### Overview

The **LoRaWAN Custom Device Decoder** allows you to **write your own decoder function** to process incoming LoRaWAN uplink messages.&#x20;

This provides flexibility to handle **custom payload formats** that are not available as pre-built templates.

### How It Works

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

B -- No --> C{If Incoming Payload is Valid JSON?};
C -- Yes --> D\[Store JSON Data in Qubitro];
C -- No --> E\[Requires a Decoder Function];

B -- Yes --> F\[Apply Custom Decoder];
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:#e0e0e0,stroke:#000,color:#000;
style G fill:#e0e0e0,stroke:#000,color:#000;" %}

{% 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

JavaScript payload formatters allow you to write functions to encode or decode messages. These functions are executed using a **JavaScript ECMAScript 5.1 runtime**.

* **Avoid** non-trivial logic or polyfills; they may not be supported.
* Use arithmetic operations and bit shifts to convert binary data 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 %}

### Function Schema

A **decoder function** processes raw **uplink messages** and returns a **JSON object** containing the decoded fields.

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

{% hint style="danger" %}
The function name and parameter name cannot be modified.
{% endhint %}

#### Function Parameters

The decoder function has a single parameter: input. Qubitro automatically provides this parameter, which contains three properties:

* **Metadata**: A dynamic object containing metadata from the LNS provider (e.g., RSSI, SNR, timestamp). The structure varies depending on the provider.
* **fPort**: The LoRaWAN fPort used for the uplink.
* **Bytes**: A byte array representing the raw payload from the uplink message.

#### Example: Accessing Function Parameters

```javascript
function decoder(input) {
    var bytes = input.bytes;  // Get the raw payload
    var fPort = input.fPort;  // Get the fPort value
    var metadata = input.metadata; // Get metadata from the LNS provider
    return {};
}
```

#### Return Values

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

For example, if the payload represents temperature, the function can extract and return it as follows:

```javascript
function decoder(input) {
  const bytes = input.bytes;
  const fPort = input.fPort;
  const metadata = input.metadata;

  // Convert bytes to temperature value
  const temperature = ((bytes[0] << 8) + bytes[1]) / 100;

  return {
    "temperature": temperature,
    "metadata": metadata
  };
}
```

### 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 **LoRaWAN Custom Device** as the formatter type.
   {% endstep %}

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

1. Enter your **custom decoder function** in the editor.
2. Modify the formatter code to match your device’s payload.

<figure><img src="/files/owcEkBDFvVI0jaIRwZGX" alt="" width="563"><figcaption></figcaption></figure>
{% endstep %}

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

1. Click **Save and complete** to apply the decoder function.
2. The function will now **process all incoming data** for this device automatically.
   {% endstep %}
   {% endstepper %}

## FAQs

<details>

<summary>I created the function, but my device data is not decoded correctly. What should I do?</summary>

If your function is active but the data is not decoded correctly, check the following:

* Ensure that the payload format matches your decoder logic.
* Use the **Test** feature in the function editor to validate sample data.
* Check the **device logs** to see if raw data is being received.

If you need assistance, our team can **review your custom formatter code**. Open a [ticket](https://qubitro.gitbook.io/qubitro-documentation/help-center) from the Support Page.

</details>

<details>

<summary>I don’t see my device listed. Can Qubitro help me?</summary>

Absolutely! If your device does not appear after setting up the integration, **open a** [**support ticket**](https://qubitro.gitbook.io/qubitro-documentation/help-center), and our team will assist you in troubleshooting the issue.

</details>

<details>

<summary>Can I use a custom decoder with any LNS provider?</summary>

Yes! Qubitro’s **LoRaWAN Custom Device Decoder** works with **any LNS provider**, including **Loriot, The Things Stack, and ChirpStack**. As long as the device sends data, your decoder function will process it.

</details>

<details>

<summary>Can the Qubitro team help me write a custom decoder?</summary>

While Qubitro provides flexibility for writing custom decoders, our team can **review your formatter code** to ensure compatibility and suggest improvements. Feel free to reach out via the [Support Page](https://qubitro.gitbook.io/qubitro-documentation/help-center).

</details>


---

# 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/lorawan-custom-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.
