Is it possible to combine decoders in one?

Hello, I would like to know if it’s possible to have 3 decoder functions using an IF THEN ELSE IF structure, within one code block
.
I have LoRa sensors sending pulse counts, that are being decoded using this existing and working decoder. Within the same TTN Application I also have 2 LPN Modbus brigdes from Comtac, one with 2 registers and one with 10 registers, both need their own custom decoder. Can this decoder be expanded and check for the format of the raw payload to determine which of the 3 decoder functions needs to be applied on said payload?

If that can be done, how would this decoder have to be edited? (leaving space for the 2 custom ones). I absolutely want to avoid using 3 separate applications in TTN.

function Decoder(bytes, port) {
  if (bytes[0] == 12){
    var pulse1 = (bytes[3] << 24) + (bytes[4] << 16) + (bytes[5] << 8) + (bytes[6]);
  }
  
  if (bytes[0] == 13){
    var pulse2 = (bytes[3] << 24) + (bytes[4] << 16) + (bytes[5] << 8) + (bytes[6]);
  }
  
  if (bytes[0] === 0){
    var battery = (bytes[1])
    if (battery > 100){
      battery = 100;
      var empty = 0;
    }
    else if (battery < 68){
      empty = 1;
    }
    else empty = 0;
  }
  
  return{
    pulse1: pulse1,
    pulse2: pulse2,
    batterylow: empty,
    batterystate: battery
  }
}

You can send your data on different port numbers and distinguish on port number in the decoder.

Yes…

I have a sandbox application which decodes all my payloads, base on ports as @EricVdB suggests and also payload size or signature.

The downside is that you end up with huge decoder code and a mistake will impact all your nodes. Aside from that, it works pretty well.

Note that in your above example, if you want to return all fields, you also have to initialize them. Alternatively you can only return the relevant fields for your device (that is: you don’t have to return all fields each time)