Skip to content

Plugin file

Plugin configuration file
{
  "name": "milesight-ct10x",
  "version": "1.0.0",
  "description": "Compact transformer for real-time electrical current monitoring",
  "author": "Thinger.io",
  "license": "MIT",
  "repository": {
    "type": "git",
    "url": "https://github.com/thinger-io/plugins.git",
    "directory": "milesight-ct10x"
  },
  "metadata": {
    "name": "Milesight CT10X",
    "description": "Compact transformer for real-time electrical current monitoring",
    "category": "devices",
    "image": "assets/milesight-sct.png"
  },
  "resources": {
    "products": [
      {
        "description": "",
        "enabled": true,
        "name": "Milesight Smart Current Transformer",
        "product": "milesight_sct",
        "profile": {
          "api": {
            "downlink": {
              "description": "Downlink data exit point to LNS Thinger Plugin",
              "enabled": true,
              "handle_connectivity": false,
              "request": {
                "data": {
                  "path": "/downlink",
                  "payload": "{\n    \"data\"    : \"{{payload.data=\"\"}}\",\n    \"port\"    :  {{payload.port=85}},\n    \"priority\":  {{payload.priority=3}},\n    \"confirmed\" :  {{payload.confirmed=false}},\n    \"uplink\"  :  {{property.uplink}} \n}",
                  "payload_function": "",
                  "payload_type": "",
                  "plugin": "{{property.uplink.source}}",
                  "target": "plugin_endpoint"
                }
              },
              "response": {
                "data": {}
              }
            },
            "uplink": {
              "description": "Uplink data entry point from LNS Thinger Plugin",
              "device_id_resolver": "getId",
              "enabled": true,
              "handle_connectivity": true,
              "request": {
                "data": {
                  "payload": "{{payload}}",
                  "payload_function": "",
                  "payload_type": "source_payload",
                  "target": "resource_stream"
                }
              },
              "response": {
                "data": {}
              }
            }
          },
          "autoprovisions": {
            "device_autoprovisioning": {
              "config": {
                "mode": "pattern",
                "pattern": "milesight_sct_.*"
              },
              "description": "Milesight Smart Current Transformer Product",
              "enabled": true,
              "name": ""
            }
          },
          "buckets": {
            "milesight_sct_alarms": {
              "backend": "mongodb",
              "data": {
                "payload": "{{payload}}",
                "payload_function": "getBucketAlarm",
                "payload_type": "source_payload",
                "resource_stream": "uplink_decoded",
                "source": "resource_stream"
              },
              "enabled": true,
              "retention": {
                "period": 3,
                "unit": "months"
              },
              "tags": []
            },
            "milesight_sct_data": {
              "backend": "mongodb",
              "data": {
                "payload": "{{payload}}",
                "payload_function": "getBucketData",
                "payload_type": "source_payload",
                "resource_stream": "uplink_decoded",
                "source": "resource_stream"
              },
              "enabled": true,
              "retention": {
                "period": 3,
                "unit": "months"
              },
              "tags": []
            }
          },
          "code": {
            "code": "function getFirmwareVersion(payload) {\n    return payload.firmware_version;\n}\n\nfunction getHardwareVersion(payload) {\n    return payload.hardware_version;\n}\n\nfunction getIpsoVersion(payload) {\n    return payload.ipso_version;\n}\n\nfunction getLorawanClass(payload) {\n    return payload.lorawan_class;\n}\n\nfunction getTSLVersion(payload) {\n    return payload.tsl_version;\n}\n\n// Device Identifier Resolver configured in \"uplink\" API resource.\nfunction getId(payload) {\n    return payload.deviceId;\n}\n\n// HELPER: converts hex string to byte array\nfunction hexToByteArray(hexString) {\n  hexString = hexString.replace(/^0x/, '').replace(/\\s+/g, '');\n\n  if (hexString.length % 2 !== 0) {\n    return null;\n  }\n\n  const byteArray = new Uint8Array(hexString.length / 2);\n\n  for (let i = 0; i < hexString.length; i += 2) {\n    byteArray[i / 2] = parseInt(hexString.substr(i, 2), 16);\n  }\n\n  return byteArray;\n}\n\nfunction getBucketAlarm(payload) {\n    // Detectar si hay datos de alarma\n    var hasAlarm = payload.current_over_range_alarm ||\n                   payload.current_over_range_alarm_release ||\n                   payload.current_threshold_alarm ||\n                   payload.current_threshold_alarm_release;\n\n    if (hasAlarm) {\n        return {\n            current: payload.current,\n            total_current: payload.total_current,\n\n            current_over_range_alarm: payload.current_over_range_alarm,\n            current_over_range_alarm_release: payload.current_over_range_alarm_release,\n            current_threshold_alarm: payload.current_threshold_alarm,\n            current_threshold_alarm_release: payload.current_threshold_alarm_release\n        };\n    }\n\n    return null;\n}\n\nfunction getBucketData(payload) {\n\n    if (!payload.total_current) {\n        return null;\n    }\n\n    return payload;\n}\n\n\nfunction decodeUplinkThinger(input) {\n    var decoded = milesightDeviceDecode(hexToByteArray(input.payload));\n    return decoded;\n}\n\n\n/**\n * Payload Decoder\n *\n * Copyright 2025 Milesight IoT\n *\n * @product CT101 / CT103 / CT105\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    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        // TOTAL CURRENT\n        else if (channel_id === 0x03 && channel_type === 0x97) {\n            decoded.total_current = readUInt32LE(bytes.slice(i, i + 4)) / 100;\n            i += 4;\n        }\n        // CURRENT\n        else if (channel_id === 0x04 && channel_type === 0x98) {\n            var current_value = readUInt16LE(bytes.slice(i, i + 2));\n            if (current_value === 0xffff) {\n                decoded.current_sensor_status = readSensorStatus(2);\n            } else {\n                decoded.current = current_value / 100;\n            }\n            i += 2;\n        }\n        // TEMPERATURE\n        else if (channel_id === 0x09 && channel_type === 0x67) {\n            var temperature_value = readUInt16LE(bytes.slice(i, i + 2));\n            if (temperature_value === 0xfffd) {\n                decoded.temperature_sensor_status = readSensorStatus(1);\n            } else if (temperature_value === 0xffff) {\n                decoded.temperature_sensor_status = readSensorStatus(2);\n            } else {\n                decoded.temperature = readInt16LE(bytes.slice(i, i + 2)) / 10;\n            }\n            i += 2;\n        }\n        // CURRENT ALARM\n        else if (channel_id === 0x84 && channel_type === 0x98) {\n            decoded.current_max = readUInt16LE(bytes.slice(i, i + 2)) / 100;\n            decoded.current_min = readUInt16LE(bytes.slice(i + 2, i + 4)) / 100;\n            decoded.current = readUInt16LE(bytes.slice(i + 4, i + 6)) / 100;\n            decoded.current_alarm = readCurrentAlarm(bytes[i + 6]);\n            i += 7;\n        }\n        // TEMPERATURE ALARM\n        else if (channel_id === 0x89 && 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        // 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 {\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.alarm_report_interval = readUInt16LE(bytes.slice(offset, offset + 2));\n            offset += 2;\n            break;\n        case 0x06:\n            var value = readUInt8(bytes[offset]);\n            var channel_value = (value >>> 3) & 0x07;\n            if (channel_value === 0x01) {\n                decoded.current_alarm_config = {};\n                decoded.current_alarm_config.condition = readConditionType(value & 0x07);\n                decoded.current_alarm_config.threshold_min = readUInt16LE(bytes.slice(offset + 1, offset + 3));\n                decoded.current_alarm_config.threshold_max = readUInt16LE(bytes.slice(offset + 3, offset + 5));\n                decoded.current_alarm_config.alarm_interval = readUInt16LE(bytes.slice(offset + 5, offset + 7));\n                decoded.current_alarm_config.alarm_counts = readUInt16LE(bytes.slice(offset + 7, offset + 9));\n            } else if (channel_value === 0x04) {\n                decoded.temperature_alarm_config = {};\n                decoded.temperature_alarm_config.condition = readConditionType(value & 0x07);\n                decoded.temperature_alarm_config.threshold_min = readInt16LE(bytes.slice(offset + 1, offset + 3)) / 10;\n                decoded.temperature_alarm_config.threshold_max = readInt16LE(bytes.slice(offset + 3, offset + 5)) / 10;\n            }\n            offset += 9;\n            break;\n        case 0x10:\n            decoded.reboot = readYesNoStatus(1);\n            offset += 1;\n            break;\n        case 0x27:\n            decoded.clear_current_cumulative = readYesNoStatus(1);\n            offset += 1;\n            break;\n        case 0x28:\n            decoded.report_status = readYesNoStatus(1);\n            offset += 1;\n            break;\n        case 0x8e:\n            // ignore first byte\n            decoded.report_interval = readUInt16LE(bytes.slice(offset + 1, offset + 3));\n            offset += 3;\n            break;\n        case 0xf2:\n            decoded.alarm_report_counts = readUInt16LE(bytes.slice(offset, offset + 2));\n            offset += 2;\n            break;\n        default:\n            throw new Error(\"unknown downlink response\");\n    }\n\n    return { data: decoded, offset: offset };\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 readYesNoStatus(status) {\n    var status_map = { 0: \"no\", 1: \"yes\" };\n    return getValue(status_map, status);\n}\n\nfunction readSensorStatus(status) {\n    var status_map = { 0: \"normal\", 1: \"over range alarm\", 2: \"read failed\" };\n    return getValue(status_map, status);\n}\n\nfunction readCurrentAlarm(value) {\n    var alarm_bit_offset = { current_threshold_alarm: 0, current_threshold_alarm_release: 1, current_over_range_alarm: 2, current_over_range_alarm_release: 3 };\n\n    var event = {};\n    for (var key in alarm_bit_offset) {\n        event[key] = readYesNoStatus((value >> alarm_bit_offset[key]) & 0x01);\n    }\n    return event;\n}\n\nfunction readTemperatureAlarm(type) {\n    var alarm_map = { 0: \"temperature threshold alarm release\", 1: \"temperature threshold alarm\" };\n    return getValue(alarm_map, type);\n}\n\nfunction readConditionType(type) {\n    var condition_map = { 0: \"disable\", 1: \"below\", 2: \"above\", 3: \"between\", 4: \"outside\" };\n    return getValue(condition_map, type);\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\nfunction readFloatLE(bytes) {\n    var bits = (bytes[3] << 24) | (bytes[2] << 16) | (bytes[1] << 8) | bytes[0];\n    var sign = bits >>> 31 === 0 ? 1.0 : -1.0;\n    var e = (bits >>> 23) & 0xff;\n    var m = e === 0 ? (bits & 0x7fffff) << 1 : (bits & 0x7fffff) | 0x800000;\n    var f = sign * m * Math.pow(2, e - 150);\n    return f;\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_sct_decoder": {
              "data": {
                "payload": "{{payload}}",
                "payload_function": "decodeUplinkThinger",
                "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": {
            "milesight_sct_battery": {
              "data": {
                "payload": "{{payload.metadata.battery}}",
                "payload_function": "",
                "payload_type": "",
                "resource_stream": "uplink",
                "source": "resource_stream"
              },
              "default": {
                "source": "value"
              },
              "enabled": true
            },
            "milesight_sct_firmware_version": {
              "data": {
                "payload": "{{payload.firmware_version.bytes.toString()}}",
                "payload_function": "getFirmwareVersion",
                "payload_type": "source_payload",
                "resource_stream": "uplink_decoded",
                "source": "resource_stream"
              },
              "default": {
                "source": "value"
              },
              "enabled": true
            },
            "milesight_sct_hardware_version": {
              "data": {
                "payload": "{{payload.hardware_version.toString()}}",
                "payload_function": "getHardwareVersion",
                "payload_type": "source_payload",
                "resource_stream": "uplink_decoded",
                "source": "resource_stream"
              },
              "default": {
                "source": "value"
              },
              "enabled": true
            },
            "milesight_sct_ipso_version": {
              "data": {
                "patch": false,
                "payload": "{{payload.ipso_version}}",
                "payload_function": "getIpsoVersion",
                "payload_type": "source_event",
                "resource_stream": "uplink_decoded",
                "source": "resource_stream"
              },
              "default": {
                "source": "value"
              },
              "enabled": true
            },
            "milesight_sct_lorawan_class": {
              "data": {
                "payload": "{{payload.lorawan_class}}",
                "payload_function": "getLorawanClass",
                "payload_type": "source_event",
                "resource_stream": "uplink_decoded",
                "source": "resource_stream"
              },
              "default": {
                "source": "value"
              },
              "enabled": true
            },
            "milesight_sct_tsl_version": {
              "data": {
                "payload": "{{payload.tsl_version}}",
                "payload_function": "getTSLVersion",
                "payload_type": "source_event",
                "resource_stream": "uplink_decoded",
                "source": "resource_stream"
              },
              "default": {
                "source": "value"
              },
              "enabled": true
            },
            "uplink": {
              "data": {
                "payload": "{{payload}}",
                "payload_function": "",
                "payload_type": "source_payload",
                "resource": "uplink",
                "source": "resource",
                "update": "events"
              },
              "default": {
                "source": "value"
              },
              "description": "Last raw uplink recieved",
              "enabled": true
            }
          }
        },
        "_resources": {
          "properties": [
            {
              "property": "dashboard",
              "value": {
                "functions": "function ah_to_KWh(value, ts, series){\n   \n   value=value*220/1000;\n    return value;\n}\n\n",
                "name": "Milesight SCT",
                "placeholders": {
                  "sources": []
                },
                "properties": {
                  "template": true
                },
                "tabs": [
                  {
                    "icon": "fas fa-tachometer-alt",
                    "widgets": [
                      {
                        "layout": {
                          "col": 4,
                          "row": 0,
                          "sizeX": 1,
                          "sizeY": 8
                        },
                        "panel": {
                          "color": "#ffffff",
                          "currentColor": "#ffffff",
                          "showOffline": {
                            "type": "none"
                          },
                          "title": "Power"
                        },
                        "properties": {
                          "color": "#52A6F0",
                          "max": 25000,
                          "min": 0,
                          "unit": "KWh"
                        },
                        "sources": [
                          {
                            "bucket": {
                              "backend": "mongodb",
                              "id": "milesight_sct_data",
                              "tags": {
                                "device": [
                                  "milesight_sct_24E124746E227069"
                                ],
                                "group": []
                              }
                            },
                            "color": "#1abc9c",
                            "device_bucket": {
                              "backend": "mongodb",
                              "device": "milesight_sct_24E124746E227069",
                              "id": "milesight_sct_data",
                              "mapping": "current",
                              "tags": {
                                "device": [],
                                "group": []
                              }
                            },
                            "name": "Source 1",
                            "processing": {
                              "input": "ah_to_KWh"
                            },
                            "source": "device_bucket",
                            "timespan": {
                              "mode": "latest"
                            }
                          }
                        ],
                        "type": "donutchart"
                      },
                      {
                        "layout": {
                          "col": 3,
                          "row": 0,
                          "sizeX": 1,
                          "sizeY": 8
                        },
                        "panel": {
                          "color": "#ffffff",
                          "currentColor": "#ffffff",
                          "showOffline": {
                            "type": "none"
                          },
                          "title": "Current"
                        },
                        "properties": {
                          "color": "#52A6F0",
                          "max": 100,
                          "min": 0,
                          "unit": "Amps"
                        },
                        "sources": [
                          {
                            "bucket": {
                              "backend": "mongodb",
                              "id": "milesight_sct_data",
                              "tags": {
                                "device": [
                                  "milesight_sct_24E124746E227069"
                                ],
                                "group": []
                              }
                            },
                            "color": "#1abc9c",
                            "device_bucket": {
                              "backend": "mongodb",
                              "device": "milesight_sct_24E124746E227069",
                              "id": "milesight_sct_data",
                              "mapping": "current",
                              "tags": {
                                "device": [],
                                "group": []
                              }
                            },
                            "name": "Source 1",
                            "source": "device_bucket",
                            "timespan": {
                              "mode": "latest"
                            }
                          }
                        ],
                        "type": "donutchart"
                      },
                      {
                        "layout": {
                          "col": 5,
                          "row": 0,
                          "sizeX": 1,
                          "sizeY": 8
                        },
                        "panel": {
                          "color": "#ffffff",
                          "currentColor": "#ffffff",
                          "showOffline": {
                            "type": "none"
                          }
                        },
                        "properties": {
                          "refresh_interval": 0
                        },
                        "sources": [
                          {
                            "color": "#1abc9c",
                            "image_url": "https://www.milesight.com/static/pc/en/iot/product/lorawan-sensor/ct10x/non-invasive.mp4?t=1763543151888",
                            "mjpeg_url": "https://www.orbitadigital.com/365794-large_default/milesight-ms-ct101-868m-medidor-ct-iot-lorawan-permite-medir-hasta-100a.jpg",
                            "name": "Source 1",
                            "source": "mjpeg_url"
                          }
                        ],
                        "type": "image"
                      }
                    ]
                  }
                ]
              }
            }
          ]
        }
      }
    ]
  }
}