Dragino LWL01 - decoding payload

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:

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):

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!

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

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

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