Decoder for MQTT device profiles

For MQTT device profiles, the decode function accepts the following parameter:

function decode(message, topic) {
  var decoded = {
    serialNumber: null,
    objects: []
  };
  // Place your code here and push the results to 'decoded.objects'.
  return decoded;
}

message contains the MQTT message as a string. You’ll need to parse the value correctly in order to extract the required information.

topic contains the topic of the MQTT message.

Please note that the message parameter can contain data in different formats, for example plain text, JSON or binary data. XML data will automatically be converted to JSON before being passed to the decoder.

You can use JSON.parse() to parse JSON data:

function decode(message, topic) {
  var data = JSON.decode(message);
  var decoded = {
    serialNumber: null,
    objects: []
  };
  // Place your code here and push the results to 'decoded.objects'.
  return decoded;
}

The decode function must return an object with an objects array and an optional serial number:

{
  serialNumber: null,
  objects: [{
    key: "data-point-key-abc",
    value: 23.5,
    datetime: '2025-05-21T06:21:16.138Z',
  }, {
    key: "data-point-key-xyz",
    value: 21.8,
    datetime: '2025-05-21T06:21:16.138Z',
  }]
}
serialNumber

If the device sends its own serial number, you can pass it to this property to automatically update the serial number of the device in the database. Set it to null if you don’t want to update the serial number.

objects

An array of objects with the following properties:

key

The key of the corresponding data point as defined above in the Data Points table in the Key column.

../../../_images/edit-iot-profiles-keys.png
datetime

Date and time of the measurement. Must be a string according to ISO 8601 (Date.prototype.toISOString()). Can either be set to new Date().toISOString() or parsed from the message.

value

The actually recorded measurement as an integer, floating point or truth value.

Test Payload

To test the decoding algorithm, enter the expected payload in the Payload field and the topic in the Topic field (if applicable) and then click on the Test button.

If you enter plain text, JSON or binary data, it will be passed directly to the decode function, and the result will be shown in the Result field.

If you enter XML data, it will automatically be converted to JSON. The JSON data will be shown in the JSON field and passed to the decode function. The result will be shown in the Result field.

If there are errors in the payload or in the decoder function, they will be shown in the Result field as well.

Complete example

function decode(message, topic) {
  // Decode a JSON string to an object.
  var data = JSON.parse(message);
  var now = new Date().toISOString();
  var measurements = [];

  // Helper function to generate a measurement object.
  // Change the properties on `item` according to your message format.
  function createMeasurement(item) {
    return {
      key: item.key,
      datetime: item.datetime || now,
      value: item.value
    };
  }

  // Add the decoded values to the objects array.
  // Example:

  // Convert a single item from the JSON object to a measurement object,
  // assuming the item is stored in data.items[0].
  measurements.push(createMeasurement(data.items[0]));

  // Convert all items from the JSON object to measurement objects,
  // assuming the items are stored in data.items.
  data.items.forEach(function(item) {
    measurements.push(createMeasurement(item));
  });

  // Create a measurement object manually.
  measurements.push({key: 'key_1', datetime: now, value: data.key_1.value});

  return {
    serialNumber: data.serialNumber
    objects: measurements;
  };
}