Skip to content

Instantly share code, notes, and snippets.

@projetsdiy
Last active July 18, 2019 12:00
Show Gist options
  • Save projetsdiy/fa02078c160cb3e00e09f4980b534490 to your computer and use it in GitHub Desktop.
Save projetsdiy/fa02078c160cb3e00e09f4980b534490 to your computer and use it in GitHub Desktop.
MySensors v2 Message Decoder

#MySensors v2 Message Decoder Decode every MySensors message according to the serial API specifications.

#Create a JSON object payload This flow create a JSON object as payload with the following key:value data :

nodeId : Node Id
sensorId : child Id
mode : presentation, set, req, internal, stream
type : sub-type depending to the message type
typeLabel : human readable label 
value : MySensors payload

Projets DIY - 2016

http://www.projetsdiy.fr

Post (in french) : http://www.projetsdiy.fr/mysensors-decoder-envoyer-messages-node-red/

[{"id":"f4fd4940.32ba8","type":"function","z":"eb4ead14.fd77f","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.payload.toString();\nmessage = message.replace(/(\\r\\n|\\n|\\r)/gm, \"\");\nvar tokens = message.split(\";\")\nif(tokens.length == 6)\n{\n mySensorsMessage.nodeId = parseInt(tokens[0]);\n mySensorsMessage.childSensorId= parseInt(tokens[1]);\n mySensorsMessage.messageType = parseInt(tokens[2]);\n mySensorsMessage.ack = parseInt(tokens[3]);\n mySensorsMessage.subType = parseInt(tokens[4]);\n mySensorsMessage.value = Number(tokens[5]);\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":394.5555419921875,"y":279.5555725097656,"wires":[["22cb3401.ef7e24","2d12c5a2.7304aa","fd30fa09.244ce8"]]}]
@y8s
Copy link

y8s commented Apr 8, 2019

I know this is a few years old, but I decided to try it out anyway. A few comments and updates:

  1. I reconfigured the list of S_ , V_, and I_ types to match version 2.3.1 of mysensors
  2. Since many I_ types are text, the parseInt() function returns NaN, so I removed it from the tokens list and left just mySensorsMessage.value = tokens[5]; and moved the parseInt() to cases 1 and 2 NewPayload.value. Now I get text when it's showing the name or other internal values and integers when I only want a 1/0.
  3. Probably at some point I'll add some ability to determine if a payload is a string, an integer, or a floating value (e.g. for measuremnts). But unfortunately I'm not a programmer so it'll take me a while to implement.

@y8s
Copy link

y8s commented Apr 9, 2019

It's not pretty, but here is a pretty simple update of the file:
https://gist.github.com/y8s/d54bc37ce60a37f53ca77771e14e34a9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment