-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflow-node-red.json
More file actions
125 lines (125 loc) · 5.88 KB
/
flow-node-red.json
File metadata and controls
125 lines (125 loc) · 5.88 KB
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
[
{
"id": "tab_control_bombilla",
"type": "tab",
"label": "Control Bombilla Telegram (Publico)",
"disabled": false,
"info": "Flujo anonimizado para control de iluminación mediante Telegram y Home Assistant."
},
{
"id": "telegram_receiver_anon",
"type": "telegram receiver",
"z": "tab_control_bombilla",
"name": "Comandos Telegram",
"bot": "config_bot_anon",
"saveDataDir": "",
"filterCommands": false,
"hasinput": false,
"inputs": 0,
"handleallupdates": false,
"x": 160,
"y": 200,
"wires": [
[
"logic_function_anon"
],
[]
]
},
{
"id": "logic_function_anon",
"type": "function",
"z": "tab_control_bombilla",
"name": "Control Inteligente",
"func": "// ---------------------------\n// Control Inteligente Anonimizado\n// ---------------------------\n\nconst text = msg.payload.content.toLowerCase();\n\nlet mode = global.get(\"mode\") || \"manual\";\nlet timer = global.get(\"timer\");\n\nfunction clearTimer() {\n if (timer) {\n clearTimeout(timer);\n global.set(\"timer\", null);\n }\n}\n\nlet haTopic = null;\nlet haBrightness = null;\nlet telegramText = null;\n\nif (text.includes(\"estado\")) {\n telegramText = `💡 Estado actual\\nModo: ${mode}`;\n} \nelse if (text.includes(\"modo manual\")) {\n clearTimer();\n global.set(\"mode\", \"manual\");\n telegramText = \"🟢 Modo MANUAL activado\";\n} \nelse if (text.includes(\"modo noche\")) {\n clearTimer();\n global.set(\"mode\", \"noche\");\n\n timer = setTimeout(() => {\n node.send({ topic: \"off\" });\n }, 15 * 60 * 1000);\n\n global.set(\"timer\", timer);\n haTopic = \"on\";\n telegramText = \"🌙 Modo NOCHE activado (15 min)\";\n} \nelse if (text.startsWith(\"encender\")) {\n const match = text.match(/\\d+/);\n clearTimer();\n\n if (match) {\n const minutes = parseInt(match[0]);\n timer = setTimeout(() => {\n node.send({ topic: \"off\" });\n }, minutes * 60 * 1000);\n\n global.set(\"timer\", timer);\n global.set(\"mode\", \"temporizado\");\n haTopic = \"on\";\n telegramText = `⏱ Luz encendida ${minutes} min`;\n } else {\n global.set(\"mode\", \"manual\");\n haTopic = \"on\";\n telegramText = \"💡 Luz encendida\";\n }\n} \nelse if (text.includes(\"apagar\")) {\n clearTimer();\n global.set(\"mode\", \"manual\");\n haTopic = \"off\";\n telegramText = \"🔴 Luz apagada\";\n} \nelse if (text.startsWith(\"brillo\")) {\n const match = text.match(/\\d+/);\n if (match) {\n const value = parseInt(match[0]);\n if (value < 0 || value > 100) {\n telegramText = \"⚠️ El brillo debe estar entre 0 y 100\";\n } else {\n haTopic = \"on\";\n haBrightness = value;\n telegramText = `💡 Brillo ajustado a ${value}%`;\n }\n } else {\n telegramText = \"⚠️ Debes indicar un valor de brillo, ej: brillo 50\";\n }\n} \nelse {\n telegramText = \"⚠️ Comando no reconocido\";\n}\n\nlet haMsg = null;\nif (haTopic) {\n haMsg = {\n topic: haTopic,\n payload: {\n entity_id: \"light.tu_bombilla\",\n data: {}\n }\n };\n\n if (haTopic === \"on\" && haBrightness !== null) {\n haMsg.payload.data.brightness_pct = haBrightness;\n }\n\n if (haTopic === \"off\") {\n delete haMsg.payload.data;\n }\n}\n\nlet telegramMsg = {\n payload: {\n chatId: msg.originalMessage.chat.id,\n type: 'message',\n content: telegramText\n }\n};\n\nreturn [haMsg, telegramMsg];",
"outputs": 2,
"x": 430,
"y": 200,
"wires": [
[
"switch_on_off_anon",
"debug_ha_anon"
],
[
"telegram_sender_anon"
]
]
},
{
"id": "switch_on_off_anon",
"type": "switch",
"z": "tab_control_bombilla",
"name": "ON / OFF",
"property": "topic",
"propertyType": "msg",
"rules": [
{ "t": "eq", "v": "on", "vt": "str" },
{ "t": "eq", "v": "off", "vt": "str" }
],
"checkall": "true",
"repair": false,
"outputs": 2,
"x": 680,
"y": 200,
"wires": [
[ "ha_on_anon" ],
[ "ha_off_anon" ]
]
},
{
"id": "ha_on_anon",
"type": "api-call-service",
"z": "tab_control_bombilla",
"name": "Encender Luz",
"server": "",
"version": 7,
"action": "light.turn_on",
"entityId": [ "light.tu_bombilla" ],
"domain": "light",
"service": "turn_on",
"x": 940,
"y": 160,
"wires": [[]]
},
{
"id": "ha_off_anon",
"type": "api-call-service",
"z": "tab_control_bombilla",
"name": "Apagar Luz",
"server": "",
"version": 7,
"action": "light.turn_off",
"entityId": [ "light.tu_bombilla" ],
"domain": "light",
"service": "turn_off",
"x": 930,
"y": 220,
"wires": [[]]
},
{
"id": "telegram_sender_anon",
"type": "telegram sender",
"z": "tab_control_bombilla",
"name": "Enviar Telegram",
"bot": "config_bot_anon",
"x": 680,
"y": 300,
"wires": [[]]
},
{
"id": "debug_ha_anon",
"type": "debug",
"z": "tab_control_bombilla",
"name": "Debug HA",
"active": true,
"complete": "payload",
"x": 710,
"y": 100,
"wires": []
},
{
"id": "config_bot_anon",
"type": "telegram bot",
"botname": "Bot_Generico"
}
]