Sensorclouds Docs
Device Profiles

Payload Decoder

Write JavaScript decoders to transform raw device data into structured JSON

The payload decoder transforms raw device data (bytes) into structured JSON that the platform can store and visualize.

Payload formatter

Codec Types

  • NONE: Raw data passthrough — the platform expects valid JSON payloads
  • CUSTOM: A JavaScript decoder script processes raw bytes into JSON

Writing a Custom Decoder

Three function signatures are supported. Choose the one that fits your needs.

function decodeUplink(input) {
  // input.bytes: Uint8Array of raw payload
  // input.fPort: LoRaWAN port number (if applicable)
  return {
    data: {
      temperature: (input.bytes[0] << 8 | input.bytes[1]) / 100,
      humidity: input.bytes[2]
    }
  };
}
function Decoder(bytes, port) {
  return {
    temperature: (bytes[0] << 8 | bytes[1]) / 100,
    humidity: bytes[2]
  };
}
function decode(bytes, port) {
  return {
    temperature: (bytes[0] << 8 | bytes[1]) / 100
  };
}

Built-in Templates

The platform provides starter templates you can use and modify:

  1. Temperature & Humidity (TTN v3) — 3-byte sensor reading
  2. Temperature & Humidity (Legacy) — Legacy decoder format
  3. GPS Coordinates — 8-byte latitude/longitude
  4. Multi-Sensor — Temperature + humidity + battery level
  5. Bitwise Operations — Packed bitfield extraction
  6. Simple Decode — Minimal example

Testing Decoders

Use the built-in Test Decoder feature on the profile page:

  1. Write or paste your decoder function in the code editor
  2. Enter a sample hex payload in the test input (e.g., 09D53C)
  3. Click Test
  4. Verify the decoded output matches your expected values

Always test your decoder with multiple payloads before assigning the profile to production devices.

Hex Auto-Detection

If the MQTT payload is a pure hexadecimal ASCII string, the platform automatically converts it to bytes before passing it to the decoder.

Example: "48656C6C6F"[0x48, 0x65, 0x6C, 0x6C, 0x6F]

Security Constraints

Decoder scripts run in an isolated V8 sandbox with strict limits:

ConstraintLimit
Max script size40 KB
Execution timeout5 seconds
Memory limit128 MB
Filesystem accessNone
Network accessNone
Process accessNone

Decoders that exceed the timeout or memory limit are terminated. Keep your decoder logic simple and efficient.

On this page