Sensorclouds
Step-by-Step Guides

Writing Payload Decoders

Worked examples for decoding JSON, binary, bit flags, and text payloads, and testing them

ADMIN ENGINEER

A decoder turns your device's payload into fields. This guide walks through the common cases with complete, tested examples. For the reference, see Payload Decoder.

The rules in one minute

  • Use the CUSTOM codec and edit the decoder on the profile's Payload Formatter tab.
  • Define decodeUplink(input) and return { data: { …fields } }.
  • input.bytes is an array of numbers 0–255. Buffer methods (readInt16BE, toString) don't exist.
  • Hex strings are converted to bytes first if they start with 0x, or have 8+ hex characters.
  • Throwing an error rejects the message (Decode failed in Live Data).

Example 1: JSON

The device sends {"temperature": 21.5, "humidity": 48}.

function decodeUplink(input) {
  var text = String.fromCharCode.apply(null, input.bytes);
  return { data: JSON.parse(text) };
}

Extend it: rename or convert fields before returning them.

function decodeUplink(input) {
  var d = JSON.parse(String.fromCharCode.apply(null, input.bytes));
  return {
    data: {
      temperature: d.t,                    // short key → readable name
      battery_v: d.batt_mv / 1000          // millivolts → volts
    }
  };
}

Example 2: Binary sensor reading

Layout (4 bytes, big-endian):

BytesMeaningEncoding
0–1TemperatureSigned 16-bit, °C × 100
2–3HumidityUnsigned 16-bit, % × 100
function decodeUplink(input) {
  var b = input.bytes;
  var t = (b[0] << 8) | b[1];
  if (t & 0x8000) t -= 0x10000;            // make it signed
  var h = (b[2] << 8) | b[3];
  return { data: { temperature: t / 100, humidity: h / 100 } };
}

Test: 0x091A1770 → { "temperature": 23.3, "humidity": 60 }

Negative test: 0xFF380000 → { "temperature": -2, "humidity": 0 }

Example 3: Bit flags

Byte 0 packs four on/off states.

function decodeUplink(input) {
  var f = input.bytes[0];
  return {
    data: {
      motorRunning: (f & 0x01) !== 0,
      doorOpen:     (f & 0x02) !== 0,
      alarmActive:  (f & 0x04) !== 0,
      batteryLow:   (f & 0x08) !== 0
    }
  };
}

Test: 0x05 → motorRunning and alarmActive are true, the others false.

Example 4: Text format

The device sends T:23.50|H:60.00|B:3.7.

function decodeUplink(input) {
  var text = String.fromCharCode.apply(null, input.bytes);
  var names = { T: "temperature", H: "humidity", B: "battery" };
  var data = {};
  text.split("|").forEach(function (part) {
    var kv = part.split(":");
    if (names[kv[0]]) data[names[kv[0]]] = parseFloat(kv[1]);
  });
  return { data: data };
}

Example 5: Several sensors in one message

Byte 0 is the sensor count. Then, for each sensor, 1 byte of ID and 2 bytes of signed value × 100.

function decodeUplink(input) {
  var b = input.bytes, data = {};
  for (var i = 0; i < b[0]; i++) {
    var o = 1 + i * 3;
    var v = (b[o + 1] << 8) | b[o + 2];
    if (v & 0x8000) v -= 0x10000;
    data["sensor_" + b[o]] = v / 100;
  }
  return { data: data };
}

Example 6: Readings with their own timestamp

If the device buffers readings, return a timestamp (Unix seconds or milliseconds, or ISO 8601). Add backfill: true for old data.

function decodeUplink(input) {
  var b = input.bytes;
  var ts = ((b[0] << 24) >>> 0) + (b[1] << 16) + (b[2] << 8) + b[3];   // Unix seconds
  var t = (b[4] << 8) | b[5];
  if (t & 0x8000) t -= 0x10000;
  return { data: { temperature: t / 100, timestamp: ts } };
}

Testing

Capture a real payload: open Live Data, click an event, and copy its Raw payload.

On the Payload Formatter tab, enter it in Sample Payload as hex (binary payloads) or base64 (JSON and text payloads), and click Run Test.

Check the Decoded Output, the Console Logs, and the Execution Time.

Test edge cases: negative values, maximum values, and short messages.

Guard against short messages so one bad packet doesn't fill Live Data with errors: if (input.bytes.length < 4) return { data: {} };

Common mistakes

MistakeSymptomFix
payload.readInt16BE(0)Decode failed: not a functionUse (b[0] << 8) | b[1]
Forgetting the sign-2 °C shows as 655.34Apply if (v & 0x8000) v -= 0x10000
Wrong byte orderValues look randomCheck the device's endianness
Returning a string numberCharts and alarms ignore itReturn parseFloat(...)
Short hex without 0xDecoder gets text charactersSend 0x… from the device

Next Steps

On this page