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
248
249
250
251
252
| [
{
"id": "b70a836071e65056",
"type": "group",
"z": "a5197c9e9e9aac37",
"style": {
"stroke": "#999999",
"stroke-opacity": "1",
"fill": "none",
"fill-opacity": "1",
"label": true,
"label-position": "nw",
"color": "#a4a4a4"
},
"nodes": [
"f6509b6071e57fb5",
"31c3c6a161e74b1d",
"5cd9cb9df0eb0e43",
"5bf393c9d5ac73bb",
"28168acbee4d3057",
"e2524e1725f2d2ba",
"f82c99b2a2f9ebc6",
"bd1cf2989fb28ea0"
],
"x": 14,
"y": 419,
"w": 732,
"h": 262
},
{
"id": "f6509b6071e57fb5",
"type": "mqtt in",
"z": "a5197c9e9e9aac37",
"g": "b70a836071e65056",
"name": "EVCharger",
"topic": "HomeServerLiveValues/EVCharger",
"qos": "0",
"datatype": "auto-detect",
"broker": "9f574f2f6f5531cb",
"nl": false,
"rap": true,
"rh": 0,
"inputs": 0,
"x": 220,
"y": 520,
"wires": [
[
"5bf393c9d5ac73bb",
"5cd9cb9df0eb0e43"
]
]
},
{
"id": "31c3c6a161e74b1d",
"type": "victron-output-ess",
"z": "a5197c9e9e9aac37",
"g": "b70a836071e65056",
"service": "com.victronenergy.settings",
"path": "/Settings/CGwacs/AcPowerSetPoint",
"serviceObj": {
"service": "com.victronenergy.settings",
"name": "Venus settings",
"communityTag": "ess"
},
"pathObj": {
"path": "/Settings/CGwacs/AcPowerSetPoint",
"type": "integer",
"name": "Grid set-point (W)",
"mode": "both"
},
"initial": -10,
"name": "Grid setpoint",
"onlyChanges": false,
"roundValues": "no",
"rateLimit": 0,
"conditionalMode": false,
"condition1Operator": ">",
"condition2Enabled": false,
"condition2Service": "",
"condition2Path": "",
"condition2Operator": ">",
"logicOperator": "AND",
"outputTrue": "true",
"outputFalse": "false",
"outputOnChange": false,
"debounce": 2000,
"x": 650,
"y": 580,
"wires": []
},
{
"id": "5cd9cb9df0eb0e43",
"type": "function",
"z": "a5197c9e9e9aac37",
"g": "b70a836071e65056",
"name": "set_setpoint",
"func": "// ============================================================================\n// EV -> ESS GRID SETPOINT\n// Met Virtual Switch enable/disable\n//\n// Inputs:\n// 1) MQTT:\n// HomeServerLiveValues/EVCharger\n// payload { ActualPower, ... }\n//\n// 2) Victron Virtual Switch:\n// \"Charge EV from grid\"\n//\n// Werking:\n// - Switch UIT:\n// functie uitgeschakeld\n// Grid setpoint wordt direct DEFAULT_SETPOINT_W (-10 W)\n//\n// - Switch AAN:\n// EV > drempel:\n// Grid setpoint ≈ EV-laadvermogen\n//\n// EV niet actief:\n// Grid setpoint = -10 W\n//\n// Output:\n// msg.payload -> /Settings/CGwacs/AcPowerSetPoint\n// ============================================================================\n\n\n// ---------------- USER SETTINGS ----------------\n\nconst DEFAULT_SETPOINT_W = -10;\n\nconst EV_ACTIVE_THRESHOLD_W = 1000;\n\nconst ROUND_STEP_W = 100;\n\nconst MIN_DELTA_W = 300;\n\nconst MIN_INTERVAL_MS = 10000; // 10 seconden\n\n\n// ============================================================================\n// HERKEN INPUTTYPE\n// ============================================================================\n\nconst topic = msg.topic || \"\";\n\n\n// ---------------------------------------------------------------------------\n// VIRTUAL SWITCH INPUT\n// ---------------------------------------------------------------------------\n//\n// De state-output van de Victron Virtual Switch wordt hier gebruikt.\n//\n// We proberen de gangbare payloadvormen robuust af te handelen:\n// 0 / 1\n// false / true\n// \"0\" / \"1\"\n// object met state/value\n//\n\nif (!topic.includes(\"HomeServerLiveValues/EVCharger\")) {\n\n let raw = msg.payload;\n let enabled = null;\n\n if (typeof raw === \"boolean\") {\n\n enabled = raw;\n\n } else if (\n raw === 0 ||\n raw === 1 ||\n raw === \"0\" ||\n raw === \"1\"\n ) {\n\n enabled = Number(raw) === 1;\n\n } else if (raw && typeof raw === \"object\") {\n\n if (typeof raw.state === \"boolean\") {\n enabled = raw.state;\n }\n\n else if (\n raw.state === 0 ||\n raw.state === 1 ||\n raw.state === \"0\" ||\n raw.state === \"1\"\n ) {\n enabled = Number(raw.state) === 1;\n }\n\n else if (\n raw.value === 0 ||\n raw.value === 1 ||\n raw.value === \"0\" ||\n raw.value === \"1\"\n ) {\n enabled = Number(raw.value) === 1;\n }\n }\n\n\n // Payload niet herkenbaar\n if (enabled === null) {\n\n node.status({\n fill: \"red\",\n shape: \"ring\",\n text: \"Onbekende switch payload\"\n });\n\n return null;\n }\n\n\n const oldEnabled = context.get(\"enabled\");\n\n context.set(\"enabled\", enabled);\n\n\n // --------------------------------------------------------\n // SWITCH UIT\n // --------------------------------------------------------\n //\n // Meteen terug naar normaal ESS-setpoint.\n //\n\n if (enabled === false) {\n\n const lastSetpoint = context.get(\"lastSetpoint\");\n\n node.status({\n fill: \"grey\",\n shape: \"dot\",\n text: \"UIT | EV grid priority disabled\"\n });\n\n\n // Alleen schrijven indien nodig.\n if (lastSetpoint === DEFAULT_SETPOINT_W) {\n return null;\n }\n\n\n context.set(\"lastSetpoint\", DEFAULT_SETPOINT_W);\n context.set(\"lastSentTs\", Date.now());\n\n msg.payload = DEFAULT_SETPOINT_W;\n\n return msg;\n }\n\n\n // --------------------------------------------------------\n // SWITCH AAN\n // --------------------------------------------------------\n //\n // Niet meteen iets gokken.\n // Wacht op de eerstvolgende geldige EV-vermogensmeting.\n //\n\n if (enabled === true) {\n\n node.status({\n fill: \"blue\",\n shape: \"dot\",\n text: \"AAN | wacht op EV data\"\n });\n\n return null;\n }\n}\n\n\n// ============================================================================\n// MQTT EV INPUT\n// ============================================================================\n\nlet p = msg.payload;\n\nif (typeof p === \"string\") {\n\n try {\n p = JSON.parse(p);\n\n } catch (e) {\n\n node.status({\n fill: \"red\",\n shape: \"ring\",\n text: \"Ongeldige EV JSON\"\n });\n\n return null;\n }\n}\n\n\nif (!p || typeof p !== \"object\") {\n\n node.status({\n fill: \"red\",\n shape: \"ring\",\n text: \"Geen geldige EV payload\"\n });\n\n return null;\n}\n\n\n// ---------------- EV VERMOGEN ----------------\n\nconst evPowerW = Number(p.ActualPower);\n\nif (!Number.isFinite(evPowerW)) {\n\n node.status({\n fill: \"red\",\n shape: \"ring\",\n text: \"Geen geldige ActualPower\"\n });\n\n return null;\n}\n\n\n// Bewaar laatste EV-vermogen.\ncontext.set(\"lastEvPowerW\", evPowerW);\n\n\n// ============================================================================\n// CHECK ENABLE SWITCH\n// ============================================================================\n//\n// Fail-safe keuze:\n// Als Node-RED net opnieuw gestart is en we nog geen switchstatus hebben,\n// beschouwen we de functie als UIT.\n//\n// Dit voorkomt dat de EV-gridregeling onverwacht actief wordt.\n//\n\nconst enabled = context.get(\"enabled\") === true;\n\n\nif (!enabled) {\n\n const lastSetpoint = context.get(\"lastSetpoint\");\n\n\n node.status({\n fill: \"grey\",\n shape: \"ring\",\n text:\n `UIT | EV ${Math.round(evPowerW)}W | ` +\n `SP ${DEFAULT_SETPOINT_W}W`\n });\n\n\n // Zorg eventueel éénmalig dat we werkelijk terug op default staan.\n if (lastSetpoint !== DEFAULT_SETPOINT_W) {\n\n context.set(\"lastSetpoint\", DEFAULT_SETPOINT_W);\n context.set(\"lastSentTs\", Date.now());\n\n msg.payload = DEFAULT_SETPOINT_W;\n\n return msg;\n }\n\n\n return null;\n}\n\n\n// ============================================================================\n// FUNCTIE IS AAN\n// ============================================================================\n\nconst evActive =\n evPowerW > EV_ACTIVE_THRESHOLD_W;\n\n\nlet desiredSetpoint;\n\n\nif (evActive) {\n\n desiredSetpoint =\n Math.round(evPowerW / ROUND_STEP_W) *\n ROUND_STEP_W;\n\n} else {\n\n desiredSetpoint = DEFAULT_SETPOINT_W;\n}\n\n\n// ---------------- VORIGE STATUS ----------------\n\nconst lastSetpoint = context.get(\"lastSetpoint\");\n\nconst lastSentTs =\n context.get(\"lastSentTs\") || 0;\n\nconst now = Date.now();\n\n\n// ---------------- EERSTE KEER ----------------\n\nif (lastSetpoint === undefined) {\n\n context.set(\n \"lastSetpoint\",\n desiredSetpoint\n );\n\n context.set(\n \"lastSentTs\",\n now\n );\n\n msg.payload =\n desiredSetpoint;\n\n\n node.status({\n fill: evActive ? \"green\" : \"blue\",\n shape: \"dot\",\n text:\n `AAN | EV ${Math.round(evPowerW)}W → ` +\n `SP ${desiredSetpoint}W`\n });\n\n\n return msg;\n}\n\n\n// ============================================================================\n// EV AAN / UIT TRANSITIE\n// ============================================================================\n\nconst wasEvActive =\n lastSetpoint !== DEFAULT_SETPOINT_W;\n\nconst modeChanged =\n wasEvActive !== evActive;\n\n\nif (modeChanged) {\n\n context.set(\n \"lastSetpoint\",\n desiredSetpoint\n );\n\n context.set(\n \"lastSentTs\",\n now\n );\n\n msg.payload =\n desiredSetpoint;\n\n\n node.status({\n fill: evActive ? \"green\" : \"blue\",\n shape: \"dot\",\n text:\n evActive\n ? `AAN | EV actief ${Math.round(evPowerW)}W → SP ${desiredSetpoint}W`\n : `AAN | EV gestopt → SP ${DEFAULT_SETPOINT_W}W`\n });\n\n\n return msg;\n}\n\n\n// ============================================================================\n// EV NIET ACTIEF\n// ============================================================================\n\nif (!evActive) {\n\n node.status({\n fill: \"blue\",\n shape: \"ring\",\n text:\n `AAN | wacht | EV ${Math.round(evPowerW)}W | ` +\n `SP ${lastSetpoint}W`\n });\n\n return null;\n}\n\n\n// ============================================================================\n// EV ACTIEF\n// ============================================================================\n\nconst delta =\n Math.abs(\n desiredSetpoint -\n lastSetpoint\n );\n\n\n// ---------------- DEADBAND ----------------\n\nif (delta < MIN_DELTA_W) {\n\n node.status({\n fill: \"yellow\",\n shape: \"ring\",\n text:\n `AAN | hold Δ${delta}W | ` +\n `EV ${Math.round(evPowerW)}W | ` +\n `SP ${lastSetpoint}W`\n });\n\n return null;\n}\n\n\n// ---------------- RATE LIMIT ----------------\n\nconst elapsed =\n now -\n lastSentTs;\n\n\nif (elapsed < MIN_INTERVAL_MS) {\n\n const remaining =\n Math.ceil(\n (MIN_INTERVAL_MS - elapsed) /\n 1000\n );\n\n\n node.status({\n fill: \"blue\",\n shape: \"ring\",\n text:\n `AAN | wait ${remaining}s | ` +\n `EV ${Math.round(evPowerW)}W | ` +\n `target ${desiredSetpoint}W`\n });\n\n\n return null;\n}\n\n\n// ============================================================================\n// SETPOINT VERSTUREN\n// ============================================================================\n\ncontext.set(\n \"lastSetpoint\",\n desiredSetpoint\n);\n\ncontext.set(\n \"lastSentTs\",\n now\n);\n\n\nmsg.payload =\n desiredSetpoint;\n\n\nnode.status({\n fill: \"green\",\n shape: \"dot\",\n text:\n `AAN | EV ${Math.round(evPowerW)}W → ` +\n `Grid SP ${desiredSetpoint}W`\n});\n\n\nreturn msg;",
"outputs": 1,
"timeout": 0,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 390,
"y": 580,
"wires": [
[
"31c3c6a161e74b1d"
]
]
},
{
"id": "5bf393c9d5ac73bb",
"type": "debug",
"z": "a5197c9e9e9aac37",
"g": "b70a836071e65056",
"name": "check_output",
"active": false,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 400,
"y": 520,
"wires": []
},
{
"id": "28168acbee4d3057",
"type": "inject",
"z": "a5197c9e9e9aac37",
"g": "b70a836071e65056",
"name": "Manual inject Grid setpoint",
"props": [
{
"p": "payload"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "-10",
"payloadType": "num",
"x": 170,
"y": 580,
"wires": [
[
"5cd9cb9df0eb0e43"
]
]
},
{
"id": "e2524e1725f2d2ba",
"type": "victron-virtual-switch",
"z": "a5197c9e9e9aac37",
"g": "b70a836071e65056",
"name": "ChargeEVfromGrid",
"outputs": 2,
"switch_1_type": "1",
"switch_1_initial": 0,
"switch_1_label": "",
"switch_1_customname": "Charge EV from grid",
"switch_1_group": "NodeRED",
"switch_1_include_measurement": false,
"switch_1_rgb_color_wheel": false,
"switch_1_cct_wheel": false,
"switch_1_rgb_white_dimmer": false,
"switch_1_show_ui_input": 1,
"switch_1_passthrough_mode": "auto_only",
"x": 190,
"y": 640,
"wires": [
[],
[
"5cd9cb9df0eb0e43",
"f82c99b2a2f9ebc6"
]
]
},
{
"id": "f82c99b2a2f9ebc6",
"type": "debug",
"z": "a5197c9e9e9aac37",
"g": "b70a836071e65056",
"name": "check switch_output",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 420,
"y": 640,
"wires": []
},
{
"id": "bd1cf2989fb28ea0",
"type": "comment",
"z": "a5197c9e9e9aac37",
"g": "b70a836071e65056",
"name": "Charge EV from GRID (or not)",
"info": "",
"x": 160,
"y": 460,
"wires": []
},
{
"id": "9f574f2f6f5531cb",
"type": "mqtt-broker",
"name": "",
"broker": "192.168.30.10",
"port": "1883",
"clientid": "VictronGX",
"autoConnect": true,
"usetls": false,
"protocolVersion": "4",
"keepalive": "60",
"cleansession": true,
"autoUnsubscribe": true,
"birthTopic": "",
"birthQos": "0",
"birthRetain": "false",
"birthPayload": "",
"birthMsg": {},
"closeTopic": "",
"closeQos": "0",
"closeRetain": "false",
"closePayload": "",
"closeMsg": {},
"willTopic": "",
"willQos": "0",
"willRetain": "false",
"willPayload": "",
"willMsg": {},
"userProps": "",
"sessionExpiry": ""
},
{
"id": "053915b26369563c",
"type": "global-config",
"env": [],
"modules": {
"@victronenergy/node-red-contrib-victron": "1.7.22"
}
}
] |