⬆️MClimate HT + PIR Lite Uplink decoder

Universal Decoder:

Supports: The Thinks Network, Milesight, DataCake, Chirpstack

// DataCake
function Decoder(bytes, port){
    var decoded = decodeUplink({ bytes: bytes, fPort: port }).data;
    return decoded;
}

// Milesight
function Decode(port, bytes){
    var decoded = decodeUplink({ bytes: bytes, fPort: port }).data;
    return decoded;
}

// The Things Industries / Main
function decodeUplink(input) {
    try {
        var bytes = input.bytes;
        var data = {};

        function handleKeepalive(bytes, data) {
            // Bytes 1-2: Internal temperature sensor data
            // Formula: t[°C] = (T[15:0]-400)/10
            var tempMsb = bytes[1]; // bits 15:8
            var tempLsb = bytes[2]; // bits 7:0
            var rawTemperature = (tempMsb << 8) | tempLsb;
            data.sensorTemperature = Number(((rawTemperature - 400) / 10).toFixed(2));
            
            // Byte 3: Relative Humidity data
            // Formula: RH[%] = (XX*100)/256
            data.relativeHumidity = Number(((bytes[3] * 100) / 256).toFixed(2));
            
            // Bytes 4-5: Device battery voltage data
            // Battery voltage [mV]
            var batteryMsb = bytes[4]; // bits 15:8
            var batteryLsb = bytes[5]; // bits 7:0
            data.batteryVoltage = Number((((batteryMsb << 8) | batteryLsb) / 1000).toFixed(2));
            
            // Byte 6: PIR sensor status (only bit 0 is used, other bits are reserved)
            // 0 - No motion detected
            // 1 - Motion detected
            var pirValue = bytes[6] & 0x01; // Extract only the last bit
            data.pirSensorStatus = pirValue === 1 ? "Motion detected" : "No motion detected";
            data.pirSensorValue = pirValue;
            
            return data;
        }

        function handleResponse(bytes, data){
            var commands = bytes.map(function(byte){
                return ("0" + byte.toString(16)).substr(-2); 
            });
            commands = commands.slice(0,-7); 
            var command_len = 0;
        
            commands.map(function (command, i) {
                switch (command) {
                    case '04':
                        {
                            command_len = 2;
                            var hardwareVersion = commands[i + 1];
                            var softwareVersion = commands[i + 2];
                            data.deviceVersions = { hardware: Number(hardwareVersion), software: Number(softwareVersion) };
                        }
                    break;
                    case '12':
                        {
                            command_len = 1;
                            data.keepAliveTime = parseInt(commands[i + 1], 16);
                        }
                    break;
                    case '19':
                        {
                            command_len = 1;
                            var commandResponse = parseInt(commands[i + 1], 16);
                            var periodInMinutes = commandResponse * 5 / 60;
                            data.joinRetryPeriod = periodInMinutes;
                        }
                    break;
                    case '1b':
                        {
                            command_len = 1;
                            data.uplinkType = parseInt(commands[i + 1], 16);
                        }
                    break;
                    case '1d':
                        {
                            command_len = 2;
                            var wdpC = commands[i + 1] == '00' ? false : parseInt(commands[i + 1], 16);
                            var wdpUc = commands[i + 2] == '00' ? false : parseInt(commands[i + 2], 16);
                            data.watchDogParams= { wdpC: wdpC, wdpUc: wdpUc } ;
                        }
                    break;
                    case '2f':
                        {
                            command_len = 1;
                            data.uplinkSendingOnButtonPress = parseInt(commands[i + 1], 16) ;
                        }
                    break;
                    case '3d':
                        {
                            command_len = 1;
                            data.pirSensorStatus = parseInt(commands[i + 1], 16);
                        }
                        break;
                    case '3f':
                        {
                            command_len = 1;
                            data.pirSensorSensitivity = parseInt(commands[i + 1], 16);
                        }
                        break;
                    case '49':
                        {
                            command_len = 1;
                            data.pirMeasurementPeriod = parseInt(commands[i + 1], 16);
                        }
                        break;
                    case '4b':
                        {
                            command_len = 1;
                            data.pirCheckPeriod = parseInt(commands[i + 1], 16);
                        }
                        break;
                    case '4d':
                        {
                            command_len = 1;
                            data.pirBlindPeriod = parseInt(commands[i + 1], 16);
                        }
                        break;
                    case 'a4': {
                        command_len = 1;
                        data.region = parseInt(commands[i + 1], 16);
                        break;
                    }
                    case 'a6': {
                        command_len = 1;
                        data.crystalOscillatorError = true;
                        break;
                    }
                    default:
                        break;
                }
                commands.splice(i,command_len);
            });
            return data;
        }

        // Route the message based on the command byte
        if (bytes[0] == 1) {
            // This is a keepalive message
            data = handleKeepalive(bytes, data);
        } else {
            // This is a response message
            data = handleResponse(bytes, data);
            // Handle the remaining keepalive data if present
            if (bytes.length >= 7) { // Check if there's enough bytes for a keepalive message
                bytes = bytes.slice(-7); // Extract the last 7 bytes which contain keepalive data
                data = handleKeepalive(bytes, data);
            }
        }

        return { data: data };
    } catch (e) {
        // console.log(e);
        throw new Error('Unhandled data');
    }
}

// Example usage for HT-PIR-Lite keepalive message:
// Following the format from the screenshot example
// Command byte (01), Temperature (02 - bits 15:8, 88 - bits 7:0), RH (80), Battery (0A, 45), PIR (0)
// console.log(decodeUplink({bytes: [0x01, 0x02, 0x88, 0x80, 0x0A, 0x45, 0x00]}));
console.log(decodeUplink({bytes:[1,2,136,128,10,69,0 ]}))

// Expected output:
// Temperature = (0x0288 - 400)/10 = 24.8°C
// RH = (0x80 * 100)/256 = 50%
// Battery voltage = 0x0A45 = 2629mV
// PIR = 0 (No motion detected)

Last updated

Was this helpful?