# Reading String of Hex from T-zone RD05 problem

**URL:** <https://discourse.nodered.org/t/reading-string-of-hex-from-t-zone-rd05-problem/14679>\
**Category:** General\
**Created:** [23 August 2019 03:44 UTC](https://discourse.nodered.org/t/reading-string-of-hex-from-t-zone-rd05-problem/14679 "2019-08-23T03:44:28Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bond](https://avatars.discourse-cdn.com/v4/letter/b/9dc877/32.png) [@Bond](https://discourse.nodered.org/u/Bond)\
**Post date:** [23 August 2019 03:44 UTC](https://discourse.nodered.org/t/reading-string-of-hex-from-t-zone-rd05-problem/14679/1 "2019-08-23T03:44:28Z")

</div>

Firstly, I am a newbie from using Node Red,

I got 4 DHT22 sensors connected wirelessly to a TZ-RD05. And the TZ-RD05 is connected to my raspberry pi 3 through a serial port.  
I have the data display is a string like in the picture. Can anyone help me to convert that string to a usable value? (The string is in RSSI value format and contains id, tag, check code, end code and the value of temperature and humidity).

After some quick learning some tutorials, I have an idea of adding some node such as "switch", "split", "set payload"... But have no idea how it works yet.

Thank you for any help.

 ![hello](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/4/4b465d205a072535e44eec7c6085e132686156d9.jpeg)

---

<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:** [23 August 2019 07:03 UTC](https://discourse.nodered.org/t/reading-string-of-hex-from-t-zone-rd05-problem/14679/2 "2019-08-23T07:03:30Z")

</div>

Can you point to a concise definition of RSSI value format?

---

<div class="post-metadata">

**Author:** ![Bond](https://avatars.discourse-cdn.com/v4/letter/b/9dc877/32.png) [@Bond](https://discourse.nodered.org/u/Bond)\
**Post date:** [23 August 2019 07:20 UTC](https://discourse.nodered.org/t/reading-string-of-hex-from-t-zone-rd05-problem/14679/3 "2019-08-23T07:20:21Z")

</div>

This is a data sample. I can read it from the TZ but can not decode.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/3/34838cd373a9fb25a8aaa1834ca699c9c3aa55fc.jpeg)

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [23 August 2019 07:47 UTC](https://discourse.nodered.org/t/reading-string-of-hex-from-t-zone-rd05-problem/14679/4 "2019-08-23T07:47:31Z")

</div>

This is a great use for [https://flows.nodered.org/node/node-red-contrib-binary](https://flows.nodered.org/node/node-red-contrib-binary)  
(but this has it's own learning curve to get the parser spec correct... as it uses a underlying library called packet - [https://github.com/bigeasy/packet](https://github.com/bigeasy/packet) but should be able to template that out quite easily.

---

<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:** [23 August 2019 08:29 UTC](https://discourse.nodered.org/t/reading-string-of-hex-from-t-zone-rd05-problem/14679/5 "2019-08-23T08:29:38Z")

</div>

Are you sure, given the fact that input data is a string?

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [23 August 2019 08:42 UTC](https://discourse.nodered.org/t/reading-string-of-hex-from-t-zone-rd05-problem/14679/6 "2019-08-23T08:42:18Z")

</div>

If the string always follows the same format, couldn't you use `slice()` to extract whatever you need from it?  
I'm assuming that you probably only need the battery, temperature & humidity.  
Try this example where I extract the battery voltage `0E10`;

```auto
[{"id":"c0167800.11f068","type":"inject","z":"c53060.842a0fa","name":"","topic":"","payload":"AB2375GG3655K0E10J87B","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":210,"y":970,"wires":[["f3d7b97a.f156b8"]]},{"id":"f3d7b97a.f156b8","type":"function","z":"c53060.842a0fa","name":"","func":"var batt = msg.payload;\nbatt = batt.slice(11, -4); // Get battery voltage 0E10\nmsg.payload = parseInt(batt);\nreturn msg;","outputs":1,"noerr":0,"x":420,"y":970,"wires":[["bca2271.0c202d8"]]},{"id":"bca2271.0c202d8","type":"debug","z":"c53060.842a0fa","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":630,"y":970,"wires":[]}]

```

Good luck with **node-red-contrib-binary**. I tried. I failed!!

---

<div class="post-metadata">

**Author:** ![Bond](https://avatars.discourse-cdn.com/v4/letter/b/9dc877/32.png) [@Bond](https://discourse.nodered.org/u/Bond)\
**Post date:** [23 August 2019 08:49 UTC](https://discourse.nodered.org/t/reading-string-of-hex-from-t-zone-rd05-problem/14679/7 "2019-08-23T08:49:11Z")

</div>

Yes, the TZ is working with a combination of 4 DHT22 sensors, but the output is a string of hex (shown in the first figure). I just cannot convert that string into decimal with value of temp and humidity ( noticed the space between the Hex number as well)

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [23 August 2019 08:55 UTC](https://discourse.nodered.org/t/reading-string-of-hex-from-t-zone-rd05-problem/14679/8 "2019-08-23T08:55:17Z")

</div>

> [@Bond](#):
>
> noticed the space between the Hex number as well

OK, I was looking at your later post;  
 ![sample](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/3/357a93af1b869f975216111719a6337aa52819c7.jpeg)  
which doesn't have spaces.

But that is even better! take a look at a similar bit of code that I used to decode some sensor data - [node-red-contrib-rf-decode/rf-decode.js at master · Paul-Reed/node-red-contrib-rf-decode · GitHub](https://github.com/Paul-Reed/node-red-contrib-rf-decode/blob/master/rf-decode.js)  
I am using split(), shift(), pop() & slice() to extract what data sections that I needed.

In my case, the input data took the form of `OK 23 22 1 0 0 0 0 237 94 188 4 (-61)`

I am no expert, but got a lot of forum help writing it, especially from @dceejay and learned a lot along the way!

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [23 August 2019 09:05 UTC](https://discourse.nodered.org/t/reading-string-of-hex-from-t-zone-rd05-problem/14679/9 "2019-08-23T09:05:59Z")

</div>

This may get you started...

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/4/46ca6cd64661bc2cd52dc1a7ffff0ed9f00d41f3.png)

```auto
[{"id":"da83610d.0f233","type":"inject","z":"1d34d3c1.70a2dc","name":"simulate TZ data","topic":"","payload":"7E 0D 12 34 56 62 16 01 79 00 0E 10 00 EC 4F 72 03","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":420,"y":580,"wires":[["2d8bf6a5.5e34aa"]]},{"id":"2d8bf6a5.5e34aa","type":"function","z":"1d34d3c1.70a2dc","name":"Parse data","func":"let parsed = {} \nlet p = msg.payload\nparsed.parts = p.split(\" \");\nparsed.data = p;\nparsed.datalength = parseInt(parsed.parts[1],16);\nparsed.id = parsed.parts[2] + parsed.parts[3] + parsed.parts[4] ;\nparsed.tagId = parsed.parts[5] + parsed.parts[6] + parsed.parts[7] + parsed.parts[8] ;\nparsed.tagStatus = parseInt(parsed.parts[9] ,16);\nparsed.batteryVoltage = parseInt(parsed.parts[10] + parsed.parts[11] ,16);\nparsed.temperature = parseInt(parsed.parts[12] + parsed.parts[13] ,16);\nparsed.humidity = parseInt(parsed.parts[14],16);\nparsed.checkCode = parseInt(parsed.parts[15],16);\n\nmsg.payload = parsed \nreturn msg;","outputs":1,"noerr":0,"x":530,"y":620,"wires":[["1a1b15d4.375f1a"]]},{"id":"1a1b15d4.375f1a","type":"debug","z":"1d34d3c1.70a2dc","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":650,"y":660,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [23 August 2019 09:25 UTC](https://discourse.nodered.org/t/reading-string-of-hex-from-t-zone-rd05-problem/14679/10 "2019-08-23T09:25:25Z")

</div>

@Steve-Mcl This will no doubt come up again, and would be good to have your example in the flow library.

---

<div class="post-metadata">

**Author:** ![Bond](https://avatars.discourse-cdn.com/v4/letter/b/9dc877/32.png) [@Bond](https://discourse.nodered.org/u/Bond)\
**Post date:** [23 August 2019 09:35 UTC](https://discourse.nodered.org/t/reading-string-of-hex-from-t-zone-rd05-problem/14679/11 "2019-08-23T09:35:15Z")

</div>

Sorry the data sample is from the data-sheet of the TZ-RD05, not what i have. But will try, thank you.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [23 August 2019 09:52 UTC](https://discourse.nodered.org/t/reading-string-of-hex-from-t-zone-rd05-problem/14679/12 "2019-08-23T09:52:04Z")

</div>

Its not really hardened and doenst use the checksum to verify data (I assume checksum is some BCC or XOR)

And I am uncertain of this device and this string format - is it ALWAYS that way for that particular sensor input?

But if you believe it helps other people, feel free to take it, improve it & add it as a flow library Paul. I'm fine with that (no need to mention me in the post tho haha).

@Bond did this work for you? Looking at your data output it looks like it should just work.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [23 August 2019 11:57 UTC](https://discourse.nodered.org/t/reading-string-of-hex-from-t-zone-rd05-problem/14679/13 "2019-08-23T11:57:03Z")

</div>

This example using the contrib binary node  
(assumes that the data is actually arriving as a buffer rather than a string but easy to convert if required.

```auto
[{"id":"9c328481.dfcfa8","type":"inject","z":"a68cca8a.e5ba98","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":140,"y":1100,"wires":[["a05ae5fb.4cbda8"]]},{"id":"a05ae5fb.4cbda8","type":"function","z":"a68cca8a.e5ba98","name":"","func":"msg.payload = new Buffer('7e0d12345662160179000e1000ec4f7203', 'hex');\nreturn msg;","outputs":1,"noerr":0,"x":310,"y":1100,"wires":[["ef82130.ae0e5f"]]},{"id":"ef82130.ae0e5f","type":"binary","z":"a68cca8a.e5ba98","name":"","pattern":"x8, b8 => len, b8[3] => id, b8[4] => tag, b8 => status, b16 => volt, b16 => temp, b8 => hum, b8 => crc, x8","x":470,"y":1100,"wires":[["45e7eef1.5d59b"]]},{"id":"45e7eef1.5d59b","type":"debug","z":"a68cca8a.e5ba98","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":650,"y":1100,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [23 August 2019 12:44 UTC](https://discourse.nodered.org/t/reading-string-of-hex-from-t-zone-rd05-problem/14679/14 "2019-08-23T12:44:38Z")

</div>

Easy when you know how!!  
I spent hours reading about the underlying package, and never got anywhere near reaching a solution.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [23 August 2019 13:15 UTC](https://discourse.nodered.org/t/reading-string-of-hex-from-t-zone-rd05-problem/14679/15 "2019-08-23T13:15:31Z")

</div>

Yeah - I'll try and get hold of the author and see what we can do.

---

<div class="post-metadata">

**Author:** ![Bond](https://avatars.discourse-cdn.com/v4/letter/b/9dc877/32.png) [@Bond](https://discourse.nodered.org/u/Bond)\
**Post date:** [25 August 2019 16:50 UTC](https://discourse.nodered.org/t/reading-string-of-hex-from-t-zone-rd05-problem/14679/16 "2019-08-25T16:50:37Z")

</div>

Sorry I have been away. I tried `slice()`, for some reason the output is only one character regardless of what index I put in the `slice()` function.

> var test = msg.payload;  
> test = test.slice(0,1);  
> msg.payload = parseInt(test);  
> return msg;

Then I try the code from @Steve-Mcl, and I have problem in the line shown in the pic.  
I also try to get rid of the space by using `trim()` but it only works for the first and last space, not the spaces in between. Therefore, could not use example from @dceejay.  
I still don't know why my output got 47 indexes (shown in the first pic).

 ![IMG_2354](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/5/595f982e2021ccca0ec8ef158f849094288a1d25.jpeg)

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [25 August 2019 18:01 UTC](https://discourse.nodered.org/t/reading-string-of-hex-from-t-zone-rd05-problem/14679/17 "2019-08-25T18:01:23Z")

</div>

How did you import my flow? You copied the function code from this thread and pasted it directly into a function node in I suspect?.

Youre supposed to copy the whole flow code from the forum, go to node-red editor, press CTRL+I (to import), paste in the flow code, then import.

Line 3 should be `parsed.parts = p.split(" ")`

Try importing my sample and testing that, then hook it up to your data.

---

<div class="post-metadata">

**Author:** ![Bond](https://avatars.discourse-cdn.com/v4/letter/b/9dc877/32.png) [@Bond](https://discourse.nodered.org/u/Bond)\
**Post date:** [26 August 2019 12:46 UTC](https://discourse.nodered.org/t/reading-string-of-hex-from-t-zone-rd05-problem/14679/18 "2019-08-26T12:46:30Z")

</div>

Got it working now, thank you so much.  
Now i want to split the temperature value to binary as well, because the first two bits indicate sign and stage of temperature, the other bits are real value of temperature.  
Thanks again for your help, i'm still learning 🙂

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [26 August 2019 16:44 UTC](https://discourse.nodered.org/t/reading-string-of-hex-from-t-zone-rd05-problem/14679/19 "2019-08-26T16:44:27Z")

</div>

well the binary node may be able to do that as well as it can handle bits as well - but we can't see the spec of what you're after - so it's up to you at this point.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [26 August 2019 23:42 UTC](https://discourse.nodered.org/t/reading-string-of-hex-from-t-zone-rd05-problem/14679/20 "2019-08-26T23:42:34Z")

</div>

Search Web for...  
JavaScript get bit from number  
And  
JavaScript mask number

These will help you understand how to get bit 15 and bit 14 then use a mask of 0x3fff to delete bits 15&14 to leave you with the correct temperature.

Add something like this inside the function node you already have...

```auto
function getbit(value, bit){
  // You need to figure this out
}
var sign = getbit(temperature, 15);
var stat = getbit(temperature, 14);
temperature = temperature & 0x3fff;
if(sign)
  temperature = -temperature;
msg.payload.temperature = temperature;

```

\*\*Note, this code is untested and you will need to search for how to get bits \*\*
