Plugin file
Plugin configuration file
{
"name": "milesight_iot_ws301",
"version": "1.0.0",
"description": "Milesight WS301 is a door/window sensor that detects opening/closing events for intrusion monitoring. Features 4+ year battery life and works with Milesight LoRaWAN gateway and IoT Cloud for real-time status tracking and mobile alerts.",
"author": "Thinger.io",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/thinger-io/plugins.git",
"directory": "milesight-iot-ws301"
},
"metadata": {
"name": "Milesight-Iot WS301",
"description": "Milesight WS301 is a door/window sensor that detects opening/closing events for intrusion monitoring. Features 4+ year battery life and works with Milesight LoRaWAN gateway and IoT Cloud for real-time status tracking and mobile alerts.",
"image": "assets/ws301.png",
"category": "devices",
"vendor": "milesight-iot"
},
"resources": {
"products": [
{
"description": "Milesight WS301 is a door/window sensor that detects opening/closing events for intrusion monitoring. Features 4+ year battery life and works with Milesight LoRaWAN gateway and IoT Cloud for real-time status tracking and mobile alerts.",
"enabled": true,
"name": "Milesight-Iot WS301",
"product": "milesight_iot_ws301",
"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": "ws301_.*"
},
"enabled": true
}
},
"buckets": {
"milesight_ws301_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\nfunction decodeUplink(input) {\n var res = Decoder(input.bytes, input.fPort);\n if (res.error) {\n return {\n errors: [res.error],\n };\n }\n return {\n data: res,\n };\n}\n/**\n * Payload Decoder for The Things Network\n *\n * Copyright 2021 Milesight IoT\n *\n * @product WS301\n */\nfunction Decoder(bytes, port) {\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 // BATTERY\n if (channel_id === 0x01 && channel_type === 0x75) {\n decoded.battery = bytes[i];\n i += 1;\n }\n // DOOR / WINDOW STATE\n else if (channel_id === 0x03 && channel_type === 0x00) {\n decoded.door = bytes[i] === 0 ? \"close\" : \"open\";\n i += 1;\n }\n //\n else if (channel_id === 0x04 && channel_type === 0x00) {\n decoded.install = bytes[i] === 0 ? \"yes\" : \"no\";\n i += 1;\n } else {\n break;\n }\n }\n\n return decoded;\n}\n\n\nfunction normalizeUplink(input) {\n var data = {};\n\n if (input.data.door && (input.data.door === \"close\" || input.data.door === \"open\")) {\n data.action = {\n contactState: input.data.door === \"close\" ? \"closed\" : \"open\"\n };\n }\n\n if (input.data.battery) {\n data.battery = input.data.battery;\n }\n\n return { data: data };\n}\n",
"environment": "javascript",
"storage": "",
"version": "1.0"
},
"flows": {
"milesight_ws301_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": "WS301",
"widgets": [
{
"layout": {
"col": 0,
"row": 0,
"sizeX": 3,
"sizeY": 6
},
"panel": {
"color": "#ffffff",
"currentColor": "#ffffff",
"showOffline": {
"type": "none"
},
"title": "Battery"
},
"properties": {
"color": "#21c023",
"gradient": false,
"max": 100,
"min": 0,
"unit": "%"
},
"sources": [
{
"bucket": {
"backend": "mongodb",
"id": "milesight_ws301_data",
"mapping": "battery",
"tags": {
"device": [],
"group": []
}
},
"color": "#1abc9c",
"name": "Battery",
"source": "bucket",
"timespan": {
"mode": "latest"
}
}
],
"type": "gauge"
},
{
"layout": {
"col": 3,
"row": 0,
"sizeX": 3,
"sizeY": 6
},
"panel": {
"color": "#ffffff",
"currentColor": "#ffffff",
"showOffline": {
"type": "none"
},
"title": "Door/Window Status"
},
"type": "group_widget",
"widgets": [
{
"layout": {
"col": 0,
"row": 0,
"sizeX": 3,
"sizeY": 5
},
"panel": {
"color": "#ffffff",
"currentColor": "#ffffff",
"showOffline": {
"type": "none"
},
"title": "Explanation"
},
"properties": {
"source": "code",
"template": "<div style=\"width:200px; margin:20px auto; font-family:Arial, sans-serif;\">\n <div style=\"padding:10px; background-color:#2ecc71; color:#fff; text-align:center; margin-bottom:5px; border-radius:5px;\">\n Closed\n </div>\n <div style=\"padding:10px; background-color:#e74c3c; color:#fff; text-align:center; border-radius:5px;\">\n Open\n </div>\n</div>"
},
"sources": [
{
"bucket": {
"backend": "mongodb",
"id": "milesight_ws301_data",
"mapping": "device",
"tags": {
"device": [],
"group": []
}
},
"color": "#1abc9c",
"name": "Source 1",
"source": "bucket",
"timespan": {
"mode": "latest"
}
}
],
"type": "html_time"
},
{
"layout": {
"col": 3,
"row": 0,
"sizeX": 3,
"sizeY": 5
},
"panel": {
"color": "#ffffff",
"currentColor": "#ffffff",
"showOffline": {
"type": "none"
},
"title": "Status"
},
"properties": {
"color": "#4bd763",
"colors": [
{
"blink": false,
"color": "#00ff00",
"max": 0,
"min": 0
},
{
"blink": true,
"color": "#d71d1d",
"max": 1,
"min": 1
}
],
"size": "75px"
},
"sources": [
{
"bucket": {
"backend": "mongodb",
"id": "milesight_ws301_data",
"mapping": "door",
"tags": {
"device": [],
"group": []
}
},
"color": "#1abc9c",
"name": "Door Status",
"processing": {
"input": "doorStatusToCode"
},
"source": "bucket",
"timespan": {
"mode": "latest"
}
}
],
"type": "led"
}
]
},
{
"layout": {
"col": 0,
"row": 6,
"sizeX": 6,
"sizeY": 7
},
"panel": {
"color": "#ffffff",
"currentColor": "#ffffff",
"showOffline": {
"type": "none"
},
"title": "Event History (Last 7 days)"
},
"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>Date</th>\n <th>Battery (%)</th>\n <th>Door/Window Status</th>\n <th>Install Status</th>\n </tr>\n </thead>\n <tbody>\n <tr ng-repeat=\"entry in value\">\n <td>{{ entry.ts | date:'medium' }}</td>\n <td>{{ entry.battery || '—' }}</td>\n <td>{{ entry.door || '—' }}</td>\n <td>{{ entry.install || '—' }}</td>\n </tr>\n </tbody>\n </table>\n</div>"
},
"sources": [
{
"aggregation": {},
"bucket": {
"backend": "mongodb",
"id": "milesight_ws301_data",
"mapping": "ts",
"tags": {
"device": [],
"group": []
}
},
"color": "#1abc9c",
"name": "ts",
"source": "bucket",
"timespan": {
"magnitude": "day",
"mode": "relative",
"period": "latest",
"value": 7
}
},
{
"aggregation": {},
"bucket": {
"backend": "mongodb",
"id": "milesight_ws301_data",
"mapping": "battery",
"tags": {
"device": [],
"group": []
}
},
"color": "#2ecc71",
"name": "battery",
"source": "bucket",
"timespan": {
"magnitude": "day",
"mode": "relative",
"period": "latest",
"value": 7
}
},
{
"aggregation": {},
"bucket": {
"backend": "mongodb",
"id": "milesight_ws301_data",
"mapping": "door",
"tags": {
"device": [],
"group": []
}
},
"color": "#3498db",
"name": "door",
"source": "bucket",
"timespan": {
"magnitude": "day",
"mode": "relative",
"period": "latest",
"value": 7
}
},
{
"aggregation": {},
"bucket": {
"backend": "mongodb",
"id": "milesight_ws301_data",
"mapping": "install",
"tags": {
"device": [],
"group": []
}
},
"color": "#f39c12",
"name": "install",
"source": "bucket",
"timespan": {
"magnitude": "day",
"mode": "relative",
"period": "latest",
"value": 7
}
}
],
"type": "html_time"
}
]
}
]
}
}
]
}
}
]
}
}