Skip to content

Plugin file

Plugin configuration file
{
  "name": "milesight_iot_wt201v2",
  "version": "1.0.0",
  "description": "Milesight WT201 is a LoRaWAN® smart thermostat offering remote HVAC control for enhanced energy efficiency. It enables sustainable heating and cooling management, aligning with green energy strategies for eco-friendly living spaces.",
  "author": "Thinger.io",
  "license": "MIT",
  "repository": {
    "type": "git",
    "url": "https://github.com/thinger-io/plugins.git",
    "directory": "milesight-iot-wt201v2"
  },
  "metadata": {
    "name": "Milesight-Iot WT201V2",
    "description": "Milesight WT201 is a LoRaWAN® smart thermostat offering remote HVAC control for enhanced energy efficiency. It enables sustainable heating and cooling management, aligning with green energy strategies for eco-friendly living spaces.",
    "image": "assets/wt201v2.png",
    "category": "devices",
    "vendor": "milesight-iot"
  },
  "resources": {
    "products": [
      {
        "description": "Milesight WT201 is a LoRaWAN® smart thermostat offering remote HVAC control for enhanced energy efficiency. It enables sustainable heating and cooling management, aligning with green energy strategies for eco-friendly living spaces.",
        "enabled": true,
        "name": "Milesight-Iot WT201V2",
        "product": "milesight_iot_wt201v2",
        "profile": {
          "api": {
            "downlink": {
              "enabled": true,
              "handle_connectivity": false,
              "request": {
                "data": {
                  "path": "/downlink",
                  "payload": "{\r\n    \"data\"    : \"{{payload.data=\"\"}}\",\r\n    \"port\"    :  {{payload.port=2}},\r\n    \"priority\":  {{payload.priority=3}},\r\n    \"confirmed\" :  {{payload.confirmed=false}},\r\n    \"uplink\"  :  {{property.uplink}} \r\n}",
                  "payload_function": "",
                  "payload_type": "",
                  "plugin": "{{property.uplink.source}}",
                  "target": "plugin_endpoint"
                }
              }
            },
            "uplink": {
              "enabled": true,
              "handle_connectivity": true,
              "request": {
                "data": {
                  "payload": "{{payload}}",
                  "payload_function": "",
                  "payload_type": "source_payload",
                  "resource_stream": "uplink",
                  "target": "resource_stream"
                }
              }
            }
          },
          "autoprovisions": {
            "device_autoprovisioning": {
              "config": {
                "mode": "pattern",
                "pattern": "wt201_.*"
              },
              "enabled": true
            }
          },
          "buckets": {
            "milesight_wt201v2_data": {
              "backend": "mongodb",
              "data": {
                "payload": "{{payload}}",
                "payload_function": "",
                "payload_type": "source_payload",
                "resource_stream": "uplink_decoded",
                "source": "resource_stream"
              },
              "enabled": true,
              "retention": {
                "period": 3,
                "unit": "months"
              },
              "tags": []
            }
          },
          "code": {
            "code": "function decodeThingerUplink(thingerData) {\n    // 0. If data has already been decoded, we will return it\n    if (thingerData.decodedPayload) return thingerData.decodedPayload;\n    \n    // 1. Extract and Validate Input\n    // We need 'payload' (hex string) and 'fPort' (integer)\n    const hexPayload = thingerData.payload || \"\";\n    const port = thingerData.fPort || 1;\n\n    // 2. Convert Hex String to Byte Array\n    const bytes = [];\n    for (let i = 0; i < hexPayload.length; i += 2) {\n        bytes.push(parseInt(hexPayload.substr(i, 2), 16));\n    }\n\n    // 3. Dynamic Function Detection and Execution\n    \n    // CASE A: (The Things Stack v3)\n    if (typeof decodeUplink === 'function') {\n        try {\n            const input = {\n                bytes: bytes,\n                fPort: port\n            };\n            var result = decodeUplink(input);\n            \n            if (result.data) return result.data;\n\n            return result; \n        } catch (e) {\n            console.error(\"Error inside decodeUplink:\", e);\n            throw e;\n        }\n    }\n\n    // CASE B: Legacy TTN (v2)\n    else if (typeof Decoder === 'function') {\n        try {\n            return Decoder(bytes, port);\n        } catch (e) {\n            console.error(\"Error inside Decoder:\", e);\n            throw e;\n        }\n    }\n\n    // CASE C: No decoder found\n    else {\n        throw new Error(\"No compatible TTN decoder function (decodeUplink or Decoder) found in scope.\");\n    }\n}\n\n\n// TTN decoder\n/**\n * Payload Decoder\n *\n * Copyright 2025 Milesight IoT\n *\n * @product WT201 v2\n */\nvar RAW_VALUE = 0x00;\n\n/* eslint no-redeclare: \"off\" */\n/* eslint-disable */\n// Chirpstack v4\nfunction decodeUplink(input) {\n    var decoded = milesightDeviceDecode(input.bytes);\n    return { data: decoded };\n}\n\n// Chirpstack v3\nfunction Decode(fPort, bytes) {\n    return milesightDeviceDecode(bytes);\n}\n\n// The Things Network\nfunction Decoder(bytes, port) {\n    return milesightDeviceDecode(bytes);\n}\n/* eslint-enable */\n\nfunction milesightDeviceDecode(bytes) {\n    var decoded = {};\n\n    for (var i = 0; i < bytes.length; ) {\n        var channel_id = bytes[i++];\n        var channel_type = bytes[i++];\n\n        // IPSO VERSION\n        if (channel_id === 0xff && channel_type === 0x01) {\n            decoded.ipso_version = readProtocolVersion(bytes[i]);\n            i += 1;\n        }\n        // HARDWARE VERSION\n        else if (channel_id === 0xff && channel_type === 0x09) {\n            decoded.hardware_version = readHardwareVersion(bytes.slice(i, i + 2));\n            i += 2;\n        }\n        // FIRMWARE VERSION\n        else if (channel_id === 0xff && channel_type === 0x0a) {\n            decoded.firmware_version = readFirmwareVersion(bytes.slice(i, i + 2));\n            i += 2;\n        }\n        // TSL VERSION\n        else if (channel_id === 0xff && channel_type === 0xff) {\n            decoded.tsl_version = readTslVersion(bytes.slice(i, i + 2));\n            i += 2;\n        }\n        // SERIAL NUMBER\n        else if (channel_id === 0xff && channel_type === 0x16) {\n            decoded.sn = readSerialNumber(bytes.slice(i, i + 8));\n            i += 8;\n        }\n        // LORAWAN CLASS TYPE\n        else if (channel_id === 0xff && channel_type === 0x0f) {\n            decoded.lorawan_class = readLoRaWANClass(bytes[i]);\n            i += 1;\n        }\n        // RESET EVENT\n        else if (channel_id === 0xff && channel_type === 0xfe) {\n            decoded.reset_event = readResetEvent(1);\n            i += 1;\n        }\n        // DEVICE STATUS\n        else if (channel_id === 0xff && channel_type === 0x0b) {\n            decoded.device_status = readDeviceStatus(1);\n            i += 1;\n        }\n        // TEMPERATURE\n        else if (channel_id === 0x03 && channel_type === 0x67) {\n            decoded.temperature = readInt16LE(bytes.slice(i, i + 2)) / 10;\n            i += 2;\n        }\n        // TARGET TEMPERATURE\n        else if (channel_id === 0x04 && channel_type === 0x67) {\n            decoded.target_temperature = readInt16LE(bytes.slice(i, i + 2)) / 10;\n            i += 2;\n        }\n        // TARGET TEMPERATURE 2\n        else if (channel_id === 0x0b && channel_type === 0x67) {\n            decoded.target_temperature_2 = readInt16LE(bytes.slice(i, i + 2)) / 10;\n            i += 2;\n        }\n        // TEMPERATURE CONTROL\n        else if (channel_id === 0x05 && channel_type === 0xe7) {\n            var value = bytes[i];\n            // value = temperature_control_mode(0..1) + temperature_control_status(4..7)\n            decoded.temperature_control_mode = readTemperatureControlMode((value >>> 0) & 0x03);\n            decoded.temperature_control_status = readTemperatureControlStatus((value >>> 4) & 0x0f);\n            i += 1;\n        }\n        // FAN CONTROL\n        else if (channel_id === 0x06 && channel_type === 0xe8) {\n            var fan_data = bytes[i];\n            // value = fan_mode(0..1) + fan_status(2..3)\n            decoded.fan_mode = readFanMode((fan_data >>> 0) & 0x03);\n            decoded.fan_status = readFanStatus((fan_data >>> 2) & 0x03);\n            i += 1;\n        }\n        // PLAN EVENT\n        else if (channel_id === 0x07 && channel_type === 0xbc) {\n            var plan_type_data = bytes[i];\n            // value = plan_type(0..3)\n            decoded.plan_type = readExecutePlanType((plan_type_data >>> 0) & 0x0f);\n            i += 1;\n        }\n        // SYSTEM STATUS\n        else if (channel_id === 0x08 && channel_type === 0x8e) {\n            decoded.system_status = readOnOffStatus(bytes[i]);\n            i += 1;\n        }\n        // HUMIDITY\n        else if (channel_id === 0x09 && channel_type === 0x68) {\n            decoded.humidity = readUInt8(bytes[i]) / 2;\n            i += 1;\n        }\n        // RELAY STATUS\n        else if (channel_id === 0x0a && channel_type === 0x6e) {\n            decoded.wires_relay = readWiresRelay(bytes[i]);\n            i += 1;\n        }\n        // TEMPERATURE MODE SUPPORT\n        else if (channel_id === 0xff && channel_type === 0xcb) {\n            decoded.temperature_control_support_mode = readTemperatureControlSupportMode(bytes[i]);\n            decoded.temperature_control_support_status = readTemperatureControlSupportStatus(bytes[i + 1], bytes[i + 2]);\n            i += 3;\n        }\n        // TEMPERATURE ALARM\n        else if (channel_id === 0x83 && channel_type === 0x67) {\n            decoded.temperature = readInt16LE(bytes.slice(i, i + 2)) / 10;\n            decoded.temperature_alarm = readTemperatureAlarm(bytes[i + 2]);\n            i += 3;\n        }\n        // TEMPERATURE EXCEPTION\n        else if (channel_id === 0xb3 && channel_type === 0x67) {\n            decoded.temperature_sensor_status = readSensorStatus(bytes[i]);\n            i += 1;\n        }\n        // HUMIDITY EXCEPTION\n        else if (channel_id === 0xb9 && channel_type === 0x68) {\n            decoded.humidity_sensor_status = readSensorStatus(bytes[i]);\n            i += 1;\n        }\n        // TEMPERATURE OUT OF RANGE ALARM\n        else if (channel_id === 0xf9 && channel_type === 0x40) {\n            var target_temperature_range_alarm = {};\n            target_temperature_range_alarm.temperature_control_mode = readTemperatureControlMode(bytes[i]);\n            target_temperature_range_alarm.target_temperature = readInt16LE(bytes.slice(i + 1, i + 3)) / 10;\n            target_temperature_range_alarm.min = readInt16LE(bytes.slice(i + 3, i + 5)) / 10;\n            target_temperature_range_alarm.max = readInt16LE(bytes.slice(i + 5, i + 7)) / 10;\n            i += 7;\n\n            decoded.target_temperature_range_alarm = decoded.target_temperature_range_alarm || [];\n            decoded.target_temperature_range_alarm.push(target_temperature_range_alarm);\n        }\n        // HISTORICAL DATA\n        else if (channel_id === 0x20 && channel_type === 0xce) {\n            var timestamp = readUInt32LE(bytes.slice(i, i + 4));\n            var value1 = readUInt16LE(bytes.slice(i + 4, i + 6));\n\n            var data = {};\n            data.timestamp = timestamp;\n            // value1 = system_status(0) + fan_mode(1..2) + fan_status(3..4) + temperature_control_mode(5..6) + temperature_control_status(7..10)\n            data.system_status = readOnOffStatus(value1 & 0x01);\n            data.fan_mode = readFanMode((value1 >>> 1) & 0x03);\n            data.fan_status = readFanStatus((value1 >>> 3) & 0x03);\n            data.temperature_control_mode = readTemperatureControlMode((value1 >>> 5) & 0x03);\n            data.temperature_control_status = readTemperatureControlStatus((value1 >>> 7) & 0x0f);\n\n            data.target_temperature = readInt16LE(bytes.slice(i + 6, i + 8)) / 10;\n            var temperature_target_value = readUInt16LE(bytes.slice(i + 8, i + 10));\n            if (temperature_target_value !== 0xffff) {\n                data.target_temperature_2 = readInt16LE(bytes.slice(i + 8, i + 10)) / 10;\n            }\n            data.temperature = readInt16LE(bytes.slice(i + 10, i + 12)) / 10;\n            data.humidity = readUInt8(bytes[i + 12]) / 2;\n            i += 13;\n\n            decoded.history = decoded.history || [];\n            decoded.history.push(data);\n        }\n        // DOWNLINK RESPONSE\n        else if (channel_id === 0xfe || channel_id === 0xff) {\n            var result = handle_downlink_response(channel_type, bytes, i);\n            decoded = Object.assign(decoded, result.data);\n            i = result.offset;\n        } else if (channel_id === 0xf8 || channel_id === 0xf9) {\n            var resultExt = handle_downlink_response_ext(channel_id, channel_type, bytes, i);\n            decoded = Object.assign(decoded, resultExt.data);\n            i = resultExt.offset;\n        } else {\n            break;\n        }\n    }\n\n    return decoded;\n}\n\nfunction handle_downlink_response(channel_type, bytes, offset) {\n    var decoded = {};\n\n    switch (channel_type) {\n        case 0x02:\n            decoded.collection_interval = readUInt16LE(bytes.slice(offset, offset + 2));\n            offset += 2;\n            break;\n        case 0x03:\n            decoded.outside_temperature = readInt16LE(bytes.slice(offset, offset + 2)) / 10;\n            offset += 2;\n            break;\n        case 0x06: // temperature_alarm_config\n            var ctl = readUInt8(bytes[offset]);\n            var condition = ctl & 0x07;\n            var alarm_type = (ctl >>> 3) & 0x07;\n\n            var condition_map = { 0: \"disable\", 1: \"below\", 2: \"above\", 3: \"between\", 4: \"outside\" };\n            var alarm_type_map = { 0: \"temperature threshold\", 1: \"continuous low temperature\", 2: \"continuous high temperature\" };\n            var data = { condition: getValue(condition_map, condition), alarm_type: getValue(alarm_type_map, alarm_type) };\n\n            if (condition === 1 || condition === 3 || condition === 4) {\n                data.threshold_min = readInt16LE(bytes.slice(offset + 1, offset + 3)) / 10;\n            }\n            if (condition === 2 || condition === 3 || condition === 4) {\n                data.threshold_max = readInt16LE(bytes.slice(offset + 3, offset + 5)) / 10;\n            }\n            data.lock_time = readInt16LE(bytes.slice(offset + 5, offset + 7));\n            data.continue_time = readInt16LE(bytes.slice(offset + 7, offset + 9));\n            offset += 9;\n\n            decoded.temperature_alarm_config = data;\n            break;\n        case 0x25:\n            var masked = readUInt8(bytes[offset]);\n            var status = readUInt8(bytes[offset + 1]);\n            var button_mask_bit_offset = { power_button: 0, up_button: 1, down_button: 2, fan_button: 3, mode_button: 4, reset_button: 5 };\n\n            decoded.child_lock_config = decoded.child_lock_config || {};\n            for (var button in button_mask_bit_offset) {\n                if ((masked >> button_mask_bit_offset[button]) & 0x01) {\n                    decoded.child_lock_config[button] = readEnableStatus((status >> button_mask_bit_offset[button]) & 0x01);\n                }\n            }\n            offset += 2;\n            break;\n        case 0x28:\n            var report_status_map = { 0: \"plan\", 1: \"periodic\", 2: \"target_temperature_range\" };\n            decoded.report_status = getValue(report_status_map, readUInt8(bytes[offset]));\n            offset += 1;\n            break;\n        case 0x82:\n            var value = readUInt8(bytes[offset]);\n            var mask = value >>> 4;\n            var enabled = value & 0x0f;\n\n            var group_mask_bit_offset = { group1_enable: 0, group2_enable: 1, group3_enable: 2, group4_enable: 3 };\n            decoded.multicast_group_config = {};\n            for (var group in group_mask_bit_offset) {\n                if ((mask >> group_mask_bit_offset[group]) & 0x01) {\n                    decoded.multicast_group_config[group] = readEnableStatus((enabled >> group_mask_bit_offset[group]) & 0x01);\n                }\n            }\n            offset += 1;\n            break;\n        case 0x83:\n            var d2d_slave_config = readD2DSlaveConfig(bytes.slice(offset, offset + 5));\n            offset += 5;\n            decoded.d2d_slave_config = decoded.d2d_slave_config || [];\n            decoded.d2d_slave_config.push(d2d_slave_config);\n            break;\n        case 0x96:\n            var d2d_master_config = readD2DMasterConfig(bytes.slice(offset, offset + 8));\n            offset += 8;\n            decoded.d2d_master_config = decoded.d2d_master_config || [];\n            decoded.d2d_master_config.push(d2d_master_config);\n            break;\n        case 0x4a:\n            decoded.sync_time = readYesNoStatus(readUInt8(bytes[offset]));\n            offset += 1;\n            break;\n        case 0x8e:\n            // ignore the first byte\n            decoded.report_interval = readUInt16LE(bytes.slice(offset + 1, offset + 3));\n            offset += 3;\n            break;\n        case 0xab:\n            decoded.temperature_calibration_settings = {};\n            decoded.temperature_calibration_settings.enable = readEnableStatus(readUInt8(bytes[offset]));\n            decoded.temperature_calibration_settings.calibration_value = readInt16LE(bytes.slice(offset + 1, offset + 3)) / 10;\n            offset += 3;\n            break;\n        case 0xb0:\n            decoded.freeze_protection_config = decoded.freeze_protection_config || {};\n            decoded.freeze_protection_config.enable = readEnableStatus(readUInt8(bytes[offset]));\n            decoded.freeze_protection_config.temperature = readInt16LE(bytes.slice(offset + 1, offset + 3)) / 10;\n            offset += 3;\n            break;\n        case 0xb5:\n            decoded.ob_mode = readObMode(readUInt8(bytes[offset]));\n            offset += 1;\n            break;\n        case 0xb6:\n            decoded.fan_mode = readFanMode(readUInt8(bytes[offset]));\n            offset += 1;\n            break;\n        case 0xb7:\n            decoded.temperature_control_mode = readTemperatureControlMode(readUInt8(bytes[offset]));\n            var t = readUInt8(bytes[offset + 1]);\n            decoded.target_temperature = t & 0x7f;\n            decoded.temperature_unit = readTemperatureUnit((t >>> 7) & 0x01);\n            offset += 2;\n            break;\n        case 0xb8:\n            decoded.temperature_tolerance = {};\n            decoded.temperature_tolerance.target_temperature_tolerance = readUInt8(bytes[offset]) / 10;\n            decoded.temperature_tolerance.auto_temperature_tolerance = readUInt8(bytes[offset + 1]) / 10;\n            offset += 2;\n            break;\n        case 0xb9:\n            decoded.temperature_level_up_condition = {};\n            decoded.temperature_level_up_condition.type = readTemperatureLevelUpCondition(readUInt8(bytes[offset]));\n            decoded.temperature_level_up_condition.time = readUInt8(bytes[offset + 1]);\n            decoded.temperature_level_up_condition.temperature_control_tolerance = readInt16LE(bytes.slice(offset + 2, offset + 4)) / 10;\n            offset += 4;\n            break;\n        case 0xba:\n            var enable_value = bytes[offset];\n            decoded.dst_config = {};\n            decoded.dst_config.enable = readEnableStatus(enable_value);\n            if (enable_value) {\n                decoded.dst_config.offset = readUInt8(bytes[offset + 1]);\n                decoded.dst_config.start_month = readUInt8(bytes[offset + 2]);\n                var start_day = readUInt8(bytes[offset + 3]);\n                decoded.dst_config.start_week_num = (start_day >>> 4) & 0x0f;\n                decoded.dst_config.start_week_day = start_day & 0x0f;\n                decoded.dst_config.start_time = readUInt16LE(bytes.slice(offset + 4, offset + 6));\n                decoded.dst_config.end_month = readUInt8(bytes[offset + 6]);\n                var end_day = readUInt8(bytes[offset + 7]);\n                decoded.dst_config.end_week_num = (end_day >>> 4) & 0x0f;\n                decoded.dst_config.end_week_day = end_day & 0x0f;\n                decoded.dst_config.end_time = readUInt16LE(bytes.slice(offset + 8, offset + 10));\n            }\n            offset += 10;\n            break;\n        case 0xbd:\n            decoded.time_zone = readTimeZone(readInt16LE(bytes.slice(offset, offset + 2)));\n            offset += 2;\n            break;\n        case 0xc1:\n            var action_type_map = { 0: \"power\", 1: \"plan\" };\n            decoded.card_config = {};\n            decoded.card_config.enable = readEnableStatus(bytes[offset]);\n            var action_type_value = readUInt8(bytes[offset + 1]);\n            decoded.card_config.action_type = getValue(action_type_map, action_type_value);\n            if (action_type_value === 1) {\n                var action = readUInt8(bytes[offset + 2]);\n                decoded.card_config.in_plan_type = readPlanType((action >>> 4) & 0x0f);\n                decoded.card_config.out_plan_type = readPlanType(action & 0x0f);\n            }\n            decoded.card_config.invert = readYesNoStatus(bytes[offset + 3]);\n            offset += 4;\n            break;\n        case 0xc2:\n            decoded.plan_type = readPlanType(readUInt8(bytes[offset]));\n            offset += 1;\n            break;\n        case 0xc4:\n            decoded.temperature_source_config = {};\n            decoded.temperature_source_config.source = readTemperatureSource(readUInt8(bytes[offset]));\n            decoded.temperature_source_config.timeout = readUInt8(bytes[offset + 1]);\n            offset += 2;\n            break;\n        case 0xc5:\n            decoded.temperature_control_enable = readEnableStatus(bytes[offset]);\n            offset += 1;\n            break;\n        case 0xc7:\n            var d2d_enable_data = readUInt8(bytes[offset]);\n            offset += 1;\n            var d2d_enable_mask = d2d_enable_data >>> 4;\n            var d2d_enable_status = d2d_enable_data & 0x0f;\n            if ((d2d_enable_mask >> 0) & 0x01) {\n                decoded.d2d_master_enable = readEnableStatus(d2d_enable_status & 0x01);\n            }\n            if ((d2d_enable_mask >> 1) & 0x01) {\n                decoded.d2d_slave_enable = readEnableStatus((d2d_enable_status >> 1) & 0x01);\n            }\n            break;\n        case 0xc9:\n            var schedule = readPlanSchedule(bytes.slice(offset, offset + 6));\n            offset += 6;\n\n            decoded.plan_schedule = decoded.plan_schedule || [];\n            decoded.plan_schedule.push(schedule);\n            break;\n        case 0xca:\n            decoded.wires = readWires(bytes[offset], bytes[offset + 1], bytes[offset + 2]);\n            decoded.ob_mode = readObMode((bytes[offset + 2] >>> 2) & 0x03);\n            offset += 3;\n            break;\n        case 0xeb:\n            decoded.temperature_unit = readTemperatureUnit(bytes[offset]);\n            offset += 1;\n            break;\n        case 0xf6:\n            decoded.control_permission = readControlPermission(readUInt8(bytes[offset]));\n            offset += 1;\n            break;\n        case 0xf7:\n            var wire_relay_bit_offset = { y1: 0, y2_gl: 1, w1: 2, w2_aux: 3, e: 4, g: 5, ob: 6 };\n            var wire_relay_mask = readUInt16LE(bytes.slice(offset, offset + 2));\n            var wire_relay_status = readUInt16LE(bytes.slice(offset + 2, offset + 4));\n            offset += 4;\n\n            decoded.wires_relay_config = {};\n            for (var key in wire_relay_bit_offset) {\n                if ((wire_relay_mask >>> wire_relay_bit_offset[key]) & 0x01) {\n                    decoded.wires_relay_config[key] = readOnOffStatus((wire_relay_status >>> wire_relay_bit_offset[key]) & 0x01);\n                }\n            }\n            break;\n        case 0xf8:\n            decoded.offline_control_mode = readOfflineControlMode(readUInt8(bytes[offset]));\n            offset += 1;\n            break;\n        case 0xf9:\n            decoded.humidity_calibration_settings = {};\n            decoded.humidity_calibration_settings.enable = readEnableStatus(readUInt8(bytes[offset]));\n            decoded.humidity_calibration_settings.calibration_value = readInt16LE(bytes.slice(offset + 1, offset + 3)) / 10;\n            offset += 3;\n            break;\n        case 0xfa:\n            decoded.temperature_control_mode = readTemperatureControlMode(readUInt8(bytes[offset]));\n            decoded.target_temperature = readInt16LE(bytes.slice(offset + 1, offset + 3)) / 10;\n            offset += 3;\n            break;\n        case 0xfb:\n            decoded.current_temperature_control_mode = readTemperatureControlMode(readUInt8(bytes[offset]));\n            offset += 1;\n            break;\n        default:\n            throw new Error(\"unknown downlink response\");\n    }\n\n    return { data: decoded, offset: offset };\n}\n\nfunction handle_downlink_response_ext(code, channel_type, bytes, offset) {\n    var decoded = {};\n\n    switch (channel_type) {\n        case 0x06:\n            decoded.fan_execute_time = readUInt8(bytes[offset]);\n            offset += 1;\n            break;\n        case 0x07:\n            decoded.fan_dehumidify = {};\n            var enable_value = readUInt8(bytes[offset]);\n            decoded.fan_dehumidify.enable = readEnableStatus(enable_value);\n            if (enable_value) {\n                decoded.fan_dehumidify.execute_time = readUInt8(bytes[offset + 1]);\n            }\n            offset += 2;\n            break;\n        case 0x08:\n            var screen_display_mode_map = { 0: \"on\", 1: \"without plan show\", 2: \"disable all\" };\n            decoded.screen_display_mode = getValue(screen_display_mode_map, readUInt8(bytes[offset]));\n            offset += 1;\n            break;\n        case 0x09:\n            decoded.humidity_range = {};\n            decoded.humidity_range.min = readUInt8(bytes[offset]);\n            decoded.humidity_range.max = readUInt8(bytes[offset + 1]);\n            offset += 2;\n            break;\n        case 0x0a:\n            decoded.temperature_dehumidify = {};\n            decoded.temperature_dehumidify.enable = readEnableStatus(readUInt8(bytes[offset]));\n            var temperature_tolerance_value = readUInt8(bytes[offset + 1]);\n            if (temperature_tolerance_value !== 0xff) {\n                decoded.temperature_dehumidify.temperature_tolerance = temperature_tolerance_value / 10;\n            }\n            offset += 2;\n            break;\n        case 0x1b:\n            var masked = readUInt8(bytes[offset]);\n            var enabled = readUInt8(bytes[offset + 1]);\n            decoded.temperature_up_down_enable = {};\n            var bit_offset = { forward_enable: 0, backward_enable: 1 };\n            for (var key in bit_offset) {\n                if ((masked >>> bit_offset[key]) & 0x01) {\n                    decoded.temperature_up_down_enable[key] = readEnableStatus((enabled >>> bit_offset[key]) & 0x01);\n                }\n            }\n            offset += 2;\n            break;\n        case 0x3a:\n            decoded.wires_relay_change_report_enable = readEnableStatus(readUInt8(bytes[offset]));\n            offset += 1;\n            break;\n        case 0x3b:\n            var value = readUInt8(bytes[offset]);\n            decoded.aux_control_config = {};\n            var aux_control_bit_offset = { y2_enable: 0, w2_enable: 1 };\n            for (var aux_wire_key in aux_control_bit_offset) {\n                if ((value >>> (aux_control_bit_offset[aux_wire_key] + 4)) & 0x01) {\n                    decoded.aux_control_config[aux_wire_key] = readEnableStatus((value >>> aux_control_bit_offset[aux_wire_key]) & 0x01);\n                }\n            }\n            offset += 1;\n            break;\n        case 0x3e:\n            var d2d_master_id = {};\n            d2d_master_id.id = readUInt8(bytes[offset]) + 1;\n            d2d_master_id.dev_eui = toHexString(bytes.slice(offset + 1, offset + 9));\n            offset += 9;\n            decoded.d2d_master_ids = decoded.d2d_master_ids || [];\n            decoded.d2d_master_ids.push(d2d_master_id);\n            break;\n        case 0x41:\n            decoded.target_temperature_resolution = readTemperatureResolution(readUInt8(bytes[offset]));\n            offset += 1;\n            break;\n        case 0x42:\n            decoded.target_temperature_range_config = {};\n            decoded.target_temperature_range_config.temperature_control_mode = readTemperatureControlMode(readUInt8(bytes[offset]));\n            decoded.target_temperature_range_config.min = readInt16LE(bytes.slice(offset + 1, offset + 3)) / 10;\n            decoded.target_temperature_range_config.max = readInt16LE(bytes.slice(offset + 3, offset + 5)) / 10;\n            offset += 5;\n            break;\n        case 0x43:\n            decoded.temperature_level_up_down_delta = {};\n            // skip the first byte\n            decoded.temperature_level_up_down_delta.delta_1 = readUInt8(bytes[offset + 1]) / 10;\n            decoded.temperature_level_up_down_delta.delta_2 = readUInt8(bytes[offset + 2]) / 10;\n            offset += 3;\n            break;\n        case 0x44:\n            decoded.fan_delay_config = {};\n            decoded.fan_delay_config.enable = readEnableStatus(readUInt8(bytes[offset]));\n            decoded.fan_delay_config.delay_time = readUInt16LE(bytes.slice(offset + 1, offset + 3));\n            offset += 3;\n            break;\n        case 0x45:\n            decoded.system_status = readOnOffStatus(readUInt8(bytes[offset]));\n            decoded.temperature_control_mode = readTemperatureControlMode(readUInt8(bytes[offset + 1]));\n            decoded.target_temperature = readInt16LE(bytes.slice(offset + 2, offset + 4)) / 10;\n            offset += 4;\n            break;\n        case 0x46:\n            decoded.compressor_aux_combine_enable = readEnableStatus(readUInt8(bytes[offset]));\n            offset += 1;\n            break;\n        case 0x47:\n            decoded.system_protect_config = {};\n            decoded.system_protect_config.enable = readEnableStatus(readUInt8(bytes[offset]));\n            decoded.system_protect_config.duration = readUInt8(bytes[offset + 1]);\n            offset += 2;\n            break;\n        case 0x58:\n            decoded.target_temperature_dual = readEnableStatus(readUInt8(bytes[offset]));\n            offset += 1;\n            break;\n        case 0x59:\n            var dual_temperature_plan_config = readDualTemperaturePlanConfig(bytes.slice(offset, offset + 9));\n            decoded.dual_temperature_plan_config = decoded.dual_temperature_plan_config || [];\n            decoded.dual_temperature_plan_config.push(dual_temperature_plan_config);\n            offset += 9;\n            break;\n        case 0x5a:\n            decoded.dual_temperature_tolerance = decoded.dual_temperature_tolerance || {};\n            var tolerance_index = readUInt8(bytes[offset]);\n            var tolerance_value = readUInt8(bytes[offset + 1]) / 10;\n            if (tolerance_index === 0x00) {\n                decoded.dual_temperature_tolerance.heat_tolerance = tolerance_value;\n            } else if (tolerance_index === 0x01) {\n                decoded.dual_temperature_tolerance.cool_tolerance = tolerance_value;\n            }\n            offset += 2;\n            break;\n        case 0x5c:\n            var unlock_button_bit_offset = { power_button: 0, temperature_up_button: 1, temperature_down_button: 2, fan_mode_button: 3, temperature_control_mode_button: 4 };\n            var unlock_button_data = readUInt8(bytes[offset]);\n            decoded.unlock_config = {};\n            for (var btn in unlock_button_bit_offset) {\n                decoded.unlock_config[btn] = readEnableStatus((unlock_button_data >>> unlock_button_bit_offset[btn]) & 0x01);\n            }\n            decoded.unlock_config.time = readUInt16LE(bytes.slice(offset + 1, offset + 3));\n            offset += 3;\n            break;\n        case 0x5e:\n            var single_temperature_plan_config = readSingleTemperaturePlanConfig(bytes.slice(offset, offset + 7));\n            decoded.single_temperature_plan_config = decoded.single_temperature_plan_config || [];\n            decoded.single_temperature_plan_config.push(single_temperature_plan_config);\n            offset += 7;\n            break;\n        case 0x5d:\n            var forbidden_control_bit_offset = { heat_enable: 0, em_heat_enable: 1, cool_enable: 2, auto_enable: 3 };\n            var forbidden_control_data = readUInt8(bytes[offset]);\n            decoded.temperature_control_forbidden_config = {};\n            for (var forbidden_key in forbidden_control_bit_offset) {\n                decoded.temperature_control_forbidden_config[forbidden_key] = readEnableStatus((forbidden_control_data >>> forbidden_control_bit_offset[forbidden_key]) & 0x01);\n            }\n            offset += 1;\n            break;\n        case 0x62:\n            decoded.fan_control_during_heating = readFanControlDuringHeating(readUInt8(bytes[offset]));\n            offset += 1;\n            break;\n        case 0x8b:\n            decoded.plan_schedule_enable_config = readPlanScheduleEnableConfig(bytes.slice(offset, offset + 2));\n            offset += 2;\n            break;\n        default:\n            throw new Error(\"unknown downlink response\");\n    }\n\n    if (hasResultFlag(code)) {\n        var result_value = readUInt8(bytes[offset]);\n        offset += 1;\n\n        if (result_value !== 0) {\n            var request = decoded;\n            decoded = {};\n            decoded.device_response_result = {};\n            decoded.device_response_result.channel_type = channel_type;\n            decoded.device_response_result.result = readResultStatus(result_value);\n            decoded.device_response_result.request = request;\n        }\n    }\n\n    return { data: decoded, offset: offset };\n}\n\nfunction hasResultFlag(code) {\n    return code === 0xf8;\n}\n\nfunction readResultStatus(status) {\n    var status_map = { 0: \"success\", 1: \"forbidden\", 2: \"invalid parameter\" };\n    return getValue(status_map, status);\n}\n\nfunction readProtocolVersion(bytes) {\n    var major = (bytes & 0xf0) >> 4;\n    var minor = bytes & 0x0f;\n    return \"v\" + major + \".\" + minor;\n}\n\nfunction readHardwareVersion(bytes) {\n    var major = (bytes[0] & 0xff).toString(16);\n    var minor = (bytes[1] & 0xff) >> 4;\n    return \"v\" + major + \".\" + minor;\n}\n\nfunction readFirmwareVersion(bytes) {\n    var major = (bytes[0] & 0xff).toString(16);\n    var minor = (bytes[1] & 0xff).toString(16);\n    return \"v\" + major + \".\" + minor;\n}\n\nfunction readTslVersion(bytes) {\n    var major = bytes[0] & 0xff;\n    var minor = bytes[1] & 0xff;\n    return \"v\" + major + \".\" + minor;\n}\n\nfunction readSerialNumber(bytes) {\n    var temp = [];\n    for (var idx = 0; idx < bytes.length; idx++) {\n        temp.push((\"0\" + (bytes[idx] & 0xff).toString(16)).slice(-2));\n    }\n    return temp.join(\"\");\n}\n\nfunction readLoRaWANClass(type) {\n    var class_map = {\n        0: \"Class A\",\n        1: \"Class B\",\n        2: \"Class C\",\n        3: \"Class CtoB\",\n    };\n    return getValue(class_map, type);\n}\n\nfunction readResetEvent(status) {\n    var status_map = { 0: \"normal\", 1: \"reset\" };\n    return getValue(status_map, status);\n}\n\nfunction readDeviceStatus(status) {\n    var status_map = { 0: \"off\", 1: \"on\" };\n    return getValue(status_map, status);\n}\n\nfunction readEnableStatus(status) {\n    var status_map = { 0: \"disable\", 1: \"enable\" };\n    return getValue(status_map, status);\n}\n\nfunction readYesNoStatus(type) {\n    var yes_no_status_map = { 0: \"no\", 1: \"yes\" };\n    return getValue(yes_no_status_map, type);\n}\n\nfunction readOnOffStatus(type) {\n    var on_off_status_map = { 0: \"off\", 1: \"on\" };\n    return getValue(on_off_status_map, type);\n}\n\nfunction readTemperatureUnit(type) {\n    var temperature_unit_map = { 0: \"celsius\", 1: \"fahrenheit\" };\n    return getValue(temperature_unit_map, type);\n}\n\nfunction readTemperatureResolution(type) {\n    var temperature_resolution_map = { 0: 0.5, 1: 1 };\n    return getValue(temperature_resolution_map, type);\n}\n\nfunction readTemperatureAlarm(type) {\n    var temperature_alarm_map = {\n        1: \"emergency heating timeout alarm\",\n        2: \"auxiliary heating timeout alarm\",\n        3: \"persistent low temperature alarm\",\n        4: \"persistent low temperature alarm release\",\n        5: \"persistent high temperature alarm\",\n        6: \"persistent high temperature alarm release\",\n        7: \"freeze protection alarm\",\n        8: \"freeze protection alarm release\",\n        9: \"threshold alarm\",\n        10: \"threshold alarm release\",\n    };\n    return getValue(temperature_alarm_map, type);\n}\n\nfunction readSensorStatus(type) {\n    var sensor_status_map = {\n        1: \"read failed\",\n        2: \"out of range\",\n    };\n    return getValue(sensor_status_map, type);\n}\n\nfunction readExecutePlanType(type) {\n    var fix_type = type - 1;\n    if (fix_type === -1) {\n        fix_type = 255;\n    }\n    var plan_event_map = {\n        0: \"wake\",\n        1: \"away\",\n        2: \"home\",\n        3: \"sleep\",\n        4: \"occupied\",\n        5: \"vacant\",\n        6: \"eco\",\n        255: \"not executed\",\n    };\n    return getValue(plan_event_map, fix_type);\n}\n\nfunction readPlanType(type) {\n    var plan_event_type_map = {\n        0: \"wake\",\n        1: \"away\",\n        2: \"home\",\n        3: \"sleep\",\n        4: \"occupied\",\n        5: \"vacant\",\n        6: \"eco\",\n    };\n    return getValue(plan_event_type_map, type);\n}\n\nfunction readFanMode(type) {\n    var fan_mode_map = {\n        0: \"auto\",\n        1: \"on\",\n        2: \"circulate\",\n        3: \"disable\",\n    };\n    return getValue(fan_mode_map, type);\n}\n\nfunction readFanStatus(type) {\n    var fan_status_map = {\n        0: \"standby\",\n        1: \"high speed\",\n        2: \"low speed\",\n        3: \"on\",\n    };\n    return getValue(fan_status_map, type);\n}\n\nfunction readControlPermission(type) {\n    var control_permission_map = { 0: \"thermostat\", 1: \"remote control\" };\n    return getValue(control_permission_map, type);\n}\n\nfunction readOfflineControlMode(type) {\n    var offline_control_mode_map = { 0: \"keep\", 1: \"thermostat\", 2: \"off\" };\n    return getValue(offline_control_mode_map, type);\n}\n\nfunction readTemperatureControlMode(type) {\n    var temperature_control_mode_map = {\n        0: \"heat\",\n        1: \"em heat\",\n        2: \"cool\",\n        3: \"auto\",\n        4: \"auto heat\",\n        5: \"auto cool\",\n    };\n    return getValue(temperature_control_mode_map, type);\n}\n\nfunction readTemperatureControlStatus(type) {\n    var temperature_control_status_map = {\n        0: \"standby\",\n        1: \"stage-1 heat\",\n        2: \"stage-2 heat\",\n        3: \"stage-3 heat\",\n        4: \"stage-4 heat\",\n        5: \"em heat\",\n        6: \"stage-1 cool\",\n        7: \"stage-2 cool\",\n        8: \"stage-5 heat\",\n    };\n    return getValue(temperature_control_status_map, type);\n}\n\nfunction readWires(wire1, wire2, wire3) {\n    var wire = {};\n    wire.y1 = readOnOffStatus((wire1 >>> 0) & 0x03);\n    wire.gh = readOnOffStatus((wire1 >>> 2) & 0x03);\n    wire.ob = readOnOffStatus((wire1 >>> 4) & 0x03);\n    wire.w1 = readOnOffStatus((wire1 >>> 6) & 0x03);\n\n    wire.e = readOnOffStatus((wire2 >>> 0) & 0x03);\n    wire.di = readOnOffStatus((wire2 >>> 2) & 0x03);\n    wire.pek = readOnOffStatus((wire2 >>> 4) & 0x03);\n    wire.w2 = readOnOffStatus((wire2 >>> 6) & 0x01);\n    wire.aux = readOnOffStatus(((wire2 >>> 6) & 0x03) === 2 ? 1 : 0);\n\n    wire.y2 = readOnOffStatus((wire3 >>> 0) & 0x01);\n    wire.gl = readOnOffStatus(((wire3 >>> 0) & 0x03) === 2 ? 1 : 0);\n    return wire;\n}\n\nfunction readWiresRelay(status) {\n    var relay = {};\n    relay.y1 = readOnOffStatus((status >>> 0) & 0x01);\n    relay.y2_gl = readOnOffStatus((status >>> 1) & 0x01);\n    relay.w1 = readOnOffStatus((status >>> 2) & 0x01);\n    relay.w2_aux = readOnOffStatus((status >>> 3) & 0x01);\n    relay.e = readOnOffStatus((status >>> 4) & 0x01);\n    relay.g = readOnOffStatus((status >>> 5) & 0x01);\n    relay.ob = readOnOffStatus((status >>> 6) & 0x01);\n    return relay;\n}\n\nfunction readObMode(type) {\n    var ob_mode_map = {\n        0: \"on cool\",\n        1: \"on heat\",\n        3: \"hold\",\n    };\n    return getValue(ob_mode_map, type);\n}\n\nfunction readActionType(type) {\n    var action_type_map = {\n        0: \"power\",\n        1: \"plan\",\n    };\n    return getValue(action_type_map, type);\n}\n\nfunction readTemperatureLevelUpCondition(type) {\n    var temperature_level_up_condition_map = { 0: \"heat\", 1: \"cool\" };\n    return getValue(temperature_level_up_condition_map, type);\n}\n\nfunction readTemperatureControlSupportMode(value) {\n    var enable = {};\n    enable.heat = readEnableStatus((value >>> 0) & 0x01);\n    enable.em_heat = readEnableStatus((value >>> 1) & 0x01);\n    enable.cool = readEnableStatus((value >>> 2) & 0x01);\n    enable.auto = readEnableStatus((value >>> 3) & 0x01);\n    return enable;\n}\n\nfunction readTemperatureControlSupportStatus(heat_mode_value, cool_mode_value) {\n    var enable = {};\n    enable.stage_1_heat = readEnableStatus((heat_mode_value >>> 0) & 0x01);\n    enable.stage_2_heat = readEnableStatus((heat_mode_value >>> 1) & 0x01);\n    enable.stage_3_heat = readEnableStatus((heat_mode_value >>> 2) & 0x01);\n    enable.stage_4_heat = readEnableStatus((heat_mode_value >>> 3) & 0x01);\n    enable.stage_5_heat = readEnableStatus((heat_mode_value >>> 4) & 0x01);\n    enable.stage_1_cool = readEnableStatus((cool_mode_value >>> 0) & 0x01);\n    enable.stage_2_cool = readEnableStatus((cool_mode_value >>> 1) & 0x01);\n    return enable;\n}\n\nfunction readWeekRecycleSettings(value) {\n    var week_day_bits_offset = { monday: 1, tuesday: 2, wednesday: 3, thursday: 4, friday: 5, saturday: 6, sunday: 7 };\n    var week_enable = {};\n    for (var day in week_day_bits_offset) {\n        week_enable[day] = readEnableStatus((value >>> week_day_bits_offset[day]) & 0x01);\n    }\n    return week_enable;\n}\n\nfunction readTimeZone(time_zone) {\n    var timezone_map = { \"-720\": \"UTC-12\", \"-660\": \"UTC-11\", \"-600\": \"UTC-10\", \"-570\": \"UTC-9:30\", \"-540\": \"UTC-9\", \"-480\": \"UTC-8\", \"-420\": \"UTC-7\", \"-360\": \"UTC-6\", \"-300\": \"UTC-5\", \"-240\": \"UTC-4\", \"-210\": \"UTC-3:30\", \"-180\": \"UTC-3\", \"-120\": \"UTC-2\", \"-60\": \"UTC-1\", 0: \"UTC\", 60: \"UTC+1\", 120: \"UTC+2\", 180: \"UTC+3\", 210: \"UTC+3:30\", 240: \"UTC+4\", 270: \"UTC+4:30\", 300: \"UTC+5\", 330: \"UTC+5:30\", 345: \"UTC+5:45\", 360: \"UTC+6\", 390: \"UTC+6:30\", 420: \"UTC+7\", 480: \"UTC+8\", 540: \"UTC+9\", 570: \"UTC+9:30\", 600: \"UTC+10\", 630: \"UTC+10:30\", 660: \"UTC+11\", 720: \"UTC+12\", 765: \"UTC+12:45\", 780: \"UTC+13\", 840: \"UTC+14\" };\n    return getValue(timezone_map, time_zone);\n}\n\nfunction readSingleTemperaturePlanConfig(bytes) {\n    var offset = 0;\n\n    var config = {};\n    config.plan_type = readPlanType(readUInt8(bytes[offset]));\n    config.temperature_control_mode = readTemperatureControlMode(readUInt8(bytes[offset + 1]));\n    config.fan_mode = readFanMode(readUInt8(bytes[offset + 2]));\n    config.target_temperature = readInt16LE(bytes.slice(offset + 3, offset + 5)) / 10;\n    config.target_temperature_tolerance = readUInt8(bytes[offset + 5]) / 10;\n    config.temperature_control_tolerance = readUInt8(bytes[offset + 6]) / 10;\n    return config;\n}\n\nfunction readDualTemperaturePlanConfig(bytes) {\n    var offset = 0;\n\n    var config = {};\n    config.type = readPlanType(readUInt8(bytes[offset]));\n    config.temperature_control_mode = readTemperatureControlMode(readUInt8(bytes[offset + 1]));\n    config.fan_mode = readFanMode(readUInt8(bytes[offset + 2]));\n    var heat_target_temperature_value = readUInt16LE(bytes.slice(offset + 3, offset + 5));\n    if (heat_target_temperature_value !== 0xffff) {\n        config.heat_target_temperature = readUInt16LE(bytes.slice(offset + 3, offset + 5)) / 10;\n    }\n    var heat_temperature_tolerance_value = readUInt8(bytes[offset + 5]);\n    if (heat_temperature_tolerance_value !== 0xff) {\n        config.heat_temperature_tolerance = heat_temperature_tolerance_value / 10;\n    }\n    var cool_target_temperature_value = readUInt16LE(bytes.slice(offset + 6, offset + 8));\n    if (cool_target_temperature_value !== 0xffff) {\n        config.cool_target_temperature = readUInt16LE(bytes.slice(offset + 6, offset + 8)) / 10;\n    }\n    var cool_temperature_tolerance_value = readUInt8(bytes[offset + 8]);\n    if (cool_temperature_tolerance_value !== 0xff) {\n        config.cool_temperature_tolerance = cool_temperature_tolerance_value / 10;\n    }\n    return config;\n}\n\nfunction readPlanSchedule(bytes) {\n    var offset = 0;\n    var schedule = {};\n    schedule.plan_type = readPlanType(bytes[offset]);\n    schedule.id = bytes[offset + 1] + 1;\n    schedule.enable = readEnableStatus(bytes[offset + 2]);\n    schedule.week_recycle = readWeekRecycleSettings(bytes[offset + 3]);\n    schedule.time = readUInt16LE(bytes.slice(offset + 4, offset + 6));\n    return schedule;\n}\n\nfunction readPlanScheduleEnableConfig(bytes) {\n    var offset = 0;\n    var mask = readUInt8(bytes[offset]);\n    var value = readUInt8(bytes[offset + 1]);\n\n    var plan_schedule_enable_config = {};\n    var plan_bit_offset = { wake: 0, away: 1, home: 2, sleep: 3, occupied: 4, vacant: 5, eco: 6 };\n    for (var key in plan_bit_offset) {\n        if ((mask >>> plan_bit_offset[key]) & 0x01) {\n            plan_schedule_enable_config[key] = readEnableStatus((value >>> plan_bit_offset[key]) & 0x01);\n        }\n    }\n    return plan_schedule_enable_config;\n}\nfunction readD2DCommand(bytes) {\n    return (\"0\" + (bytes[1] & 0xff).toString(16)).slice(-2) + (\"0\" + (bytes[0] & 0xff).toString(16)).slice(-2);\n}\n\nfunction readD2DMasterConfig(bytes) {\n    var offset = 0;\n    var config = {};\n    config.plan_type = readPlanType(readUInt8(bytes[offset]));\n    config.enable = readEnableStatus(bytes[offset + 1]);\n    config.lora_uplink_enable = readEnableStatus(bytes[offset + 2]);\n    config.d2d_cmd = readD2DCommand(bytes.slice(offset + 3, offset + 5));\n    config.time = readUInt16LE(bytes.slice(offset + 5, offset + 7));\n    config.time_enable = readEnableStatus(bytes[offset + 7]);\n    return config;\n}\n\nfunction readD2DSlaveConfig(bytes) {\n    var offset = 0;\n    var config = {};\n    config.id = readUInt8(bytes[offset]) + 1;\n    config.enable = readEnableStatus(bytes[offset + 1]);\n    config.d2d_cmd = readD2DCommand(bytes.slice(offset + 2, offset + 4));\n\n    var value = readUInt8(bytes[offset + 4]);\n    // value = action(0..3) + action_type(4..7)\n    var action_value = value & 0x0f;\n    var action_type_value = (value >>> 4) & 0x0f;\n    config.action = {};\n    if (action_type_value === 0) {\n        config.action.action_type = readActionType(action_type_value);\n        config.action.system_status = readOnOffStatus(action_value);\n    } else {\n        config.action.action_type = readActionType(action_type_value);\n        config.action.plan_type = readPlanType(action_value);\n    }\n    return config;\n}\n\nfunction readTemperatureSource(value) {\n    var source_map = { 0: \"disable\", 1: \"lora\", 2: \"d2d\" };\n    return getValue(source_map, value);\n}\n\nfunction readFanControlDuringHeating(value) {\n    var mode_map = { 0: \"furnace\", 1: \"thermostat\" };\n    return getValue(mode_map, value);\n}\n\n/* eslint-disable */\nfunction readUInt8(bytes) {\n    return bytes & 0xff;\n}\n\nfunction readInt8(bytes) {\n    var ref = readUInt8(bytes);\n    return ref > 0x7f ? ref - 0x100 : ref;\n}\n\nfunction readUInt16LE(bytes) {\n    var value = (bytes[1] << 8) + bytes[0];\n    return value & 0xffff;\n}\n\nfunction readInt16LE(bytes) {\n    var ref = readUInt16LE(bytes);\n    return ref > 0x7fff ? ref - 0x10000 : ref;\n}\n\nfunction readUInt32LE(bytes) {\n    var value = (bytes[3] << 24) + (bytes[2] << 16) + (bytes[1] << 8) + bytes[0];\n    return (value & 0xffffffff) >>> 0;\n}\n\nfunction readInt32LE(bytes) {\n    var ref = readUInt32LE(bytes);\n    return ref > 0x7fffffff ? ref - 0x100000000 : ref;\n}\n\n// hex bytes to hex string\nfunction toHexString(bytes) {\n    for (var i = 0; i < bytes.length; i++) {\n        bytes[i] = (\"0\" + bytes[i].toString(16)).slice(-2);\n    }\n    return bytes.join(\"\");\n}\n\nfunction getValue(map, key) {\n    if (RAW_VALUE) return key;\n\n    var value = map[key];\n    if (!value) value = \"unknown\";\n    return value;\n}\n\nif (!Object.assign) {\n    Object.defineProperty(Object, \"assign\", {\n        enumerable: false,\n        configurable: true,\n        writable: true,\n        value: function (target) {\n            \"use strict\";\n            if (target == null) {\n                throw new TypeError(\"Cannot convert first argument to object\");\n            }\n\n            var to = Object(target);\n            for (var i = 1; i < arguments.length; i++) {\n                var nextSource = arguments[i];\n                if (nextSource == null) {\n                    continue;\n                }\n                nextSource = Object(nextSource);\n\n                var keysArray = Object.keys(Object(nextSource));\n                for (var nextIndex = 0, len = keysArray.length; nextIndex < len; nextIndex++) {\n                    var nextKey = keysArray[nextIndex];\n                    var desc = Object.getOwnPropertyDescriptor(nextSource, nextKey);\n                    if (desc !== undefined && desc.enumerable) {\n                        // concat array\n                        if (Array.isArray(to[nextKey]) && Array.isArray(nextSource[nextKey])) {\n                            to[nextKey] = to[nextKey].concat(nextSource[nextKey]);\n                        } else {\n                            to[nextKey] = nextSource[nextKey];\n                        }\n                    }\n                }\n            }\n            return to;\n        },\n    });\n}",
            "environment": "javascript",
            "storage": "",
            "version": "1.0"
          },
          "flows": {
            "milesight_wt201v2_decoder": {
              "data": {
                "payload": "{{payload}}",
                "payload_function": "decodeThingerUplink",
                "payload_type": "source_payload",
                "resource": "uplink",
                "source": "resource",
                "update": "events"
              },
              "enabled": true,
              "sink": {
                "payload": "{{payload}}",
                "payload_function": "",
                "payload_type": "source_payload",
                "resource_stream": "uplink_decoded",
                "target": "resource_stream"
              },
              "split_data": false
            }
          },
          "properties": {
            "uplink": {
              "data": {
                "payload": "{{payload}}",
                "payload_function": "",
                "payload_type": "source_payload",
                "resource": "uplink",
                "source": "resource",
                "update": "events"
              },
              "default": {
                "source": "value"
              },
              "enabled": true
            }
          }
        },
        "_resources": {
          "properties": [
            {
              "property": "dashboard",
              "value": {
                "tabs": [
                  {
                    "name": "Overview",
                    "widgets": [
                      {
                        "layout": {
                          "col": 0,
                          "row": 0,
                          "sizeX": 2,
                          "sizeY": 5
                        },
                        "panel": {
                          "color": "#ffffff",
                          "currentColor": "#ffffff",
                          "showOffline": {
                            "type": "none"
                          },
                          "title": "Temperature"
                        },
                        "properties": {
                          "decimalPlaces": 1,
                          "enableExtraTextColor": false,
                          "enableIconColor": false,
                          "enableIconSize": false,
                          "extraText": "",
                          "extraTextColor": "#1E313E",
                          "extraTextColorConditions": [],
                          "extraTextConditions": [],
                          "extraTextPosition": "above-value",
                          "extraTextSize": "20px",
                          "extraTextWeight": "font-light",
                          "icon": "",
                          "iconColor": "#1E313E",
                          "iconColorConditions": [],
                          "iconConditions": [],
                          "iconGap": "8px",
                          "iconPosition": "before-value",
                          "iconSize": "75px",
                          "iconVerticalOffset": "0px",
                          "link": "",
                          "textAlign": "center",
                          "textColor": "#1E313E",
                          "textColorConditions": [],
                          "textSize": "75px",
                          "textWeight": "font-light",
                          "unit": "ºC",
                          "unitSize": "20px"
                        },
                        "sources": [
                          {
                            "bucket": {
                              "backend": "mongodb",
                              "id": "milesight_wt201v2_data",
                              "mapping": "temperature",
                              "tags": {
                                "device": [],
                                "group": []
                              }
                            },
                            "color": "#1abc9c",
                            "name": "Temperature",
                            "source": "bucket",
                            "timespan": {
                              "mode": "latest"
                            }
                          }
                        ],
                        "type": "text"
                      },
                      {
                        "layout": {
                          "col": 2,
                          "row": 0,
                          "sizeX": 2,
                          "sizeY": 5
                        },
                        "panel": {
                          "color": "#ffffff",
                          "currentColor": "#ffffff",
                          "showOffline": {
                            "type": "none"
                          },
                          "title": "Target Temperature"
                        },
                        "properties": {
                          "decimalPlaces": 1,
                          "enableExtraTextColor": false,
                          "enableIconColor": false,
                          "enableIconSize": false,
                          "extraText": "",
                          "extraTextColor": "#1E313E",
                          "extraTextColorConditions": [],
                          "extraTextConditions": [],
                          "extraTextPosition": "above-value",
                          "extraTextSize": "20px",
                          "extraTextWeight": "font-light",
                          "icon": "",
                          "iconColor": "#1E313E",
                          "iconColorConditions": [],
                          "iconConditions": [],
                          "iconGap": "8px",
                          "iconPosition": "before-value",
                          "iconSize": "75px",
                          "iconVerticalOffset": "0px",
                          "link": "",
                          "textAlign": "center",
                          "textColor": "#1E313E",
                          "textColorConditions": [],
                          "textSize": "75px",
                          "textWeight": "font-light",
                          "unit": "ºC",
                          "unitSize": "20px"
                        },
                        "sources": [
                          {
                            "bucket": {
                              "backend": "mongodb",
                              "id": "milesight_wt201v2_data",
                              "mapping": "target_temperature",
                              "tags": {
                                "device": [],
                                "group": []
                              }
                            },
                            "color": "#3498db",
                            "name": "Target",
                            "source": "bucket",
                            "timespan": {
                              "mode": "latest"
                            }
                          }
                        ],
                        "type": "text"
                      },
                      {
                        "layout": {
                          "col": 4,
                          "row": 0,
                          "sizeX": 2,
                          "sizeY": 5
                        },
                        "panel": {
                          "color": "#ffffff",
                          "currentColor": "#ffffff",
                          "showOffline": {
                            "type": "none"
                          },
                          "title": "Humidity"
                        },
                        "properties": {
                          "decimalPlaces": 1,
                          "enableExtraTextColor": false,
                          "enableIconColor": false,
                          "enableIconSize": false,
                          "extraText": "",
                          "extraTextColor": "#1E313E",
                          "extraTextColorConditions": [],
                          "extraTextConditions": [],
                          "extraTextPosition": "above-value",
                          "extraTextSize": "20px",
                          "extraTextWeight": "font-light",
                          "icon": "",
                          "iconColor": "#1E313E",
                          "iconColorConditions": [],
                          "iconConditions": [],
                          "iconGap": "8px",
                          "iconPosition": "before-value",
                          "iconSize": "75px",
                          "iconVerticalOffset": "0px",
                          "link": "",
                          "textAlign": "center",
                          "textColor": "#1E313E",
                          "textColorConditions": [],
                          "textSize": "75px",
                          "textWeight": "font-light",
                          "unit": "%",
                          "unitSize": "20px"
                        },
                        "sources": [
                          {
                            "bucket": {
                              "backend": "mongodb",
                              "id": "milesight_wt201v2_data",
                              "mapping": "humidity",
                              "tags": {
                                "device": [],
                                "group": []
                              }
                            },
                            "color": "#9b59b6",
                            "name": "Humidity",
                            "source": "bucket",
                            "timespan": {
                              "mode": "latest"
                            }
                          }
                        ],
                        "type": "text"
                      },
                      {
                        "layout": {
                          "col": 0,
                          "row": 5,
                          "sizeX": 6,
                          "sizeY": 10
                        },
                        "panel": {
                          "color": "#ffffff",
                          "currentColor": "#ffffff",
                          "showOffline": {
                            "type": "none"
                          },
                          "title": "Temperature & Humidity (24h)"
                        },
                        "properties": {
                          "alignTimeSeries": false,
                          "dataAppend": false,
                          "options": "var options = {\n    chart: {\n        type: 'line',\n        stacked: false\n    },\n    dataLabels: {\n        enabled: false\n    },\n    stroke: {\n        curve: 'smooth',\n        width: [3, 3, 2]\n    },\n    xaxis: {\n        type: 'datetime',\n        labels: {\n            datetimeUTC: false\n        },\n        tooltip: {\n            enabled: false\n        }\n    },\n    yaxis: [\n        {\n            seriesName: 'Temperature',\n            title: {\n                text: 'Temperature (ºC)'\n            },\n            labels: {\n                formatter: function (val) {\n                    if (val !== null && typeof val !== 'undefined')\n                        return val.toFixed(1);\n                }\n            }\n        },\n        {\n            seriesName: 'Temperature',\n            show: false\n        },\n        {\n            opposite: true,\n            seriesName: 'Humidity',\n            title: {\n                text: 'Humidity (%)'\n            },\n            labels: {\n                formatter: function (val) {\n                    if (val !== null && typeof val !== 'undefined')\n                        return val.toFixed(1);\n                }\n            }\n        }\n    ],\n    tooltip: {\n        x: {\n            format: 'dd/MM/yyyy HH:mm:ss'\n        }\n    },\n    legend: {\n        position: 'top'\n    }\n};",
                          "realTimeUpdate": true
                        },
                        "sources": [
                          {
                            "aggregation": {},
                            "bucket": {
                              "backend": "mongodb",
                              "id": "milesight_wt201v2_data",
                              "mapping": "temperature",
                              "tags": {
                                "device": [],
                                "group": []
                              }
                            },
                            "color": "#e74c3c",
                            "name": "Temperature",
                            "source": "bucket",
                            "timespan": {
                              "magnitude": "hour",
                              "mode": "relative",
                              "period": "latest",
                              "value": 24
                            }
                          },
                          {
                            "aggregation": {},
                            "bucket": {
                              "backend": "mongodb",
                              "id": "milesight_wt201v2_data",
                              "mapping": "target_temperature",
                              "tags": {
                                "device": [],
                                "group": []
                              }
                            },
                            "color": "#3498db",
                            "name": "Target",
                            "source": "bucket",
                            "timespan": {
                              "magnitude": "hour",
                              "mode": "relative",
                              "period": "latest",
                              "value": 24
                            }
                          },
                          {
                            "aggregation": {},
                            "bucket": {
                              "backend": "mongodb",
                              "id": "milesight_wt201v2_data",
                              "mapping": "humidity",
                              "tags": {
                                "device": [],
                                "group": []
                              }
                            },
                            "color": "#9b59b6",
                            "name": "Humidity",
                            "source": "bucket",
                            "timespan": {
                              "magnitude": "hour",
                              "mode": "relative",
                              "period": "latest",
                              "value": 24
                            }
                          }
                        ],
                        "type": "apex_charts"
                      },
                      {
                        "layout": {
                          "col": 0,
                          "row": 15,
                          "sizeX": 2,
                          "sizeY": 4
                        },
                        "panel": {
                          "color": "#ffffff",
                          "currentColor": "#ffffff",
                          "showOffline": {
                            "type": "none"
                          },
                          "title": "System Status"
                        },
                        "properties": {
                          "decimalPlaces": 0,
                          "enableExtraTextColor": false,
                          "enableIconColor": false,
                          "enableIconSize": false,
                          "extraText": "",
                          "extraTextColor": "#1E313E",
                          "extraTextColorConditions": [],
                          "extraTextConditions": [],
                          "extraTextPosition": "above-value",
                          "extraTextSize": "20px",
                          "extraTextWeight": "font-light",
                          "icon": "",
                          "iconColor": "#1E313E",
                          "iconColorConditions": [],
                          "iconConditions": [],
                          "iconGap": "8px",
                          "iconPosition": "before-value",
                          "iconSize": "50px",
                          "iconVerticalOffset": "0px",
                          "link": "",
                          "textAlign": "center",
                          "textColor": "#1E313E",
                          "textColorConditions": [],
                          "textSize": "40px",
                          "textWeight": "font-light",
                          "unit": "",
                          "unitSize": "20px"
                        },
                        "sources": [
                          {
                            "bucket": {
                              "backend": "mongodb",
                              "id": "milesight_wt201v2_data",
                              "mapping": "system_status",
                              "tags": {
                                "device": [],
                                "group": []
                              }
                            },
                            "color": "#27ae60",
                            "name": "Status",
                            "source": "bucket",
                            "timespan": {
                              "mode": "latest"
                            }
                          }
                        ],
                        "type": "text"
                      },
                      {
                        "layout": {
                          "col": 2,
                          "row": 15,
                          "sizeX": 2,
                          "sizeY": 4
                        },
                        "panel": {
                          "color": "#ffffff",
                          "currentColor": "#ffffff",
                          "showOffline": {
                            "type": "none"
                          },
                          "title": "Control Mode"
                        },
                        "properties": {
                          "decimalPlaces": 0,
                          "enableExtraTextColor": false,
                          "enableIconColor": false,
                          "enableIconSize": false,
                          "extraText": "",
                          "extraTextColor": "#1E313E",
                          "extraTextColorConditions": [],
                          "extraTextConditions": [],
                          "extraTextPosition": "above-value",
                          "extraTextSize": "20px",
                          "extraTextWeight": "font-light",
                          "icon": "",
                          "iconColor": "#1E313E",
                          "iconColorConditions": [],
                          "iconConditions": [],
                          "iconGap": "8px",
                          "iconPosition": "before-value",
                          "iconSize": "50px",
                          "iconVerticalOffset": "0px",
                          "link": "",
                          "textAlign": "center",
                          "textColor": "#1E313E",
                          "textColorConditions": [],
                          "textSize": "30px",
                          "textWeight": "font-light",
                          "unit": "",
                          "unitSize": "20px"
                        },
                        "sources": [
                          {
                            "bucket": {
                              "backend": "mongodb",
                              "id": "milesight_wt201v2_data",
                              "mapping": "temperature_control_mode",
                              "tags": {
                                "device": [],
                                "group": []
                              }
                            },
                            "color": "#e67e22",
                            "name": "Mode",
                            "source": "bucket",
                            "timespan": {
                              "mode": "latest"
                            }
                          }
                        ],
                        "type": "text"
                      },
                      {
                        "layout": {
                          "col": 4,
                          "row": 15,
                          "sizeX": 2,
                          "sizeY": 4
                        },
                        "panel": {
                          "color": "#ffffff",
                          "currentColor": "#ffffff",
                          "showOffline": {
                            "type": "none"
                          },
                          "title": "Fan Mode"
                        },
                        "properties": {
                          "decimalPlaces": 0,
                          "enableExtraTextColor": false,
                          "enableIconColor": false,
                          "enableIconSize": false,
                          "extraText": "",
                          "extraTextColor": "#1E313E",
                          "extraTextColorConditions": [],
                          "extraTextConditions": [],
                          "extraTextPosition": "above-value",
                          "extraTextSize": "20px",
                          "extraTextWeight": "font-light",
                          "icon": "",
                          "iconColor": "#1E313E",
                          "iconColorConditions": [],
                          "iconConditions": [],
                          "iconGap": "8px",
                          "iconPosition": "before-value",
                          "iconSize": "50px",
                          "iconVerticalOffset": "0px",
                          "link": "",
                          "textAlign": "center",
                          "textColor": "#1E313E",
                          "textColorConditions": [],
                          "textSize": "30px",
                          "textWeight": "font-light",
                          "unit": "",
                          "unitSize": "20px"
                        },
                        "sources": [
                          {
                            "bucket": {
                              "backend": "mongodb",
                              "id": "milesight_wt201v2_data",
                              "mapping": "fan_mode",
                              "tags": {
                                "device": [],
                                "group": []
                              }
                            },
                            "color": "#16a085",
                            "name": "Fan",
                            "source": "bucket",
                            "timespan": {
                              "mode": "latest"
                            }
                          }
                        ],
                        "type": "text"
                      }
                    ]
                  },
                  {
                    "name": "HVAC Details",
                    "widgets": [
                      {
                        "layout": {
                          "col": 0,
                          "row": 0,
                          "sizeX": 2,
                          "sizeY": 4
                        },
                        "panel": {
                          "color": "#ffffff",
                          "currentColor": "#ffffff",
                          "showOffline": {
                            "type": "none"
                          },
                          "title": "Control Status"
                        },
                        "properties": {
                          "decimalPlaces": 0,
                          "enableExtraTextColor": false,
                          "enableIconColor": false,
                          "enableIconSize": false,
                          "extraText": "",
                          "extraTextColor": "#1E313E",
                          "extraTextColorConditions": [],
                          "extraTextConditions": [],
                          "extraTextPosition": "above-value",
                          "extraTextSize": "20px",
                          "extraTextWeight": "font-light",
                          "icon": "",
                          "iconColor": "#1E313E",
                          "iconColorConditions": [],
                          "iconConditions": [],
                          "iconGap": "8px",
                          "iconPosition": "before-value",
                          "iconSize": "50px",
                          "iconVerticalOffset": "0px",
                          "link": "",
                          "textAlign": "center",
                          "textColor": "#1E313E",
                          "textColorConditions": [],
                          "textSize": "28px",
                          "textWeight": "font-light",
                          "unit": "",
                          "unitSize": "20px"
                        },
                        "sources": [
                          {
                            "bucket": {
                              "backend": "mongodb",
                              "id": "milesight_wt201v2_data",
                              "mapping": "temperature_control_status",
                              "tags": {
                                "device": [],
                                "group": []
                              }
                            },
                            "color": "#c0392b",
                            "name": "Status",
                            "source": "bucket",
                            "timespan": {
                              "mode": "latest"
                            }
                          }
                        ],
                        "type": "text"
                      },
                      {
                        "layout": {
                          "col": 2,
                          "row": 0,
                          "sizeX": 2,
                          "sizeY": 4
                        },
                        "panel": {
                          "color": "#ffffff",
                          "currentColor": "#ffffff",
                          "showOffline": {
                            "type": "none"
                          },
                          "title": "Fan Status"
                        },
                        "properties": {
                          "decimalPlaces": 0,
                          "enableExtraTextColor": false,
                          "enableIconColor": false,
                          "enableIconSize": false,
                          "extraText": "",
                          "extraTextColor": "#1E313E",
                          "extraTextColorConditions": [],
                          "extraTextConditions": [],
                          "extraTextPosition": "above-value",
                          "extraTextSize": "20px",
                          "extraTextWeight": "font-light",
                          "icon": "",
                          "iconColor": "#1E313E",
                          "iconColorConditions": [],
                          "iconConditions": [],
                          "iconGap": "8px",
                          "iconPosition": "before-value",
                          "iconSize": "50px",
                          "iconVerticalOffset": "0px",
                          "link": "",
                          "textAlign": "center",
                          "textColor": "#1E313E",
                          "textColorConditions": [],
                          "textSize": "28px",
                          "textWeight": "font-light",
                          "unit": "",
                          "unitSize": "20px"
                        },
                        "sources": [
                          {
                            "bucket": {
                              "backend": "mongodb",
                              "id": "milesight_wt201v2_data",
                              "mapping": "fan_status",
                              "tags": {
                                "device": [],
                                "group": []
                              }
                            },
                            "color": "#27ae60",
                            "name": "Fan Status",
                            "source": "bucket",
                            "timespan": {
                              "mode": "latest"
                            }
                          }
                        ],
                        "type": "text"
                      },
                      {
                        "layout": {
                          "col": 4,
                          "row": 0,
                          "sizeX": 2,
                          "sizeY": 4
                        },
                        "panel": {
                          "color": "#ffffff",
                          "currentColor": "#ffffff",
                          "showOffline": {
                            "type": "none"
                          },
                          "title": "Plan Type"
                        },
                        "properties": {
                          "decimalPlaces": 0,
                          "enableExtraTextColor": false,
                          "enableIconColor": false,
                          "enableIconSize": false,
                          "extraText": "",
                          "extraTextColor": "#1E313E",
                          "extraTextColorConditions": [],
                          "extraTextConditions": [],
                          "extraTextPosition": "above-value",
                          "extraTextSize": "20px",
                          "extraTextWeight": "font-light",
                          "icon": "",
                          "iconColor": "#1E313E",
                          "iconColorConditions": [],
                          "iconConditions": [],
                          "iconGap": "8px",
                          "iconPosition": "before-value",
                          "iconSize": "50px",
                          "iconVerticalOffset": "0px",
                          "link": "",
                          "textAlign": "center",
                          "textColor": "#1E313E",
                          "textColorConditions": [],
                          "textSize": "28px",
                          "textWeight": "font-light",
                          "unit": "",
                          "unitSize": "20px"
                        },
                        "sources": [
                          {
                            "bucket": {
                              "backend": "mongodb",
                              "id": "milesight_wt201v2_data",
                              "mapping": "plan_type",
                              "tags": {
                                "device": [],
                                "group": []
                              }
                            },
                            "color": "#8e44ad",
                            "name": "Plan",
                            "source": "bucket",
                            "timespan": {
                              "mode": "latest"
                            }
                          }
                        ],
                        "type": "text"
                      },
                      {
                        "layout": {
                          "col": 0,
                          "row": 4,
                          "sizeX": 6,
                          "sizeY": 9
                        },
                        "panel": {
                          "color": "#ffffff",
                          "currentColor": "#ffffff",
                          "showOffline": {
                            "type": "none"
                          },
                          "title": "Temperature History (7 days)"
                        },
                        "properties": {
                          "alignTimeSeries": false,
                          "dataAppend": false,
                          "options": "var options = {\n    chart: {\n        type: 'line',\n        stacked: false\n    },\n    dataLabels: {\n        enabled: false\n    },\n    stroke: {\n        curve: 'smooth',\n        width: 3\n    },\n    xaxis: {\n        type: 'datetime',\n        labels: {\n            datetimeUTC: false\n        },\n        tooltip: {\n            enabled: false\n        }\n    },\n    yaxis: {\n        title: {\n            text: 'Temperature (ºC)'\n        },\n        labels: {\n            formatter: function (val) {\n                if (val !== null && typeof val !== 'undefined')\n                    return val.toFixed(1);\n            }\n        }\n    },\n    tooltip: {\n        x: {\n            format: 'dd/MM/yyyy HH:mm:ss'\n        }\n    },\n    legend: {\n        position: 'top'\n    }\n};",
                          "realTimeUpdate": true
                        },
                        "sources": [
                          {
                            "aggregation": {},
                            "bucket": {
                              "backend": "mongodb",
                              "id": "milesight_wt201v2_data",
                              "mapping": "temperature",
                              "tags": {
                                "device": [],
                                "group": []
                              }
                            },
                            "color": "#e74c3c",
                            "name": "Current Temp",
                            "source": "bucket",
                            "timespan": {
                              "magnitude": "day",
                              "mode": "relative",
                              "period": "latest",
                              "value": 7
                            }
                          },
                          {
                            "aggregation": {},
                            "bucket": {
                              "backend": "mongodb",
                              "id": "milesight_wt201v2_data",
                              "mapping": "target_temperature",
                              "tags": {
                                "device": [],
                                "group": []
                              }
                            },
                            "color": "#3498db",
                            "name": "Target Temp",
                            "source": "bucket",
                            "timespan": {
                              "magnitude": "day",
                              "mode": "relative",
                              "period": "latest",
                              "value": 7
                            }
                          }
                        ],
                        "type": "apex_charts"
                      },
                      {
                        "layout": {
                          "col": 0,
                          "row": 13,
                          "sizeX": 6,
                          "sizeY": 7
                        },
                        "panel": {
                          "color": "#ffffff",
                          "currentColor": "#ffffff",
                          "showOffline": {
                            "type": "none"
                          },
                          "title": "Recent Data (48h)"
                        },
                        "properties": {
                          "source": "code",
                          "template": "<div style=\"width:100%; height:100%; overflow-y:auto\">\n  <table class=\"table table-striped table-condensed\">\n    <thead>\n      <tr>\n        <th>Timestamp</th>\n        <th>Temp (ºC)</th>\n        <th>Target (ºC)</th>\n        <th>Humidity (%)</th>\n        <th>System</th>\n        <th>Mode</th>\n      </tr>\n    </thead>\n    <tbody>\n      <tr ng-repeat=\"entry in value\">\n        <td>{{ entry.ts | date:'medium' }}</td>\n        <td>{{ entry.temperature !== null && entry.temperature !== undefined ? entry.temperature.toFixed(1) : '—' }}</td>\n        <td>{{ entry.target_temperature !== null && entry.target_temperature !== undefined ? entry.target_temperature.toFixed(1) : '—' }}</td>\n        <td>{{ entry.humidity !== null && entry.humidity !== undefined ? entry.humidity.toFixed(1) : '—' }}</td>\n        <td>{{ entry.system_status || '—' }}</td>\n        <td>{{ entry.temperature_control_mode || '—' }}</td>\n      </tr>\n    </tbody>\n  </table>\n</div>"
                        },
                        "sources": [
                          {
                            "aggregation": {},
                            "bucket": {
                              "backend": "mongodb",
                              "id": "milesight_wt201v2_data",
                              "mapping": "ts",
                              "tags": {
                                "device": [],
                                "group": []
                              }
                            },
                            "color": "#95a5a6",
                            "name": "ts",
                            "source": "bucket",
                            "timespan": {
                              "magnitude": "hour",
                              "mode": "relative",
                              "period": "latest",
                              "value": 48
                            }
                          },
                          {
                            "aggregation": {},
                            "bucket": {
                              "backend": "mongodb",
                              "id": "milesight_wt201v2_data",
                              "mapping": "temperature",
                              "tags": {
                                "device": [],
                                "group": []
                              }
                            },
                            "color": "#e74c3c",
                            "name": "temperature",
                            "source": "bucket",
                            "timespan": {
                              "magnitude": "hour",
                              "mode": "relative",
                              "period": "latest",
                              "value": 48
                            }
                          },
                          {
                            "aggregation": {},
                            "bucket": {
                              "backend": "mongodb",
                              "id": "milesight_wt201v2_data",
                              "mapping": "target_temperature",
                              "tags": {
                                "device": [],
                                "group": []
                              }
                            },
                            "color": "#3498db",
                            "name": "target_temperature",
                            "source": "bucket",
                            "timespan": {
                              "magnitude": "hour",
                              "mode": "relative",
                              "period": "latest",
                              "value": 48
                            }
                          },
                          {
                            "aggregation": {},
                            "bucket": {
                              "backend": "mongodb",
                              "id": "milesight_wt201v2_data",
                              "mapping": "humidity",
                              "tags": {
                                "device": [],
                                "group": []
                              }
                            },
                            "color": "#9b59b6",
                            "name": "humidity",
                            "source": "bucket",
                            "timespan": {
                              "magnitude": "hour",
                              "mode": "relative",
                              "period": "latest",
                              "value": 48
                            }
                          },
                          {
                            "aggregation": {},
                            "bucket": {
                              "backend": "mongodb",
                              "id": "milesight_wt201v2_data",
                              "mapping": "system_status",
                              "tags": {
                                "device": [],
                                "group": []
                              }
                            },
                            "color": "#27ae60",
                            "name": "system_status",
                            "source": "bucket",
                            "timespan": {
                              "magnitude": "hour",
                              "mode": "relative",
                              "period": "latest",
                              "value": 48
                            }
                          },
                          {
                            "aggregation": {},
                            "bucket": {
                              "backend": "mongodb",
                              "id": "milesight_wt201v2_data",
                              "mapping": "temperature_control_mode",
                              "tags": {
                                "device": [],
                                "group": []
                              }
                            },
                            "color": "#e67e22",
                            "name": "temperature_control_mode",
                            "source": "bucket",
                            "timespan": {
                              "magnitude": "hour",
                              "mode": "relative",
                              "period": "latest",
                              "value": 48
                            }
                          }
                        ],
                        "type": "html_time"
                      }
                    ]
                  }
                ]
              }
            }
          ]
        }
      }
    ]
  }
}