Skip to content

Instantly share code, notes, and snippets.

@letsautomatenet
Created January 15, 2024 15:27
Show Gist options
  • Save letsautomatenet/b192a92e99f0225bf69a26b97d8a070d to your computer and use it in GitHub Desktop.
Save letsautomatenet/b192a92e99f0225bf69a26b97d8a070d to your computer and use it in GitHub Desktop.
SwitchBot API - Node-RED Example
[
{
"id": "021eb3f3192c53ca",
"type": "tab",
"label": "Switchbot",
"disabled": false,
"info": "",
"env": []
},
{
"id": "36b1aef7b40835c7",
"type": "inject",
"z": "021eb3f3192c53ca",
"name": "",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "",
"payloadType": "date",
"x": 120,
"y": 80,
"wires": [
[
"da301782ea613799"
]
]
},
{
"id": "c4f3469b87f171bb",
"type": "function",
"z": "021eb3f3192c53ca",
"name": "Generate Header",
"func": "const token = msg.token;\nconst secret = msg.secret;\n\nconst t = Date.now();\nconst nonce = msg.payload;\nconst data = token + t + nonce;\nconst signTerm = crypto.createHmac('sha256', secret)\n .update(Buffer.from(data, 'utf-8'))\n .digest();\nconst sign = signTerm.toString(\"base64\");\n\nmsg = { \"headers\": { \"Authorization\": token, \"t\": t, \"sign\": sign, \"nonce\": nonce } }\n\nreturn msg;\n\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [
{
"var": "crypto",
"module": "crypto"
}
],
"x": 610,
"y": 80,
"wires": [
[
"a20f486cdaa4ca8f"
]
]
},
{
"id": "852917e81deadd18",
"type": "UUID",
"z": "021eb3f3192c53ca",
"name": "UUID",
"format": "Hex",
"x": 450,
"y": 80,
"wires": [
[
"c4f3469b87f171bb"
]
]
},
{
"id": "c7334a80a68ffb2f",
"type": "debug",
"z": "021eb3f3192c53ca",
"name": "Switchbot Result",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 890,
"y": 400,
"wires": []
},
{
"id": "a20f486cdaa4ca8f",
"type": "http request",
"z": "021eb3f3192c53ca",
"name": "",
"method": "GET",
"ret": "txt",
"paytoqs": "ignore",
"url": "https://api.switch-bot.com/v1.1/devices",
"tls": "aad507593baeeb79",
"persist": false,
"proxy": "",
"insecureHTTPParser": false,
"authType": "",
"senderr": false,
"headers": [],
"credentials": {},
"x": 810,
"y": 80,
"wires": [
[
"b6cd56821e3c2dd0"
]
]
},
{
"id": "da301782ea613799",
"type": "credentials",
"z": "021eb3f3192c53ca",
"name": "Switchbot Creds",
"props": [
{
"value": "token",
"type": "msg"
},
{
"value": "secret",
"type": "msg"
}
],
"credentials": {},
"x": 280,
"y": 80,
"wires": [
[
"852917e81deadd18"
]
]
},
{
"id": "b6cd56821e3c2dd0",
"type": "json",
"z": "021eb3f3192c53ca",
"name": "",
"property": "payload",
"action": "",
"pretty": false,
"x": 850,
"y": 220,
"wires": [
[
"c7334a80a68ffb2f"
]
]
},
{
"id": "aad507593baeeb79",
"type": "tls-config",
"name": "",
"cert": "",
"key": "",
"ca": "",
"certname": "",
"keyname": "",
"caname": "",
"servername": "",
"verifyservercert": true,
"alpnprotocol": ""
}
]
@letsautomatenet
Copy link
Author

This example connects to the SwitchBot API and returns a list of devices associated with that account.

Requires the following palettes to be installed:

node-red-contrib-credentials - For storing credentials: https://github.com/Steveorevo/node-red-contrib-credentials
random-generator_node-red-contrib - UUID Generator: https://flows.nodered.org/node/random-generator_node-red-contrib

Then follow the SwitchBot documentation to make different API calls: https://github.com/OpenWonderLabs/SwitchBotAPI

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