-
Notifications
You must be signed in to change notification settings - Fork 19
/
scr-485.yaml
481 lines (435 loc) · 15.7 KB
/
scr-485.yaml
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
#################################################
##scr-485.yaml##
#################################################
## Add in your secrets.yaml
# wifi_ssid: 'iammeter'
# wifi_password: '12345678'
#################################################
esphome:
name: scr-485
friendly_name: scr_485
platformio_options:
# platform: https://github.com/platformio/platform-espressif32.git#feature/arduino-upstream
board_build.flash_mode: dio
## If you want to use auto mode please enable auto_mode_switch below, if you have battery you can enable with_battery
# on_boot:
# then:
# - switch.turn_on: auto_mode_switch
# - switch.turn_on: with_battery
esp32:
board: esp32-c3-devkitm-1
framework:
type: arduino
## Enable logging
# logger:
# level: DEBUG
## Enable Home Assistant API
api:
# encryption:
# key: "sgCuRJxLsye8p+zn6/0+TaijdeinqqayDoHTboX6TlQ="
reboot_timeout: 0s
ota:
# password: "279ec37f5bec6c4097b63bf9af78d375"
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
## Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Scr-485"
# password: "12345678"
web_server:
port: 80
# local: true
version: 2
time:
- platform: sntp
id: sntp_time
# servers: ntp5.aliyun.com #NTP server address
# timezone: Asia/Shanghai
captive_portal:
globals:
- id: auto_mode
type: bool
restore_value: true
initial_value: 'false'
- id: global_with_battery
type: bool
restore_value: true
initial_value: 'false'
- id: interval_flag
type: bool
restore_value: true
initial_value: 'false'
- id: non_pvp_flag
type: bool
restore_value: true
initial_value: 'false'
http_request:
useragent: esphome/device
timeout: 10s
id: http_request_data
status_led:
pin:
number: GPIO08
inverted: true
# light:
# - platform: status_led
# name: "Status Led"
# pin:
# number: GPIO08
# inverted: true
uart:
id: mod_bus
tx_pin: 0
rx_pin: 1
baud_rate: 9600
stop_bits: 1
modbus:
# flow_control_pin: 8
id: modbus1
modbus_controller:
- id: modbus_device
address: 0x01 ## address of the Modbus slave device on the bus
modbus_id: modbus1
setup_priority: -10
update_interval: 3s
sensor:
# - platform: modbus_controller
# modbus_controller_id: modbus_device
# name: "scr_485_adc_read"
# id: im1266_voltage
# register_type: holding
# address: 0x0001
# # unit_of_measurement: "ADC"
# value_type: U_WORD
# # accuracy_decimals: 4
# # filters:
# # - multiply: 0.0001
- platform: template
name: "Grid Power"
id: grid_power
unit_of_measurement: "W"
update_interval: 3s
# - platform: template
# name: "Grid Power REV"
# id: grid_power_rev
# unit_of_measurement: "W"
# update_interval: 3s
- platform: template
name: "Battery Power"
id: battery_power
unit_of_measurement: "W"
update_interval: 3s
# - platform: template
# name: "Battery Power REV"
# id: battery_power_rev
# unit_of_measurement: "W"
# update_interval: 3s
number:
- platform: modbus_controller
modbus_controller_id: modbus_device
id: scr_485_set_adc
name: "scr_485_set_adc"
address: 0x0002
value_type: U_WORD
# multiply: 1.0
min_value: 0
max_value: 4096
- platform: template
id: max_power
name: "Max Power"
optimistic: true
min_value: 1
max_value: 3600
step: 1
initial_value: 3600
restore_value: True
unit_of_measurement: "W"
mode: box
- platform: template
id: threshold
name: "Threshold"
optimistic: true
min_value: -200
max_value: 200
step: 0
initial_value: -20
restore_value: True
unit_of_measurement: "W"
mode: box
- platform: template
id: hysteresis
name: "Hysteresis"
optimistic: true
min_value: 0
max_value: 200
step: 0
initial_value: 50
restore_value: True
unit_of_measurement: "W"
mode: box
- platform: template
id: set_power
name: "Set Power"
unit_of_measurement: "W"
optimistic: true
min_value: 0
max_value: 3600
step: 1
# initial_value: 1500
restore_value: True
mode: box
on_value:
then:
- number.set:
id: scr_485_set_adc
value: !lambda |-
uint16_t input_val = id(set_power).state;
uint16_t output_val = 0;
uint16_t max_power_val = id(max_power).state;
if (input_val <= 0){
output_val = 0;
}else if (input_val <= max_power_val) {
output_val = 1450 + (input_val - 0) * (4096 - 1450) / (max_power_val - 0);
}else{
output_val = 4096;
}
return output_val;
- platform: template
id: auto_start_time
name: "1 Start Time"
optimistic: true
min_value: 0
max_value: 24
step: 1
initial_value: 9
restore_value: True
mode: box
- platform: template
id: auto_end_time
name: "2 End Time"
optimistic: true
min_value: 0
max_value: 24
step: 1
initial_value: 16
restore_value: True
mode: box
- platform: template
id: interval_seconds
name: "4 Interval Seconds"
unit_of_measurement: "s"
optimistic: true
min_value: 1
max_value: 30
step: 1
initial_value: 3
restore_value: True
mode: box
interval:
- interval: 1s
then:
lambda: |-
static int counter = 0;
counter++;
if (counter >= id(interval_seconds).state) {
counter = 0;
id(interval_flag) = true;
ESP_LOGI("main", "Interval triggered");
}
- interval: 1s
then:
## During non-PV power generation period, reset the set power to the maximum power
- if:
condition:
# lambda: 'return id(auto_mode);'
and:
- lambda: 'return id(interval_flag);'
- lambda: 'return id(auto_mode);'
- lambda: 'return !id(non_pvp_flag);'
- lambda: 'auto time = id(sntp_time).now(); return (time.hour == id(auto_end_time).state) && (time.minute == 0);'
then:
- lambda: |-
id(non_pvp_flag) = true;
id(set_power).publish_state(id(max_power).state);
ESP_LOGI("main", "non_pvp_flag");
- if:
condition:
# lambda: 'return id(auto_mode);'
and:
- lambda: 'return id(interval_flag);'
- lambda: 'return id(auto_mode);'
- lambda: 'return id(non_pvp_flag);'
- lambda: 'auto time = id(sntp_time).now(); return time.hour >= id(auto_start_time).state && time.hour < id(auto_end_time).state;'
then:
- lambda: |-
id(non_pvp_flag) = false;
ESP_LOGI("main", "pvp_flag");
## Main regulation logic
- if:
condition:
# lambda: 'return id(auto_mode);'
and:
- lambda: 'return id(interval_flag);'
- lambda: 'return id(auto_mode);'
- lambda: 'auto time = id(sntp_time).now(); return time.hour >= id(auto_start_time).state && time.hour < id(auto_end_time).state;'
then:
- lambda: |-
id(interval_flag) = false;
ESP_LOGI("main", "interval_flag");
## with Battery
- if:
condition:
lambda: 'return id(global_with_battery);'
then:
- http_request.get: ## IP of the meter that measures the battery power.
url: "http://10.10.30.33/monitorjson"
headers:
Content-Type: "application/json"
Authorization: 'Basic YWRtaW46YWRtaW4='
verify_ssl: false
on_response:
then:
- logger.log:
format: "Response status: %d, Duration: %u ms"
args:
- status_code
- duration_ms
- lambda: |-
json::parse_json(id(http_request_data).get_string(), [](JsonObject root) {
// WEM3080
if (root.containsKey("Data") && root["Data"].is<JsonArray>() && root["Data"].size() > 2) {
int32_t tmp_battery_power = root["Data"][2];
//tmp_battery_power = -tmp_battery_power; //for test
id(battery_power).publish_state(tmp_battery_power);
// int32_t tmp_battery_power_rev = -tmp_battery_power;
// id(battery_power_rev).publish_state(tmp_battery_power_rev);
}
});
- http_request.get: ## IP of the meter that measures the grid power.
url: "http://10.10.30.38/monitorjson"
headers:
Content-Type: "application/json"
Authorization: 'Basic YWRtaW46YWRtaW4='
verify_ssl: false
on_response:
then:
- logger.log:
format: "Response status: %d, Duration: %u ms"
args:
- status_code
- duration_ms
- lambda: |-
json::parse_json(id(http_request_data).get_string(), [](JsonObject root) {
// WEM3080
if (root.containsKey("Data") && root["Data"].is<JsonArray>() && root["Data"].size() > 2) {
int32_t tmp_grid_power = root["Data"][2];
id(grid_power).publish_state(tmp_grid_power);
// int32_t tmp_grid_power_rev = -tmp_grid_power;
// id(grid_power_rev).publish_state(tmp_grid_power_rev);
int32_t tmp_threshold = id(threshold).state;
int32_t tmp_hysteresis = id(hysteresis).state;
int32_t tmp_set_power = id(set_power).state;
if ((id(global_with_battery))&&id(battery_power).state<0) {
tmp_grid_power = tmp_grid_power - id(battery_power).state;
}
if (tmp_grid_power < tmp_threshold - tmp_hysteresis) {
tmp_set_power += abs(tmp_grid_power - tmp_threshold);
if (tmp_set_power < 0) {
tmp_set_power = 0;
}
if (tmp_set_power > id(max_power).state) {
tmp_set_power = id(max_power).state;
}
if (id(auto_mode)) {
id(set_power).publish_state(tmp_set_power);
}
}
if (tmp_grid_power > tmp_threshold + tmp_hysteresis) {
tmp_set_power -= abs(tmp_grid_power - tmp_threshold);
if (tmp_set_power < 0) {
tmp_set_power = 0;
}
if (tmp_set_power > id(max_power).state) {
tmp_set_power = id(max_power).state;
}
if (id(auto_mode)) {
id(set_power).publish_state(tmp_set_power);
}
}
}
// WEM3080T/WEM3050T/WEM3046T
if (root.containsKey("Datas") && root["Datas"].is<JsonArray>() && root["Datas"].size() > 2) {
// If your solar PV uses phase A, it is ["Datas"][0][2], if it is phase B, it is ["Datas"][1][2], if it is phase C, it is ["Datas"] [2][2]
int32_t tmp_grid_power = root["Datas"][1][2];
//tmp_grid_power = -tmp_grid_power; //for test
id(grid_power).publish_state(tmp_grid_power);
// int32_t tmp_grid_power_rev = -tmp_grid_power;
// id(grid_power_rev).publish_state(tmp_grid_power_rev);
int32_t tmp_threshold = id(threshold).state;
int32_t tmp_hysteresis = id(hysteresis).state;
int32_t tmp_set_power = id(set_power).state;
if ((id(global_with_battery))&&id(battery_power).state<0) {
tmp_grid_power = tmp_grid_power - id(battery_power).state;
}
if (tmp_grid_power < tmp_threshold - tmp_hysteresis) {
tmp_set_power += abs(tmp_grid_power - tmp_threshold);
if (tmp_set_power < 0) {
tmp_set_power = 0;
}
if (tmp_set_power > id(max_power).state) {
tmp_set_power = id(max_power).state;
}
if (id(auto_mode)) {
id(set_power).publish_state(tmp_set_power);
}
}
if (tmp_grid_power > tmp_threshold + tmp_hysteresis) {
tmp_set_power -= abs(tmp_grid_power - tmp_threshold);
if (tmp_set_power < 0) {
tmp_set_power = 0;
}
if (tmp_set_power > id(max_power).state) {
tmp_set_power = id(max_power).state;
}
if (id(auto_mode)) {
id(set_power).publish_state(tmp_set_power);
}
}
}
});
switch:
- platform: template
name: "3 Auto Mode Switch"
id: auto_mode_switch
restore_mode: DISABLED
turn_on_action:
- globals.set:
id: auto_mode
value: 'true'
turn_off_action:
- globals.set:
id: auto_mode
value: 'false'
# - number.set:
# id: set_power
# value: '0.0'
lambda: |-
return id(auto_mode);
- platform: template
name: "3 With Battery"
id: with_battery
restore_mode: DISABLED
turn_on_action:
- globals.set:
id: global_with_battery
value: 'true'
turn_off_action:
- globals.set:
id: global_with_battery
value: 'false'
lambda: |-
return id(global_with_battery);
button:
- platform: restart
name: "Restart"