The bytes array is zero-indexed, so the first byte has index 0. So all indexes are 1 too high; 4 for the battery should read 3, and so on.
Temperatures are signed values, which need 4 bytes in JavaScript; see Negative Temperature. So:
// Sign-extend to 32 bits to support negative values, by shifting 24 bits
// (16 too far) to the left, followed by a sign-propagating right shift:
var Temperature = (bytes[4]<<24>>16 | bytes[5]) / 100;
You’re right about decoding the unsigned humidity, but I really doubt the sensor is really that accurate. But it confirms to the documentation.
Finally, one might want to take the first 3 bytes into account as well for other uplink types. Or, lacking any details about that, fail when they don’t match:
if (bytes[0] === 0x01 && bytes[1] === 0x01 && bytes[2] === 0x01) {
// decode as shown in earlier post
return {
...
};
}
// Maybe one could also return null, or throw an error, to make TTN fail
return {
error: "unsupported version, device type or report type"
};
@arjanvanb, For the record. I don’t think i’ve ever experienced a negative temperature, have no idea what that is. Temps this week will be peaking to 40C and anything lower than 10C is considered unbearable.