Thanks Julian, Will digest over the weekend !!
As usual plenty to learn from the master !
How are the eyes going - on the mend ?
regards
Craig
Thanks Julian, Will digest over the weekend !!
As usual plenty to learn from the master !
How are the eyes going - on the mend ?
regards
Craig
Thanks for asking Craig. Getting there. I've some temporary glasses which should help tide me over until I can get my other eye's cataract sorted.
If you need to look at some code, check out resources\editor-common.js which is loaded as an editor plugin. Each of my uibuilder nodes references the global object it creates.
Hi @craigcurtin, here is what I'm doing. After seeing @TotallyInformationās setup, Iām rethinking some things, though. Thereās things I can optimise, especially table structure.
[
{
"id": "5b275397f046e4ac",
"type": "function",
"z": "7fae2b56e5a5a4f9",
"name": "Runs on Start, sets up global object of devices and topics",
"func": "",
"outputs": 1,
"timeout": 0,
"noerr": 0,
"initialize": "\n//incoming MQTT topics get filtered against this, swapped if necessary, and passed through otherwise.\nconst inputtable = global.set(\"inputtable\", {\n \"/site/nest/downstairs/kitchen/light/island/status\": {\n \"topic\": \"/site/nest/downstairs/kitchen/light/island/status\"\n },\n \"/site/nest/downstairs/passage/light/passagelight/status/POWER\": {\n \"topic\": \"/site/nest/downstairs/passage/light/passagelight/status\"\n }\n}); \n\n//outgoing MQTT commands check against this and reformat as necessary.\nconst devicecmds = global.set(\"devicecmds\", {\n \"/site/nest/downstairs/kitchen/light/island/command\": {\n \"topic\": \"/site/nest/downstairs/kitchen/light/island/command\",\n \"suffix\": \"\",\n \"oncmd\": { \"state\": true },\n \"offcmd\": { \"state\": false }\n },\n \"/site/nest/downstairs/passage/light/passagelight/command\": {\n \"topic\": \"/site/nest/downstairs/passage/light/passagelight/command\",\n \"suffix\": \"/POWER\",\n \"oncmd\": \"ON\",\n \"offcmd\": \"OFF\"\n }\n});\n",
"finalize": "",
"libs": [],
"x": 370,
"y": 100,
"wires": [
[]
]
},
{
"id": "56715e17b139f601",
"type": "function",
"z": "7fae2b56e5a5a4f9",
"name": "Translate outgoing commands",
"func": "//grab the object of devices\nconst devicecmds = global.get(\"devicecmds\");\n\n//from it, grab the one relevant to the topic\nvar device = devicecmds[msg.topic];\n\n//check if its on or off, and replace with the appropriate command\nvar dothing = msg.payload.state;\nmsg.topic = msg.topic + device.suffix;\nif (dothing){\n msg.payload = device.oncmd;\n} else {\n msg.payload = device.offcmd;\n};\nreturn msg;",
"outputs": 1,
"timeout": 0,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 750,
"y": 400,
"wires": [
[
"af01d689e9075a0b"
]
]
},
{
"id": "9a83d53622022816",
"type": "function",
"z": "7fae2b56e5a5a4f9",
"name": "Translate incoming statuses",
"func": "//grab the object of topics we care about\nconst inputtable = global.get(\"inputtable\");\n\n//from that, grab the one mentioned in the topic\nvar thistopic = inputtable[msg.topic] || {};\n\nif (thistopic.topic == undefined){\n //if it's not on the list, it'll be undefined, so do nothing\n} \nelse {\n //otherwise replace with the right topic, and convert the payload\n msg.topic = thistopic.topic;\n switch(msg.payload){ \n case(\"ON\"):\n msg.payload = {\"state\": true};\n break;\n case(\"OFF\"):\n msg.payload = {\"state\": true};\n break;\n };\n};\nreturn msg;",
"outputs": 1,
"timeout": 0,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 470,
"y": 260,
"wires": [
[
"e6e80f8d33eacbbc"
]
]
},
{
"id": "68dc58ed08d7bbb3",
"type": "mqtt in",
"z": "7fae2b56e5a5a4f9",
"name": "MQTT IN",
"topic": "",
"qos": "2",
"datatype": "auto-detect",
"broker": "",
"nl": false,
"rap": true,
"rh": 0,
"inputs": 0,
"x": 200,
"y": 260,
"wires": [
[
"9a83d53622022816"
]
]
},
{
"id": "e6e80f8d33eacbbc",
"type": "link out",
"z": "7fae2b56e5a5a4f9",
"name": "Off we go to whichever relevant flow",
"mode": "link",
"links": [],
"x": 790,
"y": 260,
"wires": [],
"l": true
},
{
"id": "1908f12e31137d9b",
"type": "comment",
"z": "7fae2b56e5a5a4f9",
"name": "MQTT messages come in, and are translated if necessary before heading on",
"info": "",
"x": 410,
"y": 220,
"wires": []
},
{
"id": "dbe52e77f5147273",
"type": "comment",
"z": "7fae2b56e5a5a4f9",
"name": "After all the flows, we translate outgoing commands into the format the device expects",
"info": "",
"x": 440,
"y": 360,
"wires": []
},
{
"id": "1cd3428dcb8fe5f2",
"type": "link in",
"z": "7fae2b56e5a5a4f9",
"name": "We're heading out to the MQTT broker",
"links": [],
"x": 290,
"y": 400,
"wires": [
[
"56715e17b139f601"
]
],
"l": true
},
{
"id": "af01d689e9075a0b",
"type": "mqtt out",
"z": "7fae2b56e5a5a4f9",
"name": "",
"topic": "Publish MQTT",
"qos": "",
"retain": "",
"respTopic": "",
"contentType": "",
"userProps": "",
"correl": "",
"expiry": "",
"broker": "",
"x": 1050,
"y": 400,
"wires": []
}
]
I think that one of the big things I learned when using MQTT for a bit was that, at least using Mosquitto, it is so efficient at handling messages that it is not problem at all having different topic threads representing the same data for different purposes. Even my humble home automation setup can hit >10k messages in each direction each minute and has no real impact on my host system at all (and the same was true when running previously on a Pi).
For the most part though, I try to keep each message as simple as possible. Preferring single value messages to more complex and larger JSON values where feasible.