Skip to content

Instantly share code, notes, and snippets.

@bglnelissen
Last active May 25, 2020 17:09
Show Gist options
  • Save bglnelissen/c31e18e6bfd9733879e5f9099c91f1c2 to your computer and use it in GitHub Desktop.
Save bglnelissen/c31e18e6bfd9733879e5f9099c91f1c2 to your computer and use it in GitHub Desktop.
mqtt and Homekit for Shelly 1 with Tasmota

This flow can turn the Shelly 1 with Tasmota on or off and sets the user interface accordingly.

The flow works in 2 ways:

  1. It set the values using Homekit or Node-RED UI and send the mqtt commands.
  2. It listens to the mqtt commands and set the switches in the Home app & Node-RED UI accordingly.

This way you can also control the light using a direct switch on the Shelly device and your user interface correctly shows on or off.

This flow has some custom javascript because that is my preferred way to handle the message (msg) flow. This flow also prevents to go in an infinite loop which seems to be a feature of homekit-service.

The device in this example is called bureaulamp (which is Dutch for desk light or desk lamp). Personally I use a text editor to rename the device in the JSON file to give it a new name. I guess using an inject node could also work.

Let me know if you have improvements.

Have fun,

Bas Nelissen

[
{
"id": "cccc2a50.dc1d98",
"type": "mqtt in",
"z": "4d2d578e.6cea28",
"name": "bureaulamp",
"topic": "server/stat/bureaulamp/RESULT",
"qos": "0",
"datatype": "auto",
"broker": "707b233c.d0ad2c",
"x": 150,
"y": 480,
"wires": [
[
"1313e7b8.7cad58"
]
]
},
{
"id": "849705a5.16c0b",
"type": "function",
"z": "4d2d578e.6cea28",
"name": "mqtt 2 homekit",
"func": "// delete topic to prevent loop\nif (msg.payload.POWER == \"ON\"){\n msg.payload = {\n \"On\": 1\n }\n}else{\n msg.payload = {\n \"On\": 0\n }\n}\nreturn msg;",
"outputs": 1,
"noerr": 0,
"x": 560,
"y": 540,
"wires": [
[
"2ca327b4.382a28"
]
]
},
{
"id": "8b289149.f7a3c",
"type": "function",
"z": "4d2d578e.6cea28",
"name": "prepare switch",
"func": "if (msg.payload.POWER == \"ON\"){\n msg.payload = true\n}else{\n msg.payload = false\n}\nreturn msg;",
"outputs": 1,
"noerr": 0,
"x": 560,
"y": 480,
"wires": [
[
"c13bd0bf.3eb018"
]
]
},
{
"id": "32328423.dc283c",
"type": "function",
"z": "4d2d578e.6cea28",
"name": "homekit 2 mqtt",
"func": "// vars\nif(msg.payload.On){\n msg.payload = true\n}else{\n msg.payload = false\n}\nreturn msg;",
"outputs": 1,
"noerr": 0,
"x": 940,
"y": 540,
"wires": [
[
"e1c12735.825cb8"
]
]
},
{
"id": "c13bd0bf.3eb018",
"type": "ui_switch",
"z": "4d2d578e.6cea28",
"name": "",
"label": "Bureaulamp",
"tooltip": "",
"group": "efa9c015.d79758",
"order": 5,
"width": 0,
"height": 0,
"passthru": false,
"decouple": "true",
"topic": "server/cmnd/bureaulamp/POWER",
"style": "",
"onvalue": "true",
"onvalueType": "bool",
"onicon": "",
"oncolor": "",
"offvalue": "false",
"offvalueType": "bool",
"officon": "",
"offcolor": "",
"x": 750,
"y": 480,
"wires": [
[
"f0f75ebd.c55388"
]
]
},
{
"id": "1313e7b8.7cad58",
"type": "json",
"z": "4d2d578e.6cea28",
"name": "",
"property": "payload",
"action": "",
"pretty": false,
"x": 310,
"y": 480,
"wires": [
[
"26bf9763.22444"
]
]
},
{
"id": "f0f75ebd.c55388",
"type": "mqtt out",
"z": "4d2d578e.6cea28",
"name": "",
"topic": "",
"qos": "",
"retain": "",
"broker": "707b233c.d0ad2c",
"x": 1110,
"y": 480,
"wires": []
},
{
"id": "2ca327b4.382a28",
"type": "homekit-service",
"z": "4d2d578e.6cea28",
"isParent": true,
"bridge": "93e1fd.7adf2e",
"parentService": "",
"name": "Bureaulamp",
"serviceName": "Lightbulb",
"topic": "",
"filter": false,
"manufacturer": "Default Manufacturer",
"model": "Default Model",
"serialNo": "Default Serial Number",
"cameraConfigVideoProcessor": "ffmpeg",
"cameraConfigSource": "",
"cameraConfigStillImageSource": "",
"cameraConfigMaxStreams": 2,
"cameraConfigMaxWidth": 1280,
"cameraConfigMaxHeight": 720,
"cameraConfigMaxFPS": 10,
"cameraConfigMaxBitrate": 300,
"cameraConfigVideoCodec": "libx264",
"cameraConfigAudioCodec": "libfdk_aac",
"cameraConfigAudio": false,
"cameraConfigPacketSize": 1316,
"cameraConfigVerticalFlip": false,
"cameraConfigHorizontalFlip": false,
"cameraConfigMapVideo": "0:0",
"cameraConfigMapAudio": "0:1",
"cameraConfigVideoFilter": "scale=1280:720",
"cameraConfigAdditionalCommandLine": "-tune zerolatency",
"cameraConfigDebug": false,
"cameraConfigSnapshotOutput": "disabled",
"cameraConfigInterfaceName": "",
"characteristicProperties": "{}",
"x": 750,
"y": 540,
"wires": [
[
"32328423.dc283c"
],
[]
]
},
{
"id": "e1c12735.825cb8",
"type": "mqtt out",
"z": "4d2d578e.6cea28",
"name": "",
"topic": "",
"qos": "",
"retain": "",
"broker": "707b233c.d0ad2c",
"x": 1110,
"y": 540,
"wires": []
},
{
"id": "26bf9763.22444",
"type": "function",
"z": "4d2d578e.6cea28",
"name": "",
"func": "// fix repeated messages\nlet millisecondsBetweenMessages = 300;\nlet previousTime=context.get('time') || 0;\nlet currentTime=Math.round(Date.now());\nlet timeDifference=currentTime-previousTime;\n\n// set variable for next round\ncontext.set('time', currentTime);\n\nif (timeDifference < millisecondsBetweenMessages){\n node.warn('To fast: '+timeDifference+'ms');\n msg = null; // stop msg from sending\n}\n\nreturn msg;",
"outputs": 1,
"noerr": 0,
"x": 415,
"y": 480,
"wires": [
[
"8b289149.f7a3c",
"849705a5.16c0b"
]
],
"icon": "node-red/timer.svg",
"l": false
},
{
"id": "707b233c.d0ad2c",
"type": "mqtt-broker",
"z": "",
"name": "serverPi",
"broker": "localhost",
"port": "1883",
"clientid": "",
"usetls": false,
"compatmode": false,
"keepalive": "60",
"cleansession": true,
"birthTopic": "",
"birthQos": "0",
"birthRetain": "false",
"birthPayload": "",
"closeTopic": "",
"closeQos": "0",
"closeRetain": "false",
"closePayload": "",
"willTopic": "",
"willQos": "0",
"willRetain": "false",
"willPayload": ""
},
{
"id": "efa9c015.d79758",
"type": "ui_group",
"z": "",
"name": "Woonkamer",
"tab": "f529ddcc.a47f8",
"order": 1,
"disp": true,
"width": "6",
"collapse": false
},
{
"id": "93e1fd.7adf2e",
"type": "homekit-bridge",
"z": "",
"bridgeName": "server Home",
"pinCode": "111-11-111",
"port": "",
"allowInsecureRequest": false,
"manufacturer": "server",
"model": "Model",
"serialNo": "Serial Number",
"customMdnsConfig": false,
"mdnsMulticast": true,
"mdnsInterface": "",
"mdnsPort": "",
"mdnsIp": "",
"mdnsTtl": "",
"mdnsLoopback": true,
"mdnsReuseAddr": true,
"allowMessagePassthrough": true
},
{
"id": "f529ddcc.a47f8",
"type": "ui_tab",
"z": "",
"name": "Lichten",
"icon": "dashboard",
"order": 1,
"disabled": false,
"hidden": false
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment