[{"id":"a3ec21cf3fdf3b6c","type":"switch","z":"8c51d1a367a55d26","name":"ncd-filter-mode","property":"topic","propertyType":"msg","rules":[{"t":"eq","v":"sensor_mode","vt":"str"},{"t":"eq","v":"sensor_data","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":540,"y":480,"wires":[["30478f96ba8ca3c5"],["14f98bc3ae61d970"]]},{"id":"f11a633009861adc","type":"change","z":"8c51d1a367a55d26","name":"ncd-add-battery-to-sensor-data","rules":[{"t":"set","p":"payload.sensor_data.battery_percent","pt":"msg","to":"payload.battery_percent","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1010,"y":520,"wires":[["838b254974c309ec"]]},{"id":"14f98bc3ae61d970","type":"switch","z":"8c51d1a367a55d26","name":"ncd-filter-by-type","property":"payload.sensor_type","propertyType":"msg","rules":[{"t":"eq","v":"80","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":750,"y":500,"wires":[["2909a494bed6a4ce","f11a633009861adc"]]},{"id":"aa32152a83421949","type":"mqtt out","z":"8c51d1a367a55d26","name":"ncd-to-thingsboard","topic":"","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"781cea0d7c2535bc","x":1930,"y":480,"wires":[]},{"id":"30478f96ba8ca3c5","type":"switch","z":"8c51d1a367a55d26","name":"ncd-filter-by-run-mode","property":"payload.mode","propertyType":"msg","rules":[{"t":"eq","v":"RUN","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":760,"y":440,"wires":[["5adc76026c7603c4"]]},{"id":"5adc76026c7603c4","type":"change","z":"8c51d1a367a55d26","name":"ncd-set-device","rules":[{"t":"set","p":"mac","pt":"msg","to":"$substring(payload.mac,12)","tot":"jsonata"},{"t":"delete","p":"payload","pt":"msg"},{"t":"set","p":"payload.device","pt":"msg","to":"mac","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":960,"y":440,"wires":[["2321ade5b32f2522"]]},{"id":"2909a494bed6a4ce","type":"function","z":"8c51d1a367a55d26","name":"ncd-attributes","func":"// Retrieve the 'flag' from the context or default to false\nconst flag = context.get(\"flag\") || false;\n\nif (!flag) {\n    // Destructure properties from msg.payload for clarity\n    const {\n        addr,\n        sensor_name: sensorName,\n        firmware,\n        nodeId,\n        sensor_type: sensorType\n    } = msg.payload;\n\n    // Extract the device ID from the address\n    const device = addr.substring(12);\n\n    msg.payload = {\n        [device]: {\n            \"Sensor Name\": sensorName,\n            \"Firmware\": firmware,\n            \"Node ID\": nodeId,\n            \"Sensor Type\": sensorType\n        }\n    };\n\n    // Set the 'flag' in the context to avoid re-execution\n    context.set(\"flag\", true);\n    return msg;\n}\n","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":960,"y":480,"wires":[["b5b0567fee98a327"]]},{"id":"838b254974c309ec","type":"function","z":"8c51d1a367a55d26","name":"ncd-telemetry","func":"// Destructure relevant properties from msg.payload\nconst { addr, received: timestamp } = msg.payload;\n\n// Extract the device ID from the address\nconst device = addr.substring(12);\n\n// Extract the data from the message\nconst sensor_data = msg.payload.sensor_data;\n\n// Construct the payload in a clean and structured way\nmsg.payload = {\n    [device]: [\n        {\n            ts: timestamp,\n            values: sensor_data\n        }\n    ]\n};\n\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1240,"y":520,"wires":[["dcd99e157f0fe049"]]},{"id":"2321ade5b32f2522","type":"change","z":"8c51d1a367a55d26","name":"ncd-set-mqtt-topic-connect","rules":[{"t":"set","p":"topic","pt":"msg","to":"v1/gateway/connect","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1540,"y":440,"wires":[["bbbb190e1dbc8af9"]]},{"id":"b5b0567fee98a327","type":"change","z":"8c51d1a367a55d26","name":"ncd-set-mqtt-topic-attributes","rules":[{"t":"set","p":"topic","pt":"msg","to":"v1/gateway/attributes","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1540,"y":480,"wires":[["bbbb190e1dbc8af9"]]},{"id":"dcd99e157f0fe049","type":"change","z":"8c51d1a367a55d26","name":"ncd-set-mqtt-topic-telemetry","rules":[{"t":"set","p":"topic","pt":"msg","to":"v1/gateway/telemetry","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1540,"y":520,"wires":[["bbbb190e1dbc8af9"]]},{"id":"03a01205f4f68483","type":"debug","z":"8c51d1a367a55d26","name":"debug thingsboard","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1930,"y":440,"wires":[]},{"id":"bbbb190e1dbc8af9","type":"junction","z":"8c51d1a367a55d26","x":1740,"y":480,"wires":[["03a01205f4f68483","aa32152a83421949"]]},{"id":"781cea0d7c2535bc","type":"mqtt-broker","name":"","broker":"mqtt.thingsboard.cloud","port":"1883","clientid":"","autoConnect":true,"usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"autoUnsubscribe":true,"birthTopic":"","birthQos":"0","birthRetain":"false","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closeRetain":"false","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willRetain":"false","willPayload":"","willMsg":{},"userProps":"","sessionExpiry":""}]
