node-red-contrib-universal-mqtt-switch 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +33 -0
- package/package.json +26 -0
- package/universal-switch.js +15 -0
- package/universal_MQTT_switch.json +279 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 gparduino
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# node-red-contrib-universal-mqtt-switch
|
|
2
|
+
|
|
3
|
+
A robust, "set-and-forget" Node-RED subflow for controlling nearly any MQTT-enabled switch. Perfect for mixing old Shelly Gen 1 hardware with newer OpenBeken or Tasmota devices in **Dashboard 2.0**.
|
|
4
|
+
|
|
5
|
+
## Why use this?
|
|
6
|
+
MQTT devices are notorious for having inconsistent topic structures. This node abstracts that away:
|
|
7
|
+
- **Input:** Accepts simple `true`/`false` booleans.
|
|
8
|
+
- **Output:** Sends the correct string (`on`/`off`) or integer (`1`/`0`) based on your config.
|
|
9
|
+
- **Feedback:** Converts device responses back into booleans for UI switches.
|
|
10
|
+
- **Safety:** Built-in 5-minute watchdog warns you via node status if the device drops off the network.
|
|
11
|
+
|
|
12
|
+
## Installation
|
|
13
|
+
Run the following command in your Node-RED user directory (typically `~/.node-red`):
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install node-red-contrib-universal-mqtt-switch
|
|
17
|
+
|
|
18
|
+
## Getting Started
|
|
19
|
+
1. **Drag** the 'Universal MQTT Switch' node onto your workspace.
|
|
20
|
+
2. **Double-click** to configure.
|
|
21
|
+
3. **Select your Broker** from the dropdown (or create a new one).
|
|
22
|
+
4. **Enter your topics** (Tip: Copy/Paste exactly from your device's web UI).
|
|
23
|
+
5. **Connect** a Dashboard 2.0 Switch to the input and output.
|
|
24
|
+
|
|
25
|
+
REMEMBER: if you wire a Dashboard 2.0 Switch to this node - to prevent command loops wire the first output of this node to the input of your Dashboard 2.0 Switch to keep its state synchronized, but ENSURE the Dashboard Switch node does pass input messages to its output (that checkbox is off by default usually and should stay that way.)
|
|
26
|
+
|
|
27
|
+
### Payload Compatibility Guide
|
|
28
|
+
| Device Type | Use Strings Setting | Expected MQTT Payload |
|
|
29
|
+
| :--- | :--- | :--- |
|
|
30
|
+
| **Shelly (Gen 1)** | Checked (True) | `"on"` / `"off"` |
|
|
31
|
+
| **Tasmota** | Checked (True) | `"ON"` / `"OFF"` |
|
|
32
|
+
| **OpenBeken** | Unchecked (False) | `1` / `0` |
|
|
33
|
+
| **Home Assistant** | Unchecked (False) | `1` / `0` |
|
package/package.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "node-red-contrib-universal-mqtt-switch",
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"description": "A universal MQTT switch subflow for Shelly, OpenBeken, and legacy devices",
|
|
5
|
+
"main": "universal-switch.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
|
+
},
|
|
9
|
+
"keywords": [
|
|
10
|
+
"node-red",
|
|
11
|
+
"mqtt",
|
|
12
|
+
"shelly",
|
|
13
|
+
"openbeken",
|
|
14
|
+
"home-automation"
|
|
15
|
+
],
|
|
16
|
+
"node-red": {
|
|
17
|
+
"nodes": {
|
|
18
|
+
"universal-switch": "universal-switch.js"
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"author": "gparduino",
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"engines": {
|
|
24
|
+
"node": ">=12.0.0"
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
module.exports = function(RED) {
|
|
2
|
+
const fs = require("fs");
|
|
3
|
+
const path = require("path");
|
|
4
|
+
|
|
5
|
+
try {
|
|
6
|
+
// Load the subflow JSON file
|
|
7
|
+
const subflowPath = path.join(__dirname, "universal_MQTT_switch.json");
|
|
8
|
+
const subflowData = JSON.parse(fs.readFileSync(subflowPath, "utf8"));
|
|
9
|
+
|
|
10
|
+
// Register the FULL array so Node-RED sees the subflow AND its internal nodes
|
|
11
|
+
RED.nodes.registerSubflow(subflowData);
|
|
12
|
+
} catch (err) {
|
|
13
|
+
RED.log.error("Failed to load Universal MQTT Switch subflow: " + err.message);
|
|
14
|
+
}
|
|
15
|
+
};
|
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
[
|
|
2
|
+
{
|
|
3
|
+
"id": "ed07e87722270aef",
|
|
4
|
+
"type": "subflow",
|
|
5
|
+
"name": "Universal MQTT Switch",
|
|
6
|
+
"info": "Generic handler for Shelly, OpenBeken, and legacy MQTT devices.",
|
|
7
|
+
"category": "",
|
|
8
|
+
"in": [
|
|
9
|
+
{
|
|
10
|
+
"x": 60,
|
|
11
|
+
"y": 80,
|
|
12
|
+
"wires": [
|
|
13
|
+
{
|
|
14
|
+
"id": "87c2ee5bfb812754"
|
|
15
|
+
}
|
|
16
|
+
]
|
|
17
|
+
}
|
|
18
|
+
],
|
|
19
|
+
"out": [
|
|
20
|
+
{
|
|
21
|
+
"x": 950,
|
|
22
|
+
"y": 60,
|
|
23
|
+
"wires": [
|
|
24
|
+
{
|
|
25
|
+
"id": "87c2ee5bfb812754",
|
|
26
|
+
"port": 0
|
|
27
|
+
}
|
|
28
|
+
]
|
|
29
|
+
}
|
|
30
|
+
],
|
|
31
|
+
"env": [
|
|
32
|
+
{
|
|
33
|
+
"name": "CMD_TOPIC",
|
|
34
|
+
"type": "str",
|
|
35
|
+
"value": "",
|
|
36
|
+
"ui": {
|
|
37
|
+
"label": {
|
|
38
|
+
"en-US": "Command Topic (Set)"
|
|
39
|
+
},
|
|
40
|
+
"type": "input",
|
|
41
|
+
"opts": {
|
|
42
|
+
"types": [
|
|
43
|
+
"str",
|
|
44
|
+
"num",
|
|
45
|
+
"bool",
|
|
46
|
+
"json",
|
|
47
|
+
"bin",
|
|
48
|
+
"env",
|
|
49
|
+
"conf-types"
|
|
50
|
+
]
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
"name": "STATE_TOPIC",
|
|
56
|
+
"type": "str",
|
|
57
|
+
"value": "",
|
|
58
|
+
"ui": {
|
|
59
|
+
"label": {
|
|
60
|
+
"en-US": "State Topic (Get)"
|
|
61
|
+
},
|
|
62
|
+
"type": "input",
|
|
63
|
+
"opts": {
|
|
64
|
+
"types": [
|
|
65
|
+
"str",
|
|
66
|
+
"num",
|
|
67
|
+
"bool",
|
|
68
|
+
"json",
|
|
69
|
+
"bin",
|
|
70
|
+
"env",
|
|
71
|
+
"conf-types"
|
|
72
|
+
]
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
{
|
|
77
|
+
"name": "AVAIL_TOPIC",
|
|
78
|
+
"type": "str",
|
|
79
|
+
"value": "",
|
|
80
|
+
"ui": {
|
|
81
|
+
"label": {
|
|
82
|
+
"en-US": "Availability Topic"
|
|
83
|
+
},
|
|
84
|
+
"type": "input",
|
|
85
|
+
"opts": {
|
|
86
|
+
"types": [
|
|
87
|
+
"str",
|
|
88
|
+
"num",
|
|
89
|
+
"bool",
|
|
90
|
+
"json",
|
|
91
|
+
"bin",
|
|
92
|
+
"env",
|
|
93
|
+
"conf-types"
|
|
94
|
+
]
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
},
|
|
98
|
+
{
|
|
99
|
+
"name": "USE_STRINGS",
|
|
100
|
+
"type": "bool",
|
|
101
|
+
"value": "true",
|
|
102
|
+
"ui": {
|
|
103
|
+
"label": {
|
|
104
|
+
"en-US": "Use 'on/off' strings (Shelly style)"
|
|
105
|
+
},
|
|
106
|
+
"type": "checkbox"
|
|
107
|
+
}
|
|
108
|
+
},
|
|
109
|
+
{
|
|
110
|
+
"name": "CONFIG_NODE",
|
|
111
|
+
"type": "mqtt-broker",
|
|
112
|
+
"value": "",
|
|
113
|
+
"ui": {
|
|
114
|
+
"label": {
|
|
115
|
+
"en-US": "Broker"
|
|
116
|
+
},
|
|
117
|
+
"type": "conf-types"
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
],
|
|
121
|
+
"meta": {
|
|
122
|
+
"module": "node-red-contrib-universal-mqtt",
|
|
123
|
+
"type": "universal-switch",
|
|
124
|
+
"version": "1.1.0",
|
|
125
|
+
"author": "gparduino",
|
|
126
|
+
"desc": "Support for Shelly, OpenBeken, and others",
|
|
127
|
+
"license": "MIT"
|
|
128
|
+
},
|
|
129
|
+
"color": "#C0DEED",
|
|
130
|
+
"icon": "node-red/light.svg",
|
|
131
|
+
"status": {
|
|
132
|
+
"x": 950,
|
|
133
|
+
"y": 140,
|
|
134
|
+
"wires": [
|
|
135
|
+
{
|
|
136
|
+
"id": "87c2ee5bfb812754",
|
|
137
|
+
"port": 2
|
|
138
|
+
}
|
|
139
|
+
]
|
|
140
|
+
}
|
|
141
|
+
},
|
|
142
|
+
{
|
|
143
|
+
"id": "87c2ee5bfb812754",
|
|
144
|
+
"type": "function",
|
|
145
|
+
"z": "ed07e87722270aef",
|
|
146
|
+
"name": "Logic Engine",
|
|
147
|
+
"func": "const cmdTopic = env.get(\"CMD_TOPIC\");\nconst stateTopic = env.get(\"STATE_TOPIC\");\nconst availTopic = env.get(\"AVAIL_TOPIC\");\nconst useStrings = env.get(\"USE_STRINGS\");\n\n// 1. WATCHDOG\nif (msg.topic === \"watchdog\") {\n const s = { fill: \"red\", shape: \"ring\", text: \"Offline (Timeout)\" };\n node.status(s);\n return [null, null, { payload: s }];\n}\n\n// 2. INCOMING MQTT\nif (msg.topic === availTopic) {\n const isOnline = (msg.payload === \"online\" || msg.payload === \"1\" || msg.payload === 1 || msg.payload === true);\n const s = { fill: isOnline ? \"yellow\" : \"red\", shape: isOnline ? \"dot\" : \"ring\", text: isOnline ? \"Online\" : \"Offline\" };\n node.status(s);\n return [null, null, { payload: s }];\n}\n\nif (msg.topic === stateTopic) {\n const isOn = (msg.payload === \"on\" || msg.payload == 1 || msg.payload === \"1\" || msg.payload === true);\n const s = { fill: isOn ? \"green\" : \"grey\", shape: \"dot\", text: isOn ? \"On\" : \"Off\" };\n node.status(s);\n return [{ payload: isOn }, null, { payload: s }];\n}\n\n// 3. UI COMMAND (Input from Dashboard)\nif (msg.topic !== stateTopic && msg.topic !== availTopic) {\n const isTrue = (msg.payload === true || msg.payload === \"on\" || msg.payload === \"1\" || msg.payload === 1);\n let outPayload;\n \n if (useStrings) {\n outPayload = isTrue ? \"on\" : \"off\";\n } else {\n outPayload = isTrue ? 1 : 0;\n }\n \n return [null, { topic: cmdTopic, payload: outPayload }, null];\n}\n\nreturn null;",
|
|
148
|
+
"outputs": 3,
|
|
149
|
+
"x": 720,
|
|
150
|
+
"y": 80,
|
|
151
|
+
"wires": [
|
|
152
|
+
[],
|
|
153
|
+
[
|
|
154
|
+
"928a0fc426a549af"
|
|
155
|
+
],
|
|
156
|
+
[]
|
|
157
|
+
]
|
|
158
|
+
},
|
|
159
|
+
{
|
|
160
|
+
"id": "af111b31d258147f",
|
|
161
|
+
"type": "mqtt in",
|
|
162
|
+
"z": "ed07e87722270aef",
|
|
163
|
+
"name": "Listen",
|
|
164
|
+
"topic": "",
|
|
165
|
+
"qos": "2",
|
|
166
|
+
"datatype": "auto-detect",
|
|
167
|
+
"broker": "${CONFIG_NODE}",
|
|
168
|
+
"nl": false,
|
|
169
|
+
"rap": false,
|
|
170
|
+
"inputs": 1,
|
|
171
|
+
"x": 190,
|
|
172
|
+
"y": 200,
|
|
173
|
+
"wires": [
|
|
174
|
+
[
|
|
175
|
+
"12ee9dea65533d9b",
|
|
176
|
+
"87c2ee5bfb812754"
|
|
177
|
+
]
|
|
178
|
+
]
|
|
179
|
+
},
|
|
180
|
+
{
|
|
181
|
+
"id": "12ee9dea65533d9b",
|
|
182
|
+
"type": "trigger",
|
|
183
|
+
"z": "ed07e87722270aef",
|
|
184
|
+
"name": "Watchdog",
|
|
185
|
+
"op1": "",
|
|
186
|
+
"op2": "Offline",
|
|
187
|
+
"op1type": "nul",
|
|
188
|
+
"op2type": "str",
|
|
189
|
+
"duration": "5",
|
|
190
|
+
"extend": true,
|
|
191
|
+
"units": "min",
|
|
192
|
+
"reset": "",
|
|
193
|
+
"bytopic": "all",
|
|
194
|
+
"topic": "topic",
|
|
195
|
+
"outputs": 1,
|
|
196
|
+
"x": 410,
|
|
197
|
+
"y": 260,
|
|
198
|
+
"wires": [
|
|
199
|
+
[
|
|
200
|
+
"c78da593ddcb06fe"
|
|
201
|
+
]
|
|
202
|
+
]
|
|
203
|
+
},
|
|
204
|
+
{
|
|
205
|
+
"id": "c78da593ddcb06fe",
|
|
206
|
+
"type": "change",
|
|
207
|
+
"z": "ed07e87722270aef",
|
|
208
|
+
"name": "Set Topic",
|
|
209
|
+
"rules": [
|
|
210
|
+
{
|
|
211
|
+
"t": "set",
|
|
212
|
+
"p": "topic",
|
|
213
|
+
"pt": "msg",
|
|
214
|
+
"to": "watchdog",
|
|
215
|
+
"tot": "str"
|
|
216
|
+
}
|
|
217
|
+
],
|
|
218
|
+
"x": 570,
|
|
219
|
+
"y": 260,
|
|
220
|
+
"wires": [
|
|
221
|
+
[
|
|
222
|
+
"87c2ee5bfb812754"
|
|
223
|
+
]
|
|
224
|
+
]
|
|
225
|
+
},
|
|
226
|
+
{
|
|
227
|
+
"id": "928a0fc426a549af",
|
|
228
|
+
"type": "mqtt out",
|
|
229
|
+
"z": "ed07e87722270aef",
|
|
230
|
+
"name": "",
|
|
231
|
+
"topic": "",
|
|
232
|
+
"qos": "",
|
|
233
|
+
"retain": "",
|
|
234
|
+
"broker": "${CONFIG_NODE}",
|
|
235
|
+
"x": 940,
|
|
236
|
+
"y": 200,
|
|
237
|
+
"wires": []
|
|
238
|
+
},
|
|
239
|
+
{
|
|
240
|
+
"id": "2f171e57d4be7d5f",
|
|
241
|
+
"type": "inject",
|
|
242
|
+
"z": "ed07e87722270aef",
|
|
243
|
+
"name": "Init",
|
|
244
|
+
"props": [
|
|
245
|
+
{
|
|
246
|
+
"p": "payload"
|
|
247
|
+
}
|
|
248
|
+
],
|
|
249
|
+
"repeat": "",
|
|
250
|
+
"crontab": "",
|
|
251
|
+
"once": true,
|
|
252
|
+
"onceDelay": 0.1,
|
|
253
|
+
"topic": "",
|
|
254
|
+
"payload": "",
|
|
255
|
+
"payloadType": "date",
|
|
256
|
+
"x": 100,
|
|
257
|
+
"y": 140,
|
|
258
|
+
"wires": [
|
|
259
|
+
[
|
|
260
|
+
"1b4b4c486cf45ebb"
|
|
261
|
+
]
|
|
262
|
+
]
|
|
263
|
+
},
|
|
264
|
+
{
|
|
265
|
+
"id": "1b4b4c486cf45ebb",
|
|
266
|
+
"type": "function",
|
|
267
|
+
"z": "ed07e87722270aef",
|
|
268
|
+
"name": "Sub Manager",
|
|
269
|
+
"func": "const state = env.get(\"STATE_TOPIC\");\nconst avail = env.get(\"AVAIL_TOPIC\");\n\n// Return multiple subscriptions as an array of messages or handle sequentially\nnode.send({ topic: state, action: \"subscribe\" });\nnode.send({ topic: avail, action: \"subscribe\" });\nreturn null;",
|
|
270
|
+
"outputs": 1,
|
|
271
|
+
"x": 300,
|
|
272
|
+
"y": 140,
|
|
273
|
+
"wires": [
|
|
274
|
+
[
|
|
275
|
+
"af111b31d258147f"
|
|
276
|
+
]
|
|
277
|
+
]
|
|
278
|
+
}
|
|
279
|
+
]
|