Skip to content

Instantly share code, notes, and snippets.

@THWillert
Last active March 12, 2021 07:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save THWillert/87fde671978032f32d542d19984b8c8b to your computer and use it in GitHub Desktop.
Save THWillert/87fde671978032f32d542d19984b8c8b to your computer and use it in GitHub Desktop.
Function node for easy debugging.

Debug

Function node for easy debugging.

Simply insert it into a connection:

screenshot

The following is displayed as the status text:

  • "Data-type" as color / shape:
    • Boolean: Red (dot)
    • String: Blue (dot)
    • JSON: Blue (ring)
    • Number: Yellow (dot)
    • BigInt: Yellow (ring)
    • Array: Green (ring)
    • Object: Green (dot)
    • Everything else: Grey (dot)
  • Date and time of the last message
  • Topic (msg.topic)
  • Data-type
  • Payload (msg.payload) limited to 50 chars

Homepage

[
{
"id": "a19dfe55.edac48",
"type": "function",
"z": "1e47caf.81f0ab5",
"name": "Debug",
"func": "// Debug V2.0, by Thorsten Willert\n// added: BigInt, JSON and Array (+ length) as \"data-types\".\n// changed: limited message-display to 50 chars.\n// changed: removed \"debug\" topic and added msg.topic to the node.status\n\nconst dtNow = new Date().toLocaleString();\n\nlet Type = typeof(msg.payload);\n\nlet sMsg = msg.payload;\n\nif (msg.payload.isArray) {\n Type = 'array';\n sMsg += ' (' + msg.payload.length + ')';\n}\n\nif (Type == \"string\") sMsg = msg.payload.substring(0, 50) + ' ...'; \n\n\nif (Type == 'string' && IsJsonString(msg.payload) ) {\n Type = 'JSON';\n}\n\nlet sColor = typeToColor(Type);\nlet sShape = typeToShape(Type);\nlet Topic = '';\n\nif (msg.topic !== '') {\n Topic = msg.topic + \" | \";\n}\n\nnode.status({\n fill: sColor ,\n shape: sShape,\n text: dtNow + \" | \" + Topic + Type + \" | \" + sMsg\n});\n\nreturn msg;\n\n// -------------------------------------------------------\n\nfunction typeToColor(value) {\n switch(value){\n case \"boolean\":\n return \"red\";\n case \"string\":\n case \"JSON\":\n return \"blue\";\n case \"number\":\n return \"yellow\";\n case \"object\":\n case \"array\":\n return \"green\";\n default:\n return \"grey\";\n }\n}\n\nfunction typeToShape(value) {\n switch(value){\n case \"boolean\":\n case \"string\":\n case \"number\":\n case \"object\":\n return \"dot\";\n case \"bigint\":\n case \"JSON\":\n case \"array\":\n return \"ring\";\n default:\n return \"dot\";\n }\n}\n\nfunction IsJsonString(str) {\n try {\n JSON.parse(str);\n } catch (e) {\n return false;\n }\n return true;\n}",
"outputs": 1,
"noerr": 0,
"x": 810,
"y": 1440,
"wires": [
[
"b3f4bd23.0ce8f8"
]
],
"inputLabels": [
"msg.payload"
],
"outputLabels": [
"msg.payload"
],
"info": "# Debug\n\nFunction node for easy debugging.\n\nSimply insert it into a connection:\n\n![screenshot](http://www.thorsten-willert.de//images/Software/Node-RED/Thorsten_H_Willert_-_Node-RED_Debug.png)\n\nThe following is displayed as the status text:\n\n- \"Data-type\" as color / shape:\n - Boolean: Red (dot)\n - String: Blue (dot)\n - JSON: Blue (ring)\n - Number: Yellow (dot)\n - BigInt: Yellow (ring)\n - Array: Green (ring)\n - Object: Green (dot)\n - Everything else: Grey (dot)\n- Date and time of the last message\n- Topic (msg.topic)\n- Data-type\n- Payload (msg.payload) limited to 50 chars"
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment