# Dragino LWL01 - decoding payload

**URL:** https://discourse.nodered.org/t/dragino-lwl01-decoding-payload/47032
**Category:** General
**Created:** [11 June 2021 13:39 UTC](https://discourse.nodered.org/t/dragino-lwl01-decoding-payload/47032 "2021-06-11T13:39:46Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Secundus](https://avatars.discourse-cdn.com/v4/letter/s/df705f/32.png) [@Secundus](https://discourse.nodered.org/u/Secundus)
#### Post date: [11 June 2021 13:39 UTC](https://discourse.nodered.org/t/dragino-lwl01-decoding-payload/47032/1 "2021-06-11T13:39:47Z")

</div>

Hi everybody,

I have a small issue concerning decoding the payload of the Dragino LWL01 sensor.  
The sensor gives off data, which I receive (as should) as a payloadstring. (take 0B 76 02 00 00 17 00 00 00, for example)

According to the LWL01 manual, the payload is translated into:

> _Size (bytes)_  
> 2 (status & bat)  
> 1 (mod Always 0x02)  
> 3 (total waterleak events)  
> 3 (last waterleak duration in mins)
> 
> 9 bytes in total (10 in newer documents)

I use the function node with the following lines to decode in NodeRed:

```auto
let payloadData = msg.payload;

let batteryRaw = ('0x' + payloadData.substr(1,3)) & 0x3FFF;
let batteryDec = batteryRaw/1000;

let statusNow = ('0x'+ payloadData.substr(1,2)) & 0x40;

let Moddata = (payloadData.substr(4,2));
let totalLeaks = (payloadData.substr(6,6));
let lastleakduration = (payloadData.substr(12,6));
    
msg.payload = {
    data : batteryDec,
    Status : statusNow,
    Mod : Moddata,
    leaks : totalLeaks,
    duration : lastleakduration
}

return msg;

```

Now the issue is that when I test the function node with the example payload, I can see every value (battery 2,934, mod 0x20, waterleaks 000017, time 000000) but _not_ the most important value for me: the status (open/close, cq. leak / no leak).  
Regardless of which payload I supply, the status always seems to be 0. (open, no leak).

Dragino has a payload example for TTN, but I have no idea how I should rewrite the status part into my own decoder in NodeRed.  
For TTN they use (shortened):

```auto
function Decoder(bytes, port) {
  // Decode an uplink message from a buffer
  // (array) of bytes to an object of fields.
  var value=(bytes[0]<<8 | bytes[1])&0x3FFF;
  var bat=value/1000;//Battery,units:V
 
  var water_leak_status=bytes[0]&0x40?1:0;
  
  var mod=bytes[2];
  var alarm=bytes[9]&0x01;
  
  var leak_times=bytes[3]<<16 | bytes[4]<<8 | bytes[5];
  var leak_duration=bytes[6]<<16 | bytes[7]<<8 | bytes[8];//units:min
  if(bytes.length==10 && 0x07>bytes[0]< 0x0f)
  return {
      BAT_V:bat,
      MOD:mod,
      WATER_LEAK_STATUS:water_leak_status,
      WATER_LEAK_TIMES:leak_times,
      LAST_WATER_LEAK_DURATION:leak_duration
  }; 
}

```

Two other payload examples:  
4BB80200001700000000 (closed, leak)  
0B760200001700000000 (open, no leak)

If anybody can help me out...would be highly appreciated!

Thank you!

---

<div class="post-metadata">

### Author: ![edje11](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/edje11/32/572_2.png) [@edje11](https://discourse.nodered.org/u/edje11)
#### Post date: [11 June 2021 15:25 UTC](https://discourse.nodered.org/t/dragino-lwl01-decoding-payload/47032/2 "2021-06-11T15:25:45Z")

</div>

> [@Secundus](#):
>
> `let statusNow = ('0x'+ payloadData.substr(1,2)) & 0x40;`

You start at the wrong position, Substring need to start at position 0. Substring end = The position (up to, but not including) where to end the extraction.  
The above line will give a string as output and on strings you can't do byte mask (& 0x40). You need to convert first to number.  
And then as final add the if (?1:0) to get the correct output. (1 or 0)

This works

```auto
let statusNow = Number('0x' + payloadData.substr(0,2)) & 0x40?1:0;

```

---

<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: [10 August 2021 15:26 UTC](https://discourse.nodered.org/t/dragino-lwl01-decoding-payload/47032/3 "2021-08-10T15:26:30Z")

</div>

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