Skip to content

Instantly share code, notes, and snippets.

@mkosmo
Last active August 1, 2023 01:49
Show Gist options
  • Save mkosmo/878a0a3205392231d8cb267d34ace36c to your computer and use it in GitHub Desktop.
Save mkosmo/878a0a3205392231d8cb267d34ace36c to your computer and use it in GitHub Desktop.
Node-RED Flow - BambuLab X1 MQTT Relay for Home Assistant MQTT Auto-Discovery
[
{
"disabled": false,
"env": [
],
"id": "fbda6ab16491b918",
"info": "",
"label": "BambuLab MQTT",
"type": "tab"
},
{
"broker": "ee08bb6102171c28",
"datatype": "json",
"id": "b1c1923cb89b667b",
"inputs": 0,
"name": "3dp-x1c-001",
"nl": false,
"qos": "2",
"rap": true,
"rh": 0,
"topic": "device/00M00A2B0801888/report",
"type": "mqtt in",
"wires": [
[
"69b4c2a7d4558125"
]
],
"x": 130,
"y": 320,
"z": "fbda6ab16491b918"
},
{
"addname": "topic",
"arraySplt": 1,
"arraySpltType": "len",
"id": "6638a055d6e708b6",
"name": "",
"splt": "\\n",
"spltType": "str",
"stream": false,
"type": "split",
"wires": [
[
"44fb18c0174cc237"
]
],
"x": 310,
"y": 380,
"z": "fbda6ab16491b918"
},
{
"finalize": "",
"func": "msg.machine_name = \"3dp-x1c-001\";\nreturn msg;",
"id": "7c5bb5efa50cfa5b",
"initialize": "",
"libs": [
],
"name": "Inject Machine Name",
"noerr": 0,
"outputs": 1,
"type": "function",
"wires": [
[
"6638a055d6e708b6"
]
],
"x": 360,
"y": 320,
"z": "fbda6ab16491b918"
},
{
"action": "obj",
"id": "69b4c2a7d4558125",
"name": "",
"pretty": false,
"property": "payload.print",
"type": "json",
"wires": [
[
"7c5bb5efa50cfa5b"
]
],
"x": 310,
"y": 260,
"z": "fbda6ab16491b918"
},
{
"checkall": "true",
"id": "44fb18c0174cc237",
"name": "",
"outputs": 2,
"property": "topic",
"propertyType": "msg",
"repair": false,
"rules": [
{
"t": "eq",
"v": "mc_print",
"vt": "str"
},
{
"t": "eq",
"v": "print",
"vt": "str"
}
],
"type": "switch",
"wires": [
[
],
[
"8c0408168c2f814b"
]
],
"x": 310,
"y": 440,
"z": "fbda6ab16491b918"
},
{
"broker": "489094618c340eef",
"contentType": "",
"correl": "",
"expiry": "",
"id": "2ac19671d3d98fac",
"name": "Home Assistant",
"qos": "",
"respTopic": "",
"retain": "",
"topic": "",
"type": "mqtt out",
"userProps": "",
"wires": [
],
"x": 1420,
"y": 260,
"z": "fbda6ab16491b918"
},
{
"addname": "topic",
"arraySplt": 1,
"arraySpltType": "len",
"id": "8c0408168c2f814b",
"name": "",
"splt": "\\n",
"spltType": "str",
"stream": false,
"type": "split",
"wires": [
[
"2b35953bf411865b"
]
],
"x": 470,
"y": 440,
"z": "fbda6ab16491b918"
},
{
"checkall": "true",
"id": "2b35953bf411865b",
"name": "",
"outputs": 8,
"property": "topic",
"propertyType": "msg",
"repair": false,
"rules": [
{
"t": "eq",
"v": "upgrade_state",
"vt": "str"
},
{
"t": "eq",
"v": "upload",
"vt": "str"
},
{
"t": "eq",
"v": "xcam",
"vt": "str"
},
{
"t": "eq",
"v": "ipcam",
"vt": "str"
},
{
"t": "eq",
"v": "lights_report",
"vt": "str"
},
{
"t": "eq",
"v": "ams",
"vt": "str"
},
{
"t": "eq",
"v": "gcode_start_time",
"vt": "str"
},
{
"t": "else"
}
],
"type": "switch",
"wires": [
[
"177d66237f5a270b"
],
[
"179539d12a6e4620"
],
[
"1cfc4c6db8427d1c"
],
[
"809b0ed827f26077"
],
[
"a44364902f9c609d"
],
[
"dc745edbd075f93c"
],
[
"ce69e5023a185b3b"
],
[
"c07c594a0c2e129f",
"8881f43c7a7a51d2"
]
],
"x": 630,
"y": 440,
"z": "fbda6ab16491b918"
},
{
"addname": "topic",
"arraySplt": 1,
"arraySpltType": "len",
"id": "177d66237f5a270b",
"name": "",
"splt": "\\n",
"spltType": "str",
"stream": false,
"type": "split",
"wires": [
[
"884d9cc17803a687"
]
],
"x": 870,
"y": 400,
"z": "fbda6ab16491b918"
},
{
"finalize": "",
"func": "msg.topic = \"upgrade_state_\" + msg.topic;\nreturn msg;",
"id": "884d9cc17803a687",
"initialize": "",
"libs": [
],
"name": "upgrade_state",
"noerr": 0,
"outputs": 1,
"type": "function",
"wires": [
[
"c07c594a0c2e129f",
"8881f43c7a7a51d2"
]
],
"x": 1060,
"y": 400,
"z": "fbda6ab16491b918"
},
{
"addname": "topic",
"arraySplt": 1,
"arraySpltType": "len",
"id": "179539d12a6e4620",
"name": "",
"splt": "\\n",
"spltType": "str",
"stream": false,
"type": "split",
"wires": [
[
"6efd1164988e5d54"
]
],
"x": 870,
"y": 440,
"z": "fbda6ab16491b918"
},
{
"finalize": "",
"func": "msg.topic = \"upload_\" + msg.topic;\nreturn msg;",
"id": "6efd1164988e5d54",
"initialize": "",
"libs": [
],
"name": "upload",
"noerr": 0,
"outputs": 1,
"type": "function",
"wires": [
[
"c07c594a0c2e129f",
"8881f43c7a7a51d2"
]
],
"x": 1090,
"y": 440,
"z": "fbda6ab16491b918"
},
{
"addname": "topic",
"arraySplt": 1,
"arraySpltType": "len",
"id": "1cfc4c6db8427d1c",
"name": "",
"splt": "\\n",
"spltType": "str",
"stream": false,
"type": "split",
"wires": [
[
"bbe702116839ce33"
]
],
"x": 870,
"y": 480,
"z": "fbda6ab16491b918"
},
{
"addname": "topic",
"arraySplt": 1,
"arraySpltType": "len",
"id": "dc745edbd075f93c",
"name": "",
"splt": "\\n",
"spltType": "str",
"stream": false,
"type": "split",
"wires": [
[
"1ba24409c2968b9b"
]
],
"x": 870,
"y": 600,
"z": "fbda6ab16491b918"
},
{
"addname": "topic",
"arraySplt": 1,
"arraySpltType": "len",
"id": "809b0ed827f26077",
"name": "",
"splt": "\\n",
"spltType": "str",
"stream": false,
"type": "split",
"wires": [
[
"6d436f1ee46dfcfb"
]
],
"x": 870,
"y": 520,
"z": "fbda6ab16491b918"
},
{
"addname": "topic",
"arraySplt": 1,
"arraySpltType": "len",
"id": "a44364902f9c609d",
"name": "",
"splt": "\\n",
"spltType": "str",
"stream": false,
"type": "split",
"wires": [
[
"857cbcb12a286893"
]
],
"x": 870,
"y": 560,
"z": "fbda6ab16491b918"
},
{
"finalize": "",
"func": "msg.topic = \"xcam_\" + msg.topic;\nreturn msg;",
"id": "bbe702116839ce33",
"initialize": "",
"libs": [
],
"name": "xcam",
"noerr": 0,
"outputs": 1,
"type": "function",
"wires": [
[
"c07c594a0c2e129f",
"8881f43c7a7a51d2"
]
],
"x": 1090,
"y": 480,
"z": "fbda6ab16491b918"
},
{
"finalize": "",
"func": "msg.topic = \"ipcam_\" + msg.topic;\nreturn msg;",
"id": "6d436f1ee46dfcfb",
"initialize": "",
"libs": [
],
"name": "ipcam",
"noerr": 0,
"outputs": 1,
"type": "function",
"wires": [
[
"c07c594a0c2e129f",
"8881f43c7a7a51d2"
]
],
"x": 1090,
"y": 520,
"z": "fbda6ab16491b918"
},
{
"finalize": "",
"func": "msg.topic = \"lights_report_\" + msg.topic;\nreturn msg;",
"id": "857cbcb12a286893",
"initialize": "",
"libs": [
],
"name": "lights_report",
"noerr": 0,
"outputs": 1,
"type": "function",
"wires": [
[
"c07c594a0c2e129f",
"8881f43c7a7a51d2"
]
],
"x": 1070,
"y": 560,
"z": "fbda6ab16491b918"
},
{
"checkall": "true",
"id": "1ba24409c2968b9b",
"name": "",
"outputs": 2,
"property": "topic",
"propertyType": "msg",
"repair": false,
"rules": [
{
"t": "eq",
"v": "ams",
"vt": "str"
},
{
"t": "else"
}
],
"type": "switch",
"wires": [
[
"ab0dc6a090a0a446"
],
[
]
],
"x": 1050,
"y": 600,
"z": "fbda6ab16491b918"
},
{
"addname": "topic",
"arraySplt": 1,
"arraySpltType": "len",
"id": "ab0dc6a090a0a446",
"name": "",
"splt": "\\n",
"spltType": "str",
"stream": false,
"type": "split",
"wires": [
[
"a7161043993f2991"
]
],
"x": 1050,
"y": 660,
"z": "fbda6ab16491b918"
},
{
"finalize": "",
"func": "msg.ams_index = msg.parts.index;\nreturn msg;",
"id": "a7161043993f2991",
"initialize": "",
"libs": [
],
"name": "ams_index",
"noerr": 0,
"outputs": 1,
"type": "function",
"wires": [
[
"bc03ac54c7f2fbe7"
]
],
"x": 1230,
"y": 660,
"z": "fbda6ab16491b918"
},
{
"addname": "topic",
"arraySplt": 1,
"arraySpltType": "len",
"id": "bc03ac54c7f2fbe7",
"name": "",
"splt": "\\n",
"spltType": "str",
"stream": false,
"type": "split",
"wires": [
[
"83f634566d1a8fbe"
]
],
"x": 1050,
"y": 720,
"z": "fbda6ab16491b918"
},
{
"finalize": "",
"func": "msg.topic = \"ams_\" + msg.ams_index + \"_\" + msg.topic;\nreturn msg;",
"id": "83f634566d1a8fbe",
"initialize": "",
"libs": [
],
"name": "ams",
"noerr": 0,
"outputs": 1,
"type": "function",
"wires": [
[
"5c082346a4d4c710"
]
],
"x": 1210,
"y": 720,
"z": "fbda6ab16491b918"
},
{
"finalize": "",
"func": "let data = {};\nlet payload = {};\nlet device = {};\n\n// Filter UoM from payload\nif (msg.topic == \"wifi_signal\")\n msg.payload = msg.payload.match(/^-?\\d+/)[0];\n\n// Inject config\nif (msg.topic.match(/temper/)) {\n msg.device_class = \"temperature\";\n}\nif (msg.topic.match(/humidity/)) {\n msg.device_class = \"humidity\";\n}\nif (msg.topic.match(/ams.+temp/)) {\n msg.device_class = \"temperature\";\n}\nif (msg.topic == \"wifi_signal\") {\n msg.device_class = \"signal_strength\"\n msg.unit_of_measurement = \"dBm\";\n}\n\nlet base_topic = \"homeassistant/sensor/\" + msg.machine_name + \"/\" + msg.topic;\ndata.topic = base_topic + \"/config\";\npayload.name = msg.topic;\n\ndevice.identifiers = [];\ndevice.identifiers[0] = msg.machine_name;\ndevice.manufacturer = \"BambuLab\";\ndevice.model = \"X1 Carbon\"\ndevice.name = msg.machine_name;\npayload.device = device;\npayload.unique_id = device.name + \"_\" + payload.name;\npayload.object_id = payload.unique_id;\n//payload.force_update = true;\npayload.expire_after = 3600;\n\nif (msg.device_class != undefined)\n payload.device_class = msg.device_class;\n\nif (msg.unit_of_measurement != undefined)\n payload.unit_of_measurement = msg.unit_of_measurement;\n\nif (payload.device_class == \"temperature\") {\n payload.unit_of_measurement = \"C\";\n payload.temperature_unit = \"C\";\n}\n\npayload.state_topic = base_topic + \"/state\";\ndata.payload = payload;\n\nreturn data;",
"id": "c07c594a0c2e129f",
"initialize": "",
"libs": [
],
"name": "Config",
"noerr": 0,
"outputs": 1,
"type": "function",
"wires": [
[
"315640efd57b9ca8"
]
],
"x": 1070,
"y": 220,
"z": "fbda6ab16491b918"
},
{
"finalize": "",
"func": "// Filter UoM from payload\nif (msg.topic == \"wifi_signal\")\n msg.payload = msg.payload.match(/^-?\\d+/)[0];\n\nmsg.topic = \"homeassistant/sensor/\" + msg.machine_name + \"/\" + msg.topic + \"/state\";\n\nreturn msg;",
"id": "8881f43c7a7a51d2",
"initialize": "",
"libs": [
],
"name": "State",
"noerr": 0,
"outputs": 1,
"type": "function",
"wires": [
[
"2ac19671d3d98fac"
]
],
"x": 1070,
"y": 260,
"z": "fbda6ab16491b918"
},
{
"action": "str",
"id": "315640efd57b9ca8",
"name": "",
"pretty": false,
"property": "payload",
"type": "json",
"wires": [
[
"0888ff83806a53f3"
]
],
"x": 1390,
"y": 220,
"z": "fbda6ab16491b918"
},
{
"adjAmount": 0,
"adjDir": "add",
"adjType": "days",
"format": "",
"id": "ce69e5023a185b3b",
"input": "",
"inputType": "msg",
"inTz": "America/Chicago",
"locale": "C",
"name": "",
"output": "",
"outputType": "msg",
"outTz": "America/Chicago",
"topic": "",
"type": "moment",
"wires": [
[
"c07c594a0c2e129f",
"8881f43c7a7a51d2"
]
],
"x": 820,
"y": 220,
"z": "fbda6ab16491b918"
},
{
"broker": "489094618c340eef",
"contentType": "",
"correl": "",
"expiry": "",
"id": "0888ff83806a53f3",
"name": "Home Assistant",
"qos": "1",
"respTopic": "",
"retain": "true",
"topic": "",
"type": "mqtt out",
"userProps": "",
"wires": [
],
"x": 1580,
"y": 220,
"z": "fbda6ab16491b918"
},
{
"func": "rbei",
"gap": "",
"id": "3f263dac732c7e32",
"inout": "out",
"name": "",
"property": "payload",
"septopics": true,
"start": "",
"topi": "topic",
"type": "rbe",
"wires": [
[
"315640efd57b9ca8"
]
],
"x": 1230,
"y": 180,
"z": "fbda6ab16491b918"
},
{
"func": "rbei",
"gap": "",
"id": "387c6bbfebcc89b6",
"inout": "out",
"name": "",
"property": "payload",
"septopics": true,
"start": "",
"topi": "topic",
"type": "rbe",
"wires": [
[
"2ac19671d3d98fac"
]
],
"x": 1230,
"y": 300,
"z": "fbda6ab16491b918"
},
{
"checkall": "true",
"id": "5c082346a4d4c710",
"name": "",
"outputs": 2,
"property": "topic",
"propertyType": "msg",
"repair": false,
"rules": [
{
"case": false,
"t": "regex",
"v": "_tray",
"vt": "str"
},
{
"t": "else"
}
],
"type": "switch",
"wires": [
[
],
[
"c07c594a0c2e129f",
"8881f43c7a7a51d2"
]
],
"x": 1350,
"y": 720,
"z": "fbda6ab16491b918"
},
{
"autoConnect": true,
"birthMsg": {
},
"birthPayload": "",
"birthQos": "0",
"birthTopic": "",
"broker": "3dp-x1c-001.home.mkosmo.net",
"cleansession": true,
"clientid": "",
"closeMsg": {
},
"closePayload": "",
"closeQos": "0",
"closeTopic": "",
"id": "ee08bb6102171c28",
"keepalive": "60",
"name": "3dp-x1c-001",
"port": "1883",
"protocolVersion": "4",
"sessionExpiry": "",
"type": "mqtt-broker",
"userProps": "",
"usetls": false,
"willMsg": {
},
"willPayload": "",
"willQos": "0",
"willTopic": ""
},
{
"autoConnect": true,
"birthMsg": {
},
"birthPayload": "",
"birthQos": "0",
"birthTopic": "",
"broker": "core-mosquitto",
"cleansession": true,
"clientid": "",
"closeMsg": {
},
"closePayload": "",
"closeQos": "0",
"closeTopic": "",
"id": "489094618c340eef",
"keepalive": "60",
"name": "homeassistant",
"port": "1883",
"protocolVersion": "4",
"sessionExpiry": "",
"type": "mqtt-broker",
"usetls": false,
"willMsg": {
},
"willPayload": "",
"willQos": "0",
"willTopic": ""
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment