# How to make a payload readable?

**URL:** <https://www.thethingsnetwork.org/forum/t/how-to-make-a-payload-readable/33364>\
**Category:** Application Development\
**Created:** [January 14, 2020, 10:33am UTC](https://www.thethingsnetwork.org/forum/t/how-to-make-a-payload-readable/33364 "2020-01-14T10:33:01Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![arjanvanb](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/arjanvanb/32/46626_2.png) [@arjanvanb](https://www.thethingsnetwork.org/forum/u/arjanvanb)\
**Post date:** [January 14, 2020, 11:34am UTC](https://www.thethingsnetwork.org/forum/t/how-to-make-a-payload-readable/33364/2 "2020-01-14T11:34:16Z")

</div>

Assuming you’re sure that 0x3411 decodes to decimal 3,411 (and [not to decimal 13,329](https://www.rapidtables.com/convert/number/hex-to-decimal.html?x=3411)), then the node is using [binary-coded decimal](https://en.wikipedia.org/wiki/Binary-coded_decimal). See [Is there any documentation on payload functions?](https://www.thethingsnetwork.org/forum/t/is-there-any-documentation-on-payload-functions/3441) for examples about conversions, including BCD.

In JavaScript, [ln(x) is `Math.log(x)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/log). We’d need more details about the NTCs to get a proper conversion from resistance to temperature. But the following Decoder can be used in the Payload Format in an application in TTN Console to get you started:

```javascript
/**
 * Convert the array of bytes into an unsigned integer, assuming packed
 * binary-coded decimal (BCD) with an even number of nibbles, MSB, like
 * decoding 0x00127808 to decimal 127808.
 */
function bcdToUint(bytes) {
  return bytes.reduce(function(acc, byte) {
    return 100*acc + 10*(byte >> 4) + (byte & 0x0F);
  }, 0);
}

/**
 * Get a temperature given a resistance, assuming an NTC using the
 * Steinhart equation. See https://www.espruino.com/Thermistors
 */
function resistanceToTemperature(r) {
  // TODO: validate if Steinhart is okay, and adjust coefficients
  var a = 0.0012874; 
  var b = 0.00023573;
  var c = 0.000000095052;
  var w = Math.log(r);
  var temperature = 1 / (a + w*(b + c*w*w)) - 273.15;
  // Unary plus operator to cast string result of toFixed to number
  return +temperature.toFixed(1);
}

function Decoder(bytes, port) {
  var i = 0;
  var r1 = bcdToUint(bytes.slice(i, i+=4));
  var r2 = bcdToUint(bytes.slice(i, i+=4));
  var r3 = bcdToUint(bytes.slice(i, i+=3));
  var v = bcdToUint(bytes.slice(i, i+=2));

  return {
    r1: r1,
    r2: r2,
    r3: r3,
    t1: resistanceToTemperature(r1),
    t2: resistanceToTemperature(r2),
    t3: resistanceToTemperature(r3),
    v: v/1000
  };
}

```

If this is some off-the-shelf device, then please add some details about the brand and type. Future users might need the same answers.

For graphs, see [Visualize (and push) your IOT data](https://www.thethingsnetwork.org/forum/t/visualize-and-push-your-iot-data/1788) and [LABS - Store and visualize data using InfluxDB and Grafana](https://www.thethingsnetwork.org/forum/t/labs-store-and-visualize-data-using-influxdb-and-grafana/5297) and many more posts on the forum and Labs.

---

_[View the full topic](https://www.thethingsnetwork.org/forum/t/how-to-make-a-payload-readable/33364)._
