# Problem with decoding Hex string

**URL:** <https://discourse.nodered.org/t/problem-with-decoding-hex-string/57401>\
**Category:** General\
**Created:** [28 January 2022 12:02 UTC](https://discourse.nodered.org/t/problem-with-decoding-hex-string/57401 "2022-01-28T12:02:25Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Supr3meleader](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/supr3meleader/32/55172_2.png) [@Supr3meleader](https://discourse.nodered.org/u/Supr3meleader)\
**Post date:** [28 January 2022 12:02 UTC](https://discourse.nodered.org/t/problem-with-decoding-hex-string/57401/1 "2022-01-28T12:02:25Z")

</div>

Hello,

I am kinda new to this and can not write code.

My problem is that I can't decode a Hex String that I am getting from my RakWireless gateway.

It is the data (in this case: 0100E00216070E2314000F9392) that I can't convert.

The node is from Elsys and I have tried their Generic Javascript decoder in a Function node with no luck.  
[https://www.elsys.se/en/elsys-payload/](https://www.elsys.se/en/elsys-payload/)

This is how it should look:  
{  
"temperature": 22.4,  
"humidity": 22,  
"vdd": 3619,  
"pressure": 1020.818  
}

I am using Node Red on a Raspberry Pi4 with Home Assistant.

Thank you in advance for looking into my problem.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [28 January 2022 12:17 UTC](https://discourse.nodered.org/t/problem-with-decoding-hex-string/57401/2 "2022-01-28T12:17:15Z")

</div>

Using the Javascript provide by the link here is an example

```auto
[{"id":"5d7d2fce.6e09a8","type":"inject","z":"c791cbc0.84f648","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0100E00216070E2314000F9392","payloadType":"str","x":160,"y":1880,"wires":[["1f84bba1.10f32c"]]},{"id":"1f84bba1.10f32c","type":"function","z":"c791cbc0.84f648","name":"","func":" /*\n ______________  _______\n |____ | | / ____\\ \\ / /____ |\n | |__| | | (___ \\ \\_/ / ( ___\n |__ | | | \\ ___\\ \\ / \\___ \\ \n | | ____| |____ ____) | | |____) |\n | ______|______ | _____/ |_| |_____ / \n \n ELSYS simple payload decoder. \n Use it as it is or remove the bugs :)\n www.elsys.se\n peter@elsys.se\n*/\nconst TYPE_TEMP = 0x01; //temp 2 bytes -3276.8°C -->3276.7°C\nconst TYPE_RH = 0x02; //Humidity 1 byte 0-100%\nconst TYPE_ACC = 0x03; //acceleration 3 bytes X,Y,Z -128 --> 127 +/-63=1G\nconst TYPE_LIGHT = 0x04; //Light 2 bytes 0-->65535 Lux\nconst TYPE_MOTION = 0x05; //No of motion 1 byte 0-255\nconst TYPE_CO2 = 0x06; //Co2 2 bytes 0-65535 ppm\nconst TYPE_VDD = 0x07; //VDD 2byte 0-65535mV\nconst TYPE_ANALOG1 = 0x08; //VDD 2byte 0-65535mV\nconst TYPE_GPS = 0x09; //3bytes lat 3bytes long binary\nconst TYPE_PULSE1 = 0x0A; //2bytes relative pulse count\nconst TYPE_PULSE1_ABS = 0x0B; //4bytes no 0->0xFFFFFFFF\nconst TYPE_EXT_TEMP1 = 0x0C; //2bytes -3276.5C-->3276.5C\nconst TYPE_EXT_DIGITAL = 0x0D; //1bytes value 1 or 0\nconst TYPE_EXT_DISTANCE = 0x0E; //2bytes distance in mm\nconst TYPE_ACC_MOTION = 0x0F; //1byte number of vibration/motion\nconst TYPE_IR_TEMP = 0x10; //2bytes internal temp 2bytes external temp -3276.5C-->3276.5C\nconst TYPE_OCCUPANCY = 0x11; //1byte data\nconst TYPE_WATERLEAK = 0x12; //1byte data 0-255\nconst TYPE_GRIDEYE = 0x13; //65byte temperature data 1byte ref+64byte external temp\nconst TYPE_PRESSURE = 0x14; //4byte pressure data (hPa)\nconst TYPE_SOUND = 0x15; //2byte sound data (peak/avg)\nconst TYPE_PULSE2 = 0x16; //2bytes 0-->0xFFFF\nconst TYPE_PULSE2_ABS = 0x17; //4bytes no 0->0xFFFFFFFF\nconst TYPE_ANALOG2 = 0x18; //2bytes voltage in mV\nconst TYPE_EXT_TEMP2 = 0x19; //2bytes -3276.5C-->3276.5C\nconst TYPE_EXT_DIGITAL2 = 0x1A; // 1bytes value 1 or 0\nconst TYPE_EXT_ANALOG_UV = 0x1B; // 4 bytes signed int (uV)\nconst TYPE_TVOC = 0x1C; // 2 bytes (ppb)\nconst TYPE_DEBUG = 0x3D; // 4bytes debug\n\nfunction bin16dec(bin) {\n var num = bin & 0xFFFF;\n if (0x8000 & num)\n num = -(0x010000 - num);\n return num;\n}\n\nfunction bin8dec(bin) {\n var num = bin & 0xFF;\n if (0x80 & num)\n num = -(0x0100 - num);\n return num;\n}\n\nfunction hexToBytes(hex) {\n for (var bytes = [], c = 0; c < hex.length; c += 2)\n bytes.push(parseInt(hex.substr(c, 2), 16));\n return bytes;\n}\n\nfunction DecodeElsysPayload(data) {\n var obj = new Object();\n for (i = 0; i < data.length; i++) {\n //console.log(data[i]);\n switch (data[i]) {\n case TYPE_TEMP: //Temperature\n var temp = (data[i + 1] << 8) | (data[i + 2]);\n temp = bin16dec(temp);\n obj.temperature = temp / 10;\n i += 2;\n break\n case TYPE_RH: //Humidity\n var rh = (data[i + 1]);\n obj.humidity = rh;\n i += 1;\n break\n case TYPE_ACC: //Acceleration\n obj.x = bin8dec(data[i + 1]);\n obj.y = bin8dec(data[i + 2]);\n obj.z = bin8dec(data[i + 3]);\n i += 3;\n break\n case TYPE_LIGHT: //Light\n obj.light = (data[i + 1] << 8) | (data[i + 2]);\n i += 2;\n break\n case TYPE_MOTION: //Motion sensor(PIR)\n obj.motion = (data[i + 1]);\n i += 1;\n break\n case TYPE_CO2: //CO2\n obj.co2 = (data[i + 1] << 8) | (data[i + 2]);\n i += 2;\n break\n case TYPE_VDD: //Battery level\n obj.vdd = (data[i + 1] << 8) | (data[i + 2]);\n i += 2;\n break\n case TYPE_ANALOG1: //Analog input 1\n obj.analog1 = (data[i + 1] << 8) | (data[i + 2]);\n i += 2;\n break\n case TYPE_GPS: //gps\n i++;\n obj.lat = (data[i + 0] | data[i + 1] << 8 | data[i + 2] << 16 | (data[i + 2] & 0x80 ? 0xFF << 24 : 0)) / 10000;\n obj.long = (data[i + 3] | data[i + 4] << 8 | data[i + 5] << 16 | (data[i + 5] & 0x80 ? 0xFF << 24 : 0)) / 10000;\n i += 5;\n break\n case TYPE_PULSE1: //Pulse input 1\n obj.pulse1 = (data[i + 1] << 8) | (data[i + 2]);\n i += 2;\n break\n case TYPE_PULSE1_ABS: //Pulse input 1 absolute value\n var pulseAbs = (data[i + 1] << 24) | (data[i + 2] << 16) | (data[i + 3] << 8) | (data[i + 4]);\n obj.pulseAbs = pulseAbs;\n i += 4;\n break\n case TYPE_EXT_TEMP1: //External temp\n var temp = (data[i + 1] << 8) | (data[i + 2]);\n temp = bin16dec(temp);\n obj.externalTemperature = temp / 10;\n i += 2;\n break\n case TYPE_EXT_DIGITAL: //Digital input\n obj.digital = (data[i + 1]);\n i += 1;\n break\n case TYPE_EXT_DISTANCE: //Distance sensor input\n obj.distance = (data[i + 1] << 8) | (data[i + 2]);\n i += 2;\n break\n case TYPE_ACC_MOTION: //Acc motion\n obj.accMotion = (data[i + 1]);\n i += 1;\n break\n case TYPE_IR_TEMP: //IR temperature\n var iTemp = (data[i + 1] << 8) | (data[i + 2]);\n iTemp = bin16dec(iTemp);\n var eTemp = (data[i + 3] << 8) | (data[i + 4]);\n eTemp = bin16dec(eTemp);\n obj.irInternalTemperature = iTemp / 10;\n obj.irExternalTemperature = eTemp / 10;\n i += 4;\n break\n case TYPE_OCCUPANCY: //Body occupancy\n obj.occupancy = (data[i + 1]);\n i += 1;\n break\n case TYPE_WATERLEAK: //Water leak\n obj.waterleak = (data[i + 1]);\n i += 1;\n break\n case TYPE_GRIDEYE: //Grideye data\n var ref = data[i+1];\n i++;\n obj.grideye = [];\n for(var j = 0; j < 64; j++) {\n obj.grideye[j] = ref + (data[1+i+j] / 10.0);\n }\n i += 64;\n break\n case TYPE_PRESSURE: //External Pressure\n temp = (data[i + 1] << 24) | (data[i + 2] << 16) | (data[i + 3] << 8) | (data[i + 4]);\n obj.pressure = temp / 1000;\n i += 4;\n break\n case TYPE_SOUND: //Sound\n obj.soundPeak = data[i + 1];\n obj.soundAvg = data[i + 2];\n i += 2;\n break\n case TYPE_PULSE2: //Pulse 2\n obj.pulse2 = (data[i + 1] << 8) | (data[i + 2]);\n i += 2;\n break\n case TYPE_PULSE2_ABS: //Pulse input 2 absolute value\n obj.pulseAbs2 = (data[i + 1] << 24) | (data[i + 2] << 16) | (data[i + 3] << 8) | (data[i + 4]);\n i += 4;\n break\n case TYPE_ANALOG2: //Analog input 2\n obj.analog2 = (data[i + 1] << 8) | (data[i + 2]);\n i += 2;\n break\n case TYPE_EXT_TEMP2: //External temp 2\n temp = (data[i + 1] << 8) | (data[i + 2]);\n temp = bin16dec(temp);\n if(typeof obj.externalTemperature2 === \"number\") {\n obj.externalTemperature2 = [obj.externalTemperature2];\n } \n if(typeof obj.externalTemperature2 === \"object\") {\n obj.externalTemperature2.push(temp / 10);\n } else {\n obj.externalTemperature2 = temp / 10;\n }\n i += 2;\n break\n case TYPE_EXT_DIGITAL2: //Digital input 2\n obj.digital2 = (data[i + 1]);\n i += 1;\n break\n case TYPE_EXT_ANALOG_UV: //Load cell analog uV\n obj.analogUv = (data[i + 1] << 24) | (data[i + 2] << 16) | (data[i + 3] << 8) | (data[i + 4]);\n i += 4;\n break\n case TYPE_TVOC:\n obj.tvoc = (data[i + 1] << 8) | (data[i + 2]);\n i += 2;\n break\n default: //somthing is wrong with data\n i = data.length;\n break\n }\n }\n return obj;\n}\n\n msg.payload = DecodeElsysPayload(hexToBytes(msg.payload));\nreturn msg;\n ","outputs":1,"noerr":0,"initialize":"","finalize":"","x":370,"y":1880,"wires":[["1f3cc108.ff5d5f"]]},{"id":"1f3cc108.ff5d5f","type":"debug","z":"c791cbc0.84f648","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":540,"y":1900,"wires":[]}]

```

Just need to edit the end as it was retrieving data from the document. You need to change that to the msg.payload and return the result.

---

<div class="post-metadata">

**Author:** ![Supr3meleader](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/supr3meleader/32/55172_2.png) [@Supr3meleader](https://discourse.nodered.org/u/Supr3meleader)\
**Post date:** [28 January 2022 13:22 UTC](https://discourse.nodered.org/t/problem-with-decoding-hex-string/57401/3 "2022-01-28T13:22:14Z")

</div>

![ELT 2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/8/9816091cdae7172bf436e84dc3c31d0b3bb35409.png)

I tried that code in function nodes (see func 1, func 2 and func 3) and still the same result.  
Am I supposed to in another way?

As I said, I am new to this, so please explain like I was three years old.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [28 January 2022 13:48 UTC](https://discourse.nodered.org/t/problem-with-decoding-hex-string/57401/4 "2022-01-28T13:48:35Z")

</div>

Please provide your full flow so we can look at it.

---

<div class="post-metadata">

**Author:** ![maru](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/maru/32/99601_2.png) [@maru](https://discourse.nodered.org/u/maru)\
**Post date:** [28 January 2022 21:56 UTC](https://discourse.nodered.org/t/problem-with-decoding-hex-string/57401/5 "2022-01-28T21:56:22Z")

</div>

Hi,

If you are using a MQTT node in your flow, to get the data from your gateway then set the output to a parsed JSON object and I guess that the javascript function will work.

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/c/fcefaf327dc7557412503031b96108790b7e40cc.png)

---

<div class="post-metadata">

**Author:** ![Supr3meleader](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/supr3meleader/32/55172_2.png) [@Supr3meleader](https://discourse.nodered.org/u/Supr3meleader)\
**Post date:** [31 January 2022 07:01 UTC](https://discourse.nodered.org/t/problem-with-decoding-hex-string/57401/6 "2022-01-31T07:01:23Z")

</div>

Hello,

Yes, that should have been mentioned earlier, I use MQTT from the Gateway.

I had missed that feature, but unfortunately it just removed the JSON parser.

 ![ELT 22](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/5/656d22a460a20ed77e9734d9ec52e8c38edbfd45.png)

---

<div class="post-metadata">

**Author:** ![Supr3meleader](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/supr3meleader/32/55172_2.png) [@Supr3meleader](https://discourse.nodered.org/u/Supr3meleader)\
**Post date:** [31 January 2022 07:01 UTC](https://discourse.nodered.org/t/problem-with-decoding-hex-string/57401/7 "2022-01-31T07:01:47Z")

</div>

How do I do that? As I said, I am new to this.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [31 January 2022 07:19 UTC](https://discourse.nodered.org/t/problem-with-decoding-hex-string/57401/8 "2022-01-31T07:19:40Z")

</div>

Here are the relevant pages from the doc's.

[Selecting nodes/flows](https://nodered.org/docs/user-guide/editor/workspace/selection)

[Import/export](https://nodered.org/docs/user-guide/editor/workspace/import-export#exporting-flows)

[Working with messages](https://nodered.org/docs/user-guide/message)

---

<div class="post-metadata">

**Author:** ![Supr3meleader](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/supr3meleader/32/55172_2.png) [@Supr3meleader](https://discourse.nodered.org/u/Supr3meleader)\
**Post date:** [31 January 2022 10:18 UTC](https://discourse.nodered.org/t/problem-with-decoding-hex-string/57401/10 "2022-01-31T10:18:16Z")

</div>

Now when I learned that I can Import, I imported the Javascript that E1cid made for me = Great Succes! 😃

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [31 January 2022 22:17 UTC](https://discourse.nodered.org/t/problem-with-decoding-hex-string/57401/11 "2022-01-31T22:17:01Z")

</div>

I recommend watching this playlist: [Node-RED Essentials](https://www.youtube.com/playlist?list=PLyNBB9VCLmo1hyO-4fIZ08gqFcXBkHy-6). The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [14 February 2022 22:17 UTC](https://discourse.nodered.org/t/problem-with-decoding-hex-string/57401/12 "2022-02-14T22:17:21Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
