-
Notifications
You must be signed in to change notification settings - Fork 7
/
flow-HA-MQTT-Tecalor-TTC05.json
247 lines (247 loc) · 12.6 KB
/
flow-HA-MQTT-Tecalor-TTC05.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
[
{
"id": "52a6dfdb.a96f1",
"type": "tab",
"label": "HA MQTT",
"disabled": false,
"info": ""
},
{
"id": "2ed2435d.2d78ac",
"type": "comment",
"z": "52a6dfdb.a96f1",
"name": "Homeassistant MQTT Discovery",
"info": "Set device defaults to use this flow",
"x": 350,
"y": 480,
"wires": []
},
{
"id": "3ae6a88d.e92dc8",
"type": "link in",
"z": "52a6dfdb.a96f1",
"name": "HA MQTT",
"links": [
"ff9f920.872f37",
"53ba033a.2de20c",
"76cbea82.27ce74"
],
"x": 235,
"y": 520,
"wires": [
[
"4d9a38ab.20ea78"
]
]
},
{
"id": "6ae16afe.403504",
"type": "function",
"z": "52a6dfdb.a96f1",
"name": "Set defaults",
"func": "// See: https://www.home-assistant.io/docs/mqtt/discovery/\n\n// PREPARE DISCOVERY TOPIC\n//\ndiscovery_prefix = \"homeassistant\";\n\n// <component>: One of the supported MQTT components, eg. sensor, binary_sensor\ncomponent = \"sensor\";\n\n// <node_id>: ID of the node providing the topic, this is not used by Home Assistant \n// but may be used to structure the MQTT topic. The ID of the node must only consist\n// of characters from the character class [a-zA-Z0-9_-] (alphanumerics, underscore\n// and hyphen).\nnode_id = msg.device[\"type\"];\n\n// <object_id>: The ID of the device. This is only to allow for separate topics for\n// each device and is not used for the entity_id. The ID of the device must only \n// consist of characters from the character class [a-zA-Z0-9_-] (alphanumerics, \n// underscore and hyphen).\n\n\n// DEFAULT SETTINGS\n//\nmsg.ha_mqtt = {\n // DEFAULTS\n \"topic_prefix\": discovery_prefix + \"/\" + component + \"/\" + node_id,\n \n // INPUT VALUES\n \"selectValue\": msg.selectValue,\n \"selectValue2\": msg.selectValue2,\n \"measurement\": msg.measurement,\n \"unit\": msg.unit,\n \"component\": component,\n\n \"device\": {\n \"name\": msg.device[\"name\"],\n \"model\": msg.device[\"model\"],\n \"manufacturer\": msg.device[\"manufacturer\"],\n //\"connections\": msg.device[\"connections\"],\n \"identifiers\": msg.device[\"identifiers\"]\n }\n};\n\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"x": 370,
"y": 560,
"wires": [
[
"d4688dd7.a904f"
]
]
},
{
"id": "4d9a38ab.20ea78",
"type": "function",
"z": "52a6dfdb.a96f1",
"name": "Check input",
"func": "error_text = \"\";\n\nif (typeof msg.selectValue === 'undefined' || msg.selectValue == \"\") {\n error_text = \"msg.selectValue is empty!\";\n}\nelse if (typeof msg.selectValue2 === 'undefined' || msg.selectValue2 == \"\") {\n error_text = \"msg.selectValue2 is empty!\";\n}\nelse if (typeof msg.measurement === 'undefined' || msg.measurement == \"\") {\n error_text = \"msg.measurement is empty!\";\n}\nelse if (typeof msg.unit === 'undefined' || msg.unit == \"\") {\n error_text = \"msg.unit is empty!\";\n}\nelse if ( typeof msg.device === 'undefined' || msg.device.length == 0) {\n error_text = \"msg.device not defined!\";\n}\n\nif (error_text != \"\") {\n node.status({fill:\"red\",shape:\"ring\",text:error_text});\n node.warn(error_text);\n}\n\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"x": 350,
"y": 520,
"wires": [
[
"6ae16afe.403504"
]
]
},
{
"id": "d4688dd7.a904f",
"type": "function",
"z": "52a6dfdb.a96f1",
"name": "Prepare MQTT topic prefix\\n and component name",
"func": "function upperCaseFirstLetter(string) {\n return string.charAt(0).toUpperCase() + string.slice(1);\n}\n\nfunction lowerCaseAllWordsExceptFirstLetters(string) {\n return string.replace(/\\S*/g, function (word) {\n return word.charAt(0) + word.slice(1).toLowerCase();\n });\n}\n\nconst umlautMap = {\n '\\u00dc': 'UE',\n '\\u00c4': 'AE',\n '\\u00d6': 'OE',\n '\\u00fc': 'ue',\n '\\u00e4': 'ae',\n '\\u00f6': 'oe',\n '\\u00df': 'ss',\n}\n\nfunction replaceUmlaute(str) {\n return str\n .replace(/[\\u00dc|\\u00c4|\\u00d6][a-z]/g, (a) => {\n const big = umlautMap[a.slice(0, 1)];\n return big.charAt(0) + big.charAt(1).toLowerCase() + a.slice(1);\n })\n .replace(new RegExp('['+Object.keys(umlautMap).join('|')+']',\"g\"),\n (a) => umlautMap[a]\n );\n}\n\n// INPUTS\nselectValue = msg.ha_mqtt[\"selectValue\"];\nselectValue2 = msg.ha_mqtt[\"selectValue2\"];\n\ncompName = \"Tecalor\" + \" / \" + selectValue + \" / \" + selectValue2\ncompNameLc = replaceUmlaute(\"Tecalor\" + \"_\" + selectValue + \"_\" + selectValue2)\n .replace(/(\\w)\\s(\\d)/, \"$1$2\") // \"HK 1\" -> \"HK1\"\n .replace(/(\\w)\\.\\s/, \"$1\") // \"MIN. \" => \"MIN\"\n .replace(/[^a-zA-Z0-9_]/g, \"_\") // non-word chars -> _\n .toLowerCase();\n\nmsg.ha_mqtt[\"comp_name\"] = compName;\nmsg.ha_mqtt[\"comp_name_lc\"] = compNameLc;\nmsg.ha_mqtt[\"topic_prefix\"] += \"/\" + compNameLc;\n\n\n//msg.ha_mqtt[\"comp_name\"] = upperCaseFirstLetter(lowerCaseAllWordsExceptFirstLetters(selectValue));\n//ha_comp_name_lc = selectValue.toLowerCase().replace(/\\ /g, \"_\").replace(/\\./g, \"\");\n//msg.ha_mqtt[\"comp_name_lc\"] = replaceUmlaute(ha_comp_name_lc);\n\n//msg.ha_mqtt[\"topic_prefix\"] += \"/\" + msg.ha_mqtt[\"comp_name_lc\"];\n\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"x": 440,
"y": 620,
"wires": [
[
"fedc70ce.5e368",
"e4b9a282.a552e"
]
]
},
{
"id": "fedc70ce.5e368",
"type": "function",
"z": "52a6dfdb.a96f1",
"name": "Define MQTT topic",
"func": "// Setup message\nmsg.topic = msg.ha_mqtt[\"topic_prefix\"] + \"/state\";\n\n// msg.payload contains already the new value\n\n// binary sensor will receive an instant state update after subscription and HA will display the correct state on startup\nmsg.retain = true;\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"x": 690,
"y": 580,
"wires": [
[
"7318f441.6fe19c"
]
]
},
{
"id": "5b2086e3.cdbca8",
"type": "function",
"z": "52a6dfdb.a96f1",
"name": "Finalize discovery msg",
"func": "// Setup message\nmsg.topic = msg.ha_mqtt[\"topic_prefix\"] + \"/config\";\n\n// Build discovery payload\n// See: https://www.home-assistant.io/docs/mqtt/discovery/\nmsg.payload = {\n \"name\": msg.ha_mqtt[\"comp_name\"],\n \"device\": msg.ha_mqtt[\"device\"],\n \"unique_id\": msg.ha_mqtt[\"unique_id\"],\n \"state_topic\": msg.ha_mqtt[\"topic_prefix\"] + \"/state\"\n}\nif (msg.ha_mqtt[\"device_class\"] != \"none\" )\n msg.payload[\"device_class\"] = msg.ha_mqtt[\"device_class\"];\nif (msg.ha_mqtt[\"comp_unit\"] != \"\" )\n msg.payload[\"unit_of_measurement\"] = msg.ha_mqtt[\"comp_unit\"];\n\n// binary sensor will receive an instant state update after subscription and HA will display the correct state on startup\nmsg.retain = true;\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"x": 720,
"y": 660,
"wires": [
[
"b7d00210.db23",
"ec880450.9498e8",
"7318f441.6fe19c"
]
]
},
{
"id": "e4b9a282.a552e",
"type": "function",
"z": "52a6dfdb.a96f1",
"name": "Prepare component details",
"func": "// COMPONENT DETAILS\n// ref: https://www.home-assistant.io/integrations/sensor/#device-class\nswitch (msg.ha_mqtt[\"measurement\"]) {\n case \"temperature\":\n msg.ha_mqtt[\"device_class\"] = \"temperature\";\n break;\n case \"humidity\":\n msg.ha_mqtt[\"device_class\"] = \"humidity\";\n break;\n case \"frequency\":\n msg.ha_mqtt[\"device_class\"] = \"none\";\n break;\n case \"energy\":\n msg.ha_mqtt[\"device_class\"] = \"energy\";\n break;\n case \"power\":\n msg.ha_mqtt[\"device_class\"] = \"power\";\n break;\n case \"pressure\":\n msg.ha_mqtt[\"device_class\"] = \"pressure\";\n break;\n case \"volume_flow\":\n case \"state\":\n case \"time\":\n msg.ha_mqtt[\"device_class\"] = \"none\";\n break;\n default:\n error_text = \"Unknown measurement: \" + msg.ha_mqtt[\"measurement\"];\n node.status({fill:\"red\",shape:\"ring\",text:error_text});\n node.warn(error_text);\n break;\n}\n\n\n// '%'' - humidity, '°C' - degree celcius\nswitch (msg.ha_mqtt[\"unit\"]) {\n case \"celsius\": // temperature\n msg.ha_mqtt[\"comp_unit\"] = \"°C\";\n break;\n case \"percent\": // humidity\n msg.ha_mqtt[\"comp_unit\"] = \"%\"\n break;\n case \"hertz\": // frequency\n msg.ha_mqtt[\"comp_unit\"] = \"Hz\"\n break;\n case \"kWh\": // energy\n msg.ha_mqtt[\"comp_unit\"] = \"kWh\"\n break;\n case \"MWh\": // energy\n msg.ha_mqtt[\"comp_unit\"] = \"MWh\"\n break;\n case \"W\": // power\n msg.ha_mqtt[\"comp_unit\"] = \"W\"\n break;\n case \"litre_per_min\": // volume_flow\n msg.ha_mqtt[\"comp_unit\"] = \"l/min\"\n break;\n case \"value\": // state\n msg.ha_mqtt[\"comp_unit\"] = \"\"\n break;\n case \"hour\": // time\n msg.ha_mqtt[\"comp_unit\"] = \"h\"\n break;\n case \"second\": // time\n msg.ha_mqtt[\"comp_unit\"] = \"s\"\n break;\n case \"bar\": // time\n msg.ha_mqtt[\"comp_unit\"] = \"bar\"\n break;\n default:\n error_text = \"Unknown unit: \" + msg.ha_mqtt[\"unit\"];\n node.status({fill:\"red\",shape:\"ring\",text:error_text});\n node.warn(error_text);\n break;\n}\nmsg.ha_mqtt[\"unique_id\"] = msg.ha_mqtt[\"component\"] + \"_\" + msg.ha_mqtt[\"comp_name_lc\"] + \"_\" + msg.ha_mqtt[\"device_class\"];\n\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"x": 720,
"y": 620,
"wires": [
[
"5b2086e3.cdbca8"
]
]
},
{
"id": "b7d00210.db23",
"type": "delay",
"z": "52a6dfdb.a96f1",
"name": "Limit config msgs",
"pauseType": "timed",
"timeout": "5",
"timeoutUnits": "seconds",
"rate": "1",
"nbRateUnits": "1",
"rateUnits": "hour",
"randomFirst": "1",
"randomLast": "5",
"randomUnits": "seconds",
"drop": true,
"x": 950,
"y": 660,
"wires": [
[
"7318f441.6fe19c"
]
]
},
{
"id": "7318f441.6fe19c",
"type": "mqtt out",
"z": "52a6dfdb.a96f1",
"name": "Publish",
"topic": "",
"qos": "",
"retain": "",
"broker": "3d57c91f.bdd2d6",
"x": 1160,
"y": 580,
"wires": []
},
{
"id": "ec880450.9498e8",
"type": "debug",
"z": "52a6dfdb.a96f1",
"name": "",
"active": false,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "true",
"targetType": "full",
"statusVal": "",
"statusType": "auto",
"x": 690,
"y": 700,
"wires": []
},
{
"id": "b72b4ed8.20638",
"type": "inject",
"z": "52a6dfdb.a96f1",
"name": "Trigger to flush messages \\n to get HA discover new \\n sensors immediately",
"props": [
{
"p": "flush",
"v": "true",
"vt": "bool"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"x": 710,
"y": 780,
"wires": [
[
"b7d00210.db23"
]
]
},
{
"id": "3d57c91f.bdd2d6",
"type": "mqtt-broker",
"name": "Mosquitto",
"broker": "192.168.188.132",
"port": "1883",
"clientid": "node-red",
"usetls": false,
"compatmode": false,
"keepalive": "60",
"cleansession": true,
"birthTopic": "",
"birthQos": "0",
"birthPayload": "",
"closeTopic": "",
"closeQos": "0",
"closePayload": "",
"willTopic": "",
"willQos": "0",
"willPayload": ""
}
]