Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Node-Red MySensors MQTT to human-readable MQTT flow
[{"id":"49f44e88.60375","type":"debug","z":"686096e4.b7f728","name":"From MySensors","active":false,"console":"false","complete":"payload","x":610,"y":140,"wires":[]},{"id":"e04d7263.f39a2","type":"function","z":"686096e4.b7f728","name":"Decode MySensor Message","func":"/* MySensors v2 Message Decoder\n* Payload : JSON object\n* www.projetsdiy.fr - oct. 2016\n*/\nvar mySensorsMessage = {}\nvar newPayload = {};\nvar message = msg.topic.toString();\nmessage = message.replace(/(\\r\\n|\\n|\\r)/gm, \"\");\nvar tokens = message.split(\"/\")\nif(tokens.length == 6)\n{\n mySensorsMessage.nodeId = parseInt(tokens[1]);\n mySensorsMessage.childSensorId= parseInt(tokens[2]);\n mySensorsMessage.messageType = parseInt(tokens[3]);\n mySensorsMessage.ack = parseInt(tokens[4]);\n mySensorsMessage.subType = parseInt(tokens[5]);\n mySensorsMessage.value = Number(msg.payload);\n\n var messageType = mySensorsMessage.messageType;\n var subType = mySensorsMessage.subType;\n var labelPresentation = [\"S_DOOR\",\"S_MOTION\",\"S_SMOKE\",\"S_LIGHT\",\"S_BINARY\",\"S_DIMMER\",\"S_COVER\",\"S_TEMP\",\"S_HUM\",\"S_BARO\",\"S_WIND\",\"S_RAIN\",\"S_UV\",\"S_WEIGHT\",\"S_POWER\",\"S_HEATER\",\"S_DISTANCE\",\"S_LIGHT_LEVEL\",\"S_ARDUINO_NODE\",\"S_ARDUINO_REPEATER_NODE\",\"S_LOCK\",\"S_IR\",\"S_WATER\",\"S_AIR_QUALITY\",\"S_CUSTOM\",\"S_DUST\",\"S_SCENE_CONTROLLER\",\"S_RGB_LIGHT\",\"S_RGBW_LIGHT\",\"S_COLOR_SENSOR\",\"S_HVAC\",\"S_MULTIMETER\",\"S_SPRINKLER\",\"S_WATER_LEAK\",\"S_SOUND\",\"S_VIBRATION\",\"S_MOISTURE\",\"S_INFO\",\"S_GAS\",\"S_GPS\",\"S_WATER_QUALITY\"];\n var labelSet = [\"V_TEMP\",\"V_HUM\",\"V_STATUS\",\"V_LIGHT\",\"V_PERCENTAGE\",\"V_DIMMER\",\"V_PRESSURE\",\"V_FORECAST\",\"V_RAIN\",\"V_RAINRATE\",\"V_WIND\",\"V_GUST\",\"V_DIRECTION\",\"V_UV\",\"V_WEIGHT\",\"V_DISTANCE\",\"V_IMPEDANCE\",\"V_ARMED\",\"V_TRIPPED\",\"V_WATT\",\"V_KWH\",\"V_SCENE_ON\",\"V_SCENE_OFF\",\"V_HVAC_FLOW_STATE\",\"V_HVAC_SPEED\",\"V_LIGHT_LEVEL\",\"V_VAR1\",\"V_VAR2\",\"V_VAR3\",\"V_VAR4\",\"V_VAR5\",\"V_UP\",\"V_DOWN\",\"V_STOP\",\"V_IR_SEND\",\"V_IR_RECEIVE\",\"V_FLOW\",\"V_VOLUME\",\"V_LOCK_STATUS\",\"V_LEVEL\",\"V_VOLTAGE\",\"V_CURRENT\",\"V_RGB\",\"V_RGBW\",\"V_ID\",\"V_UNIT_PREFIX\",\"V_HVAC_SETPOINT_COOL\",\"V_HVAC_SETPOINT_HEAT\",\"V_HVAC_FLOW_MODE\",\"V_TEXT\",\"V_CUSTOM\",\"V_POSITION\",\"V_IR_RECORD\",\"V_PH\",\"V_ORP\",\"V_EC\",\"V_VAR\",\"V_VA\",\"V_POWER_FACTOR\"]\n var labelInternal = [\"I_BATTERY_LEVEL\",\"I_TIME\",\"I_VERSION\",\"I_ID_REQUEST\",\"I_ID_RESPONSE\",\"I_INCLUSION_MODE\",\"I_CONFIG\",\"I_FIND_PARENT\",\"I_FIND_PARENT_RESPONSE\",\"I_LOG_MESSAGE\",\"I_CHILDREN\",\"I_SKETCH_NAME\",\"I_SKETCH_VERSION\",\"I_REBOOT\",\"I_GATEWAY_READY\",\"I_REQUEST_SIGNING\",\"I_GET_NONCE\",\"I_GET_NONCE_RESPONSE\",\"I_HEARTBEAT\",\"I_PRESENTATION\",\"I_DISCOVER\",\"I_DISCOVER_RESPONSE\",\"I_HEARTBEAT_RESPONSE\",\"I_LOCKED\",\"I_PING\",\"I_PONG\",\"I_REGISTRATION_REQUEST\",\"I_REGISTRATION_RESPONSE\",\"I_DEBUG\"]\n \n switch (messageType) {\n case 0: // Presentation\n \n newPayload.mode = \"Presentation\";\n newPayload.type = labelPresentation[subType];\n break;\n case 1: // Set\n newPayload.nodeId= mySensorsMessage.nodeId;\n newPayload.sensorId= mySensorsMessage.childSensorId;\n newPayload.mode= \"Set\";\n newPayload.type= subType;\n newPayload.typeLabel= labelSet[subType];\n newPayload.value= mySensorsMessage.value;\n break;\n case 2: // Req\n newPayload.nodeId= mySensorsMessage.nodeId;\n newPayload.sensorId= mySensorsMessage.childSensorId;\n newPayload.mode= \"Req\";\n newPayload.type= subType;\n newPayload.typeLabel= labelSet[subType];\n newPayload.value= mySensorsMessage.value;\n break; \n case 3: // Internal\n newPayload.nodeId= mySensorsMessage.nodeId;\n newPayload.sensorId= mySensorsMessage.childSensorId;\n newPayload.mode= \"Internal\";\n newPayload.type= subType;\n newPayload.typeLabel= labelInternal[subType];\n newPayload.value= mySensorsMessage.value;\n break; \n case 4: // Stream - OTA firmware update\n newPayload.nodeId= mySensorsMessage.nodeId;\n newPayload.mode= \"stream\";\n break;\n default:\n break;\n }\n\n msg.payload = newPayload; \n} else {\n msg.payload = \"Error! Nothing to decode\"\n} \n\nreturn msg;","outputs":1,"noerr":0,"x":340,"y":200,"wires":[["49f44e88.60375","49876dec.2cc704"]]},{"id":"49876dec.2cc704","type":"switch","z":"686096e4.b7f728","name":"nodeID","property":"payload.nodeId","propertyType":"msg","rules":[{"t":"eq","v":"2","vt":"num"}],"checkall":"true","outputs":1,"x":240,"y":320,"wires":[["8639933.26e7ff"]]},{"id":"8639933.26e7ff","type":"switch","z":"686096e4.b7f728","name":"Node 1 sensorID","property":"payload.sensorId","propertyType":"msg","rules":[{"t":"eq","v":"255","vt":"str"},{"t":"eq","v":"1","vt":"str"},{"t":"eq","v":"2","vt":"str"},{"t":"eq","v":"3","vt":"str"}],"checkall":"true","outputs":4,"x":590,"y":320,"wires":[["53032327.ddaa3c"],["192f02da.9f27cd"],["aa530e22.3589c8"],["f7886541.d08008"]],"outputLabels":["Battery Level / 15","Switch 1","Switch 2","Switch 3"]},{"id":"53032327.ddaa3c","type":"function","z":"686096e4.b7f728","name":"Battery Percentage","func":"var max_mV = 3200;\nvar min_mV = 1800;\nvar perc_low = 50;\n\nvar mV = Number(msg.payload.value) * 15;\nvar perc = 0.0;\nvar low = false;\n\nif (mV < min_mV) {\n perc = 0;\n} else if (mV > max_mV) {\n perc = 100;\n} else {\n perc = 100 * (mV - min_mV)/(max_mV - min_mV);\n}\n\nif (perc < perc_low) {\n low = true;\n}\n\nreturn [{payload: perc.toFixed(1)},\n {payload: low}];","outputs":"2","noerr":0,"x":850,"y":260,"wires":[["3e2169d5.4904b6","bc327866.2f09a"],[]]},{"id":"3e2169d5.4904b6","type":"debug","z":"686096e4.b7f728","name":"Battery Level","active":false,"console":"false","complete":"payload","x":1090,"y":200,"wires":[]},{"id":"1cc6e364.a0c45d","type":"function","z":"686096e4.b7f728","name":"Toggle","func":"msg.payload = \"TOGGLE\";\nreturn msg;","outputs":1,"noerr":0,"x":950,"y":340,"wires":[["32fd8766.0c78a"]]},{"id":"bdc3de58.5fccb8","type":"mqtt out","z":"686096e4.b7f728","name":"light.Patio","topic":"cmnd/patio/led/POWER","qos":"1","retain":"","broker":"2ebfc4bc.a7cbbc","x":1080,"y":380,"wires":[]},{"id":"32fd8766.0c78a","type":"mqtt out","z":"686096e4.b7f728","name":"light.Top_Garden","topic":"cmnd/tvroom/lights/POWER4","qos":"1","retain":"","broker":"2ebfc4bc.a7cbbc","x":1110,"y":340,"wires":[]},{"id":"245dd3b0.4e3074","type":"mqtt out","z":"686096e4.b7f728","name":"light.Dining_Room","topic":"cmnd/kitchen/lights/POWER4","qos":"1","retain":"","broker":"2ebfc4bc.a7cbbc","x":1110,"y":300,"wires":[]},{"id":"7a32465b.f3fd8","type":"function","z":"686096e4.b7f728","name":"Toggle","func":"msg.payload = \"TOGGLE\";\nreturn msg;","outputs":1,"noerr":0,"x":950,"y":300,"wires":[["245dd3b0.4e3074"]]},{"id":"6ef2c2f0.ef256c","type":"function","z":"686096e4.b7f728","name":"Toggle","func":"msg.payload = \"TOGGLE\";\nreturn msg;","outputs":1,"noerr":0,"x":950,"y":380,"wires":[["bdc3de58.5fccb8"]]},{"id":"bc327866.2f09a","type":"mqtt out","z":"686096e4.b7f728","name":"switch.Patio","topic":"tele/patio/switch/batt","qos":"1","retain":"","broker":"2ebfc4bc.a7cbbc","x":1090,"y":240,"wires":[]},{"id":"f7886541.d08008","type":"delay","z":"686096e4.b7f728","name":"3 msg/s","pauseType":"rate","timeout":"100","timeoutUnits":"milliseconds","rate":"3","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":true,"x":820,"y":380,"wires":[["6ef2c2f0.ef256c"]]},{"id":"aa530e22.3589c8","type":"delay","z":"686096e4.b7f728","name":"3 msg/s","pauseType":"rate","timeout":"100","timeoutUnits":"milliseconds","rate":"3","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":true,"x":820,"y":340,"wires":[["1cc6e364.a0c45d"]]},{"id":"192f02da.9f27cd","type":"delay","z":"686096e4.b7f728","name":"3 msg/s","pauseType":"rate","timeout":"100","timeoutUnits":"milliseconds","rate":"3","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":true,"x":820,"y":300,"wires":[["7a32465b.f3fd8"]]},{"id":"7ae05dff.fd40c4","type":"mqtt in","z":"686096e4.b7f728","name":"","topic":"mysensors-out/#","qos":"1","broker":"2ebfc4bc.a7cbbc","x":120,"y":200,"wires":[["e04d7263.f39a2"]]},{"id":"d8ab7664.0c4048","type":"comment","z":"686096e4.b7f728","name":"Disable OverlayFS to save changes...","info":"","x":190,"y":60,"wires":[]},{"id":"2ebfc4bc.a7cbbc","type":"mqtt-broker","z":"","name":"","broker":"127.0.0.1","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"willTopic":"","willQos":"0","willPayload":"","birthTopic":"","birthQos":"0","birthPayload":""}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment