{ "name": "Loriot Uplink Converter for MClimate 16ASPM", "type": "UPLINK", "integrationType": "LORIOT", "debugMode": false, "debugSettings": { "failuresEnabled": true, "allEnabled": false, "allEnabledUntil": 0 }, "configuration": { "scriptLang": "TBEL", "decoder": "/**\n * Decodes the incoming payload and returns a structured object containing telemetry data and attributes.\n *\n * @param {number[]} input - The raw payload received as an array of bytes.\n * @returns {Object} output - The structured output with decoded telemetry and attributes.\n */\n\nfunction decodePayload(input) {\n // Initialize the output object with empty attributes and telemetry for clarity.\n var result = { attributes: {}, telemetry: {}};\n\n // Decode serial number (SN) from the first 4 bytes of the payload.\n // Press '?' icon in the top right corner to learn more about built in helper functions and capabilities.\n result.attributes.sn = parseBytesToInt(input, 0, 4);\n\n // Extract the timestamp from metadata (represented in milliseconds).\n var timestamp = metadata.ts; // ts from the incoming message.\n\n // Initialize an object to store decoded key/value telemetry data.\n var values = {};\n\n // Decode battery level from the 5th byte of the payload.\n values.battery = parseBytesToInt(input, 4, 1);\n\n // Decode temperature from the 6th and 7th bytes of the payload (divided by 100).\n values.temperature = parseBytesToInt(input, 5, 2) / 100.0;\n\n // Decode saturation from the 8th byte of the payload.\n values.saturation = parseBytesToInt(input, 7, 1);\n\n // Combine the timestamp with values and add it to the telemetry.\n result.telemetry = {\n ts: timestamp,\n values: values\n };\n\n // Return the fully constructed output object.\n return result;\n // Same logic, less code:\n // return {\n // attributes: {\n // sn: parseBytesToInt(input, 0, 4)\n // },\n // telemetry: {\n // ts: metadata.ts,\n // values: {\n // battery: parseBytesToInt(input, 4, 1),\n // temperature: parseBytesToInt(input, 5, 2) / 100.0,\n // saturation: parseBytesToInt(input, 7, 1)\n // }\n // }\n // };\n}\n\nvar result = decodePayload(payload);\n// Uncomment this code block to overwrite values set in the main configuration window. Useful if you extract device/asset/customer/group names from the payload;\n// result.type = 'DEVICE'; // Entity type allows you to choose type of created entity. Can be 'DEVICE' or 'ASSET'.\n// result.name = 'Temperature Sensor'; // Device or asset name (the value must be unique)\n// result.profile = 'IndustrialSensorProfile'; // Device or asset profile name.\n// result.customer = 'MyCustomer'; // If customer is not null - created entity will be assigned to customer with such name.\n// result.group = 'SensorsGroup'; // If group is not null - created entity will be added to the entity group with such name.\n\n// Return the final result object.\nreturn result;\n\n/**\n * Parse a slice of bytes from an array into an integer (big-endian).\n *\n * @param {number[]} input - The array of bytes.\n * @param {number} offset - The starting index.\n * @param {number} length - The number of bytes to convert.\n * @returns {number} - The resulting integer.\n */\nfunction parseBytesToInt(input, offset, length) {\n var result = 0;\n for (var i = offset; i < offset + length; i++) {\n result = (result << 8) | (input[i] & 0xFF);\n }\n return result;\n}", "tbelDecoder": "function decodePayload(input) {\n var result = { attributes: {}, telemetry: {} };\n var timestamp = metadata.ts;\n var values = {};\n\n if (parseBytesToInt(input, 0, 1) === 1) {\n values = handleKeepalive(input, values);\n } else {\n values = handleResponse(input, values);\n var tail = input.slice(-12);\n values = handleKeepalive(tail, values);\n }\n\n result.telemetry = {\n ts: timestamp,\n values: values\n };\n return result;\n}\n\nfunction handleKeepalive(bytes, data) {\n data.internalTemperature = parseBytesToInt(bytes, 1, 1);\n data.relayState = (parseBytesToInt(bytes, 11, 1) === 0x01 ? \"ON\" : \"OFF\");\n\n var energy = parseBytesToInt(bytes, 2, 4);\n var power = parseBytesToInt(bytes, 6, 2);\n var acCurr = parseBytesToInt(bytes, 9, 2);\n var acVolt = parseBytesToInt(bytes, 8, 1);\n\n data.energy_kWh = energy / 1000;\n data.power_W = power;\n data.acVoltage_V = acVolt;\n data.acCurrent_mA = acCurr;\n\n return data;\n}\n\nfunction handleResponse(bytes, data) {\n var commands = [];\n\n var len = bytes.length - 12;\n \n for (var i = 0; i < len; i++) {\n var b = bytes[i] & 0xFF;\n var h = intToHex(b);\n \n if (h.length == 1) {\n h = '0' + h;\n } \n commands.add(h);\n }\n\n for (var i = 0; i < commands.length; i++) {\n var cmd = commands[i];\n switch (cmd) {\n case '04':\n data.deviceVersions = {\n hardware: parseInt(commands[i+1], 16),\n software: parseInt(commands[i+2], 16)\n };\n i += 2;\n break;\n case '12':\n data.keepAliveTime = parseInt(commands[i+1], 16);\n i += 1;\n break;\n case '19':\n data.joinRetryPeriod = parseInt(commands[i+1], 16) * 5 / 60;\n i += 1;\n break;\n case '1b':\n data.uplinkType = parseInt(commands[i+1], 16);\n i += 1;\n break;\n case '1d':\n var wdpC = (commands[i+1] != '00') ? parseInt(commands[i+1], 16) : false;\n var wdpUc = (commands[i+2] != '00') ? parseInt(commands[i+2], 16) : false;\n data.watchDogParams = { wdpC: wdpC, wdpUc: wdpUc };\n i += 2;\n break;\n case '1f':\n data.overheatingThreshold = parseInt(commands[i+1], 16);\n i += 1;\n break;\n case '21':\n data.overvoltageThreshold = parseInt(commands[i+1] + commands[i+2], 16);\n i += 2;\n break;\n case '23':\n data.overcurrentThreshold = parseInt(commands[i+1], 16);\n i += 1;\n break;\n case '25':\n data.overpowerThreshold = parseInt(commands[i+1] + commands[i+2], 16);\n i += 2;\n break;\n case '5a':\n data.afterOverheatingProtectionRecovery = parseInt(commands[i+1], 16);\n i += 1;\n break;\n case '5c':\n data.ledIndicationMode = parseInt(commands[i+1], 16);\n i += 1;\n break;\n case '5d':\n data.manualChangeRelayState = (parseInt(commands[i+1], 16) === 0x01);\n i += 1;\n break;\n case '5f':\n data.relayRecoveryState = parseInt(commands[i+1], 16);\n i += 1;\n break;\n case '60':\n data.overheatingEvents = {\n events: parseInt(commands[i+1], 16),\n temperature: parseInt(commands[i+2], 16)\n };\n i += 2;\n break;\n case '61':\n data.overvoltageEvents = {\n events: parseInt(commands[i+1], 16),\n voltage: parseInt(commands[i+2] + commands[i+3], 16)\n };\n i += 3;\n break;\n case '62':\n data.overcurrentEvents = {\n events: parseInt(commands[i+1], 16),\n current: parseInt(commands[i+2] + commands[i+3], 16)\n };\n i += 3;\n break;\n case '63':\n data.overpowerEvents = {\n events: parseInt(commands[i+1], 16),\n power: parseInt(commands[i+2] + commands[i+3], 16)\n };\n i += 3;\n break;\n case '70':\n data.overheatingRecoveryTime = parseInt(commands[i+1] + commands[i+2], 16);\n i += 2;\n break;\n case '71':\n data.overvoltageRecoveryTime = parseInt(commands[i+1] + commands[i+2], 16);\n i += 2;\n break;\n case '72':\n data.overcurrentRecoveryTemp = parseInt(commands[i+1], 16);\n i += 1;\n break;\n case '73':\n data.overpowerRecoveryTemp = parseInt(commands[i+1], 16);\n i += 1;\n break;\n case 'b1':\n data.relayState = (parseInt(commands[i+1], 16) === 0x01);\n i += 1;\n break;\n case 'a0':\n var addrRaw = commands[i+1] + commands[i+2] + commands[i+3] + commands[i+4];\n data.fuota = {\n fuota_address: parseInt(addrRaw, 16),\n fuota_address_raw: addrRaw\n };\n i += 4;\n break;\n default:\n break;\n }\n }\n return data;\n}\n\nvar result = {};\n\nif (payload != null && payload.length > 0) {\n result = decodePayload(payload);\n}\n\nreturn result;", "encoder": null, "tbelEncoder": null, "updateOnlyKeys": [ "eui", "fPort", "frequency", "dr" ], "type": "DEVICE", "name": "16ASPM $eui", "profile": "", "label": "", "customer": "", "group": "", "telemetry": null, "attributes": [ "eui", "fPort", "frequency" ] }, "additionalInfo": { "description": "" }, "edgeTemplate": false, "converterVersion": 2 }