-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathb2500.yaml
1246 lines (1171 loc) · 44.2 KB
/
b2500.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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
esphome:
name: b2500
esp32:
board: wemos_d1_mini32 #esp32dev
framework:
type: esp-idf
version: recommended
# type: arduino
# Enable logging
logger:
level: INFO
# level: DEBUG
# baud_rate: 0
# Enable Home Assistant API
api:
encryption:
key: "nZnNzvZXYurNIShgY/GSIgZQsnGEeZqSTcOpjowA0TM="
ota:
password: "5de5c53a13568a65c99c37cd946dcef8"
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
reboot_timeout: 0s
fast_connect: True
# Enable fallback hotspot (captive portal) in case wifi connection fails
# ap:
# ssid: "B2500 Fallback Hotspot"
# password: "pNLbPnPs0ldL"
#captive_portal:
mqtt:
id: mqtt_client
broker: !secret mqtt_broker
port: !secret mqtt_port
username: !secret mqtt_user
password: !secret mqtt_password
discovery: false
reboot_timeout: 60s
topic_prefix: b2500
log_topic: b2500/debug
time:
- platform: sntp
id: sntp_time
on_time:
# Every 60 seconds
- seconds: /60
then:
- script.execute: ble_process
- script.wait: ble_process
#- script.execute: power_zero
#- button.press: query_info3
globals:
- id: ble_1_connected
type: bool
initial_value: '0'
- id: ble_1_initialized
type: bool
initial_value: '0'
- id: cmd30_xor_last_1
type: int
initial_value: '0'
- id: cmd30_xor_last_2
type: int
initial_value: '0'
- id: internal_console_dbg
type: bool
initial_value: '0'
- id: internal_console_hexdump
type: bool
initial_value: '1'
esp32_ble_tracker:
ble_client:
- mac_address: !secret hm2500_1_mac
id: bc2500_1
on_connect:
then:
- globals.set:
id: ble_1_connected
value: '1'
- binary_sensor.template.publish:
id: bool_ble_ok_1
state: ON
- script.execute:
id: ble_set_time
ble_device_nr: 1
- script.wait: ble_set_time
- script.execute:
id: ble_set_time
ble_device_nr: 1
- script.wait: ble_set_time
on_disconnect:
then:
- binary_sensor.template.publish:
id: bool_ble_ok_1
state: OFF
- globals.set:
id: ble_1_connected
value: '0'
- globals.set:
id: ble_1_initialized
value: '0'
button:
- platform: restart
id: controller_restart
name: "Restart Controller"
number:
- platform: template
name: "D1-52: Entladeschwelle"
id: sensor_discharge_treshold_1
state_topic: b2500/1/battery/discharge_treshold
command_topic: b2500/1/battery/discharge_treshold/set
optimistic: True
min_value: 1
max_value: 500
step: 1
restore_value: True
on_value:
- script.execute:
id: ble_set_discharge_treshold
ble_device_nr: 1
discharge: !lambda return x;
- platform: template
name: "D1-53: DOD"
state_topic: b2500/1/battery/dod
command_topic: b2500/1/battery/dod/set
id: sensor_dod_1
optimistic: True
min_value: 10
max_value: 100
step: 1
restore_value: True
on_value:
- script.execute:
id: ble_set_dod
ble_device_nr: 1
dod: !lambda return x;
### power zero
# - platform: template
# name: "MQTT: opendtu set limit"
# id: mqtt_opendtu_limit
# internal: False
# state_topic: !secret mqtt_opendtu_limit_cmd
# command_topic: !secret mqtt_opendtu_limit_state
# optimistic: True
# min_value: 1
# max_value: 75
# step: 1
# restore_value: True
# - platform: template
# name: "MQTT: opendtu set limit max"
# id: mqtt_opendtu_limit_max
# internal: False
# optimistic: True
# min_value: 1
# max_value: 75
# step: 1
# restore_value: True
switch:
- platform: template
id: switch_powerout_1_1
name: "D1-01: Power Out 1"
state_topic: b2500/1/power1/enabled
command_topic: b2500/1/power1/enabled/set
optimistic: True
assumed_state: True
on_turn_on:
then:
- switch.turn_on: switch_powerout_1_1
- script.execute:
id: ble_powerout
ble_device_nr: 1
on_turn_off:
then:
- switch.turn_off: switch_powerout_1_1
- script.execute:
id: ble_powerout
ble_device_nr: 1
- platform: template
id: switch_powerout_1_2
name: "D1-02: Power Out 2"
state_topic: b2500/1/power2/enabled
command_topic: b2500/1/power2/enabled/set
optimistic: True
assumed_state: True
on_turn_on:
then:
- switch.turn_on: switch_powerout_1_2
- script.execute:
id: ble_powerout
ble_device_nr: 1
on_turn_off:
then:
- switch.turn_off: switch_powerout_1_2
- script.execute:
id: ble_powerout
ble_device_nr: 1
- platform: template
id: switch_pv2_passthrough_1
name: "D1-03: PV2 Passtrough"
state_topic: b2500/1/pv2/passtrough
command_topic: b2500/1/pv2/passtrough/set
optimistic: True
assumed_state: True
on_turn_on:
then:
- script.execute:
id: ble_passthrough
ble_device_nr: 1
switch_cmd: 0
on_turn_off:
then:
- script.execute:
id: ble_passthrough
ble_device_nr: 1
switch_cmd: 1
- platform: template
id: switch_debug_hexdump
name: "INTERNAL:DEBUG HEXDUMP"
optimistic: True
#assumed_state: True
# - platform: template
# id: switch_opendtu_limit
# name: "MQTT: opendtu - zero power"
# optimistic: True
# #assumed_state: True
text:
- platform: template
name: "A1-t01 - Device Type"
id: txt_A01_1
state_topic: b2500/1/device/type
optimistic: true
max_length: 30
mode: text
- platform: template
name: "A1-t02 - Device ID"
state_topic: b2500/1/device/id
id: txt_A02_1
optimistic: true
max_length: 30
mode: text
- platform: template
name: "A1-t03 - MAC"
id: txt_A03_1
state_topic: b2500/1/device/ble_mac
optimistic: true
max_length: 30
mode: text
- platform: template
name: "A1-t04 - SSID"
id: txt_A11_1
state_topic: b2500/1/device/wifi_ssid
optimistic: true
max_length: 30
mode: text
- platform: template
name: "A1-t56: Szene"
id: txt_scene_1
state_topic: b2500/1/device/scene
optimistic: true
max_length: 32
mode: text
- platform: template
name: "A1-t57: Region"
id: txt_region_1
state_topic: b2500/1/device/region
optimistic: true
max_length: 8
mode: text
binary_sensor:
- platform: template
name: "D1-i01: PV 1 - Aktiv"
id: bool_pv_active_1_1
state_topic: b2500/1/pv1/active
- platform: template
name: "D1-i11: PV 2 - Aktiv"
id: bool_pv_active_1_2
state_topic: b2500/1/pv2/active
- platform: template
name: "D1-i02: PV 1 - Transparent"
id: bool_pv_transparent_1_1
state_topic: b2500/1/pv1/transparent
- platform: template
name: "D1-i11: PV 2 - Transparent"
id: bool_pv_transparent_1_2
state_topic: b2500/1/pv2/transparent
- platform: template
name: "D1-i54: Wifi Connected (?)"
id: bool_wifi_ok_1
state_topic: b2500/1/device/wifi_ok
- platform: template
name: "D1-i55: MQTT1 Connected"
id: bool_mqtt1_ok_1
state_topic: b2500/1/device/mqtt_ok
- platform: template
name: "D1-i58: BLE Connected"
id: bool_ble_ok_1
state_topic: b2500/1/device/ble_ok
- platform: template
name: "D1-i21: Ausgang 1 - Aktiv"
id: bool_power_active_1_1
state_topic: b2500/1/power1/active
- platform: template
name: "D1-i31: Ausgang 2 - Aktiv"
id: bool_power_active_1_2
state_topic: b2500/1/power2/active
- platform: template
name: "D1-i40: Erweiterung 1 - angeschlossen"
id: bool_extern_connected_1_1
state_topic: b2500/1/extern1/connected
- platform: template
name: "D1-i41: Erweiterung 2 - angeschlossen"
id: bool_extern_connected_1_2
state_topic: b2500/1/extern2/connected
sensor:
- platform: template
name: "D1-i05: PV 1 - Leistung"
id: sensor_pv_power_in_1_1
state_topic: b2500/1/pv1/power
accuracy_decimals: 0
- platform: template
name: "D1-i15: PV 2 - Leistung"
id: sensor_pv_power_in_1_2
state_topic: b2500/1/pv2/power
accuracy_decimals: 0
- platform: template
name: "D1-i50: Füllstand der Batterie in Prozent"
id: sensor_bat_remain_1
state_topic: b2500/1/battery/remaining_percent
accuracy_decimals: 0
- platform: template
name: "D1-i51: Füllstand der Batterie in Wh"
id: sensor_bat_capacity_1
state_topic: b2500/1/battery/remaining_capacity
accuracy_decimals: 0
- platform: template
name: "D1-i25: Ausgang 1 - Leistung"
id: sensor_power_out_1_1
state_topic: b2500/1/power1/power
accuracy_decimals: 0
- platform: template
name: "D1-i35: Ausgang 2 - Leistung"
id: sensor_power_out_1_2
state_topic: b2500/1/power2/power
accuracy_decimals: 0
- platform: template
name: "A1-t59: Geräte Version"
id: sensor_device_version_1
state_topic: b2500/1/device/fw_version
accuracy_decimals: 2
- platform: ble_client
ble_client_id: bc2500_1
internal: True
type: characteristic
name: "infoX2a"
id: infoX2a
service_uuid: 'ff00'
characteristic_uuid: 'ff02'
notify: True
lambda: |-
std::vector<char> tData;
for (auto b : x) { tData.push_back(b); }
id(ble_notify_parse).execute(1,tData);
return (float)x[0];
### power zero - mqtt grid power sensor ( any who publish the grid power to mqtt - defined in secrets,yaml )
# - platform: mqtt_subscribe
# name: "MQTT: Grid Power"
# id: mqtt_grid_power
# topic: !secret mqtt_grid_power
# on_value:
# then:
# - script.execute: power_zero
###test
- platform: ble_client
ble_client_id: bc2500_1
internal: True
type: characteristic
name: "infoX6a"
id: infoX6a
service_uuid: 'ff00'
characteristic_uuid: 'ff06'
notify: True
lambda: |-
std::vector<char> tData;
for (auto b : x) { tData.push_back(b); }
id(ble_notify_parse_test).execute(1,tData);
return (float)x[0];
script:
# ble communication
#
# action ( 00f1 )
#
# head = 0x73
# length = len(paket)
# cntl = 0x23
# cmd = 0x02 set Region 1Byte (0x00 = EU / 0x01 = China / 0x02 = Non-EU)
# = 0x03 runtimeInfo 1Byte (0x01)
# = 0x04 DeviceInfo 1Byte (0x01)
# = 0x0B DOD 1Byte (0-100)
# = 0x0C Entladeschwelle 2Byte (0-500)
# = 0x0D PV2-Passtrough 1Byte (0x00 on / 0x01 off)
# = 0x0E PowerOut 1Byte (0x00 1-2 off / 0x01 1 on / 0x02 2 on / 0x03 1-2 on)
#
# = 0x05 Wifi-Config xByte ( ssid<.,.>pwd )
# = 0x08 Wifi-State 1Byte (0x01) ????
#
# q&d c&p - more details will be added, maybe ...
# = 0x14 set AWS MQTT xByte ( url<.,.>Port ) ....
# = 0x60 set MQTT Certs xByte ( 0x00 = client.key / 0x01 = client.crt / 0x02 = ca.crt + cert len )
# = 0x61 trans MQTT Certs xByte ( jeweils 128bytes des certs )
# = 0x62 end MQTT Certs xByte ( )
#
# testing / notes
# = 0x01 Debug ?!?! 1Byte (0x00 = off / 0x01 = on) - enables QBLEGATTSNOTIFY notify 1 / 81 ( entspricht ~ runtimeinfo )
# = 0x06
# = 0x07
# = 0x09
# = 0x0A
# = 0x0E HW-RESET ????? / send before head 0XAA ( deactivate output ??? )
# = 0x0F new in fw 131 1Byte ( 0x01 )
#
# = 0x30 found in logs ... unknown parm 0x01 ???? answers since fw 131
# = 0x14 maybe not for mqtt ... set localtime ??? for auth/certs/challenge requests ????? ( query/set wifi depends ????)
#
# data = xx xx xx xx xx xx .... / depends on cmd
# crc = xor len(paket) - 1
#
#
# responses ( ff02 ):
#
# head = 0x73
# length = len(paket)
# cntl = 0x23
# cmd = cmd
# data = xx xx xx xx xx ....
#
#
#
################ maybe direct for arm
# send ( ff01 )
#
# head1 = 0xAA
# head2(?) = 0x55 ( not length ?!?!? )
# cmd = 1x / 2x / 3x ( flash - 30 "open"/ 31 - write / 32 "close" ) / 5x
# data = xx xx xx xx xx ....
# crc = xor len(paket) -1
#
################ maybe direct for bms
# send/receive ( ff06 )
#
# head = 0xAA
# len = 0x05/0x03
# data = xx xx xxx ( xx xx )
# crc = x1 + x2 + ... + xn
#
#
#
# -> aa 05 01 00 01 01 00 08
# <- aa 01 00 01
#
# -> aa 05 01 00 01 00 00 07
# <- aa 01 00 01
#
- id: ble_command_simple
parameters:
ble_device_nr: int
ble_cmd: int
ble_cmd_parm: int
then:
- logger.log:
format: "ble command parse: %i [%i] %i"
args: [ 'ble_device_nr','ble_cmd','ble_cmd_parm' ]
- if:
condition:
lambda: 'return (ble_device_nr == 1);'
then:
- ble_client.ble_write:
id: bc2500_1
#service_uuid: 'ff00'
#characteristic_uuid: 'ff01'
service_uuid: 0000ff00-0000-1000-8000-00805f9b34fb
characteristic_uuid: 0000ff01-0000-1000-8000-00805f9b34fb
value: !lambda |-
std::vector<unsigned char> rdat1{ 0x73,0x06,0x23,(unsigned char)ble_cmd};
if (ble_cmd == 0x0C) {
rdat1.push_back((uint8_t)((ble_cmd_parm >> 0) & 0xFF));
rdat1.push_back((uint8_t)((ble_cmd_parm >> 8) & 0xFF));
} else {
rdat1.push_back((unsigned char)ble_cmd_parm);
}
int rlen = rdat1.size();
rdat1.at(1) = rlen+1;
int rxor = 0;
for (int i=0;i<rlen;i++) {
rxor = rxor ^ rdat1[i];
}
rdat1.push_back(rxor);
if (id(internal_console_dbg)) {
for (auto b : rdat1) {
ESP_LOGD("COMMAND", "%x - %i - %c", b,b,b);
}
}
return rdat1;
- id: ble_command_string
parameters:
ble_device_nr: int
ble_cmd: int
ble_cmd_parm: string
then:
- logger.log:
format: "ble command parse: %i [%i]"
args: [ 'ble_device_nr','ble_cmd']
- if:
condition:
lambda: 'return (ble_device_nr == 1);'
then:
- ble_client.ble_write:
id: bc2500_1
#service_uuid: 'ff00'
#characteristic_uuid: 'ff01'
service_uuid: 0000ff00-0000-1000-8000-00805f9b34fb
characteristic_uuid: 0000ff01-0000-1000-8000-00805f9b34fb
value: !lambda |-
std::vector<unsigned char> rdat1{ 0x73,0x06,0x23,(unsigned char)ble_cmd};
for (auto b : ble_cmd_parm) {
rdat1.push_back((unsigned char)b);
}
int rlen = rdat1.size();
rdat1.at(1) = rlen+1;
int rxor = 0;
for (int i=0;i<rlen;i++) {
rxor = rxor ^ rdat1[i];
}
rdat1.push_back(rxor);
if (id(internal_console_dbg)) {
for (auto b : rdat1) {
ESP_LOGD("COMMAND", "%x - %i - %c", b,b,b);
}
}
return rdat1;
- id: ble_set_time
parameters:
ble_device_nr: int
then:
- if:
condition:
lambda: 'return (ble_device_nr == 1);'
then:
- ble_client.ble_write:
id: bc2500_1
#service_uuid: 'ff00'
#characteristic_uuid: 'ff01'
service_uuid: 0000ff00-0000-1000-8000-00805f9b34fb
characteristic_uuid: 0000ff01-0000-1000-8000-00805f9b34fb
value: !lambda |-
std::vector<unsigned char> rdat1{ 0x73,0x0d,0x23,0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3c, 0x00 };
auto time = id(sntp_time).now();
rdat1.at(4) = time.year - 1900;
rdat1.at(5) = time.month;
rdat1.at(6) = time.day_of_month;
rdat1.at(7) = time.hour;
rdat1.at(8) = time.minute;
rdat1.at(9) = time.second + 1;
int rlen = rdat1.size();
rdat1.at(1) = rlen+1;
int rxor = 0;
for (int i=0;i<rlen;i++) {
rxor = rxor ^ rdat1[i];
}
rdat1.push_back(rxor);
if (id(internal_console_dbg)) {
for (auto b : rdat1) {
ESP_LOGD("COMMAND", "%x - %i - %c", b,b,b);
}
}
return rdat1;
- globals.set:
id: ble_1_initialized
value: '1'
- id: ble_command_raw_06
parameters:
ble_device_nr: int
ble_cmd_parm: char[]
then:
- logger.log:
format: "ble command parse (raw): %i"
args: [ 'ble_device_nr']
- if:
condition:
lambda: 'return (ble_device_nr == 1);'
then:
- ble_client.ble_write:
id: bc2500_1
#service_uuid: 'ff00'
#characteristic_uuid: 'ff06'
service_uuid: 0000ff00-0000-1000-8000-00805f9b34fb
characteristic_uuid: 0000ff06-0000-1000-8000-00805f9b34fb
value: !lambda |-
std::vector<unsigned char> rdat1;
for (auto b : ble_cmd_parm) {
rdat1.push_back((unsigned char)b);
}
/*
int rlen = rdat1.size();
rdat1.at(1) = rlen+1;
int rxor = 0;
for (int i=0;i<rlen;i++) {
rxor = rxor ^ rdat1[i];
}
rdat1.push_back(rxor);
*/
//if (id(internal_console_dbg)) {
for (auto b : rdat1) {
ESP_LOGD("COMMAND raw", "%x - %i - %c", b,b,b);
}
//}
return rdat1;
- id: ble_process
then:
- if:
condition:
- lambda: 'return (id(ble_1_connected) && id(ble_1_initialized));'
then:
- script.execute:
id: ble_runtime_query
ble_device_nr: 1
- script.wait: ble_runtime_query
### query cmd30 if firmware > 1.30
- if:
condition:
- lambda: 'return (id(sensor_device_version_1).state * 100 > 130);'
then:
- script.execute:
id: ble_runtime_query30
ble_device_nr: 1
- script.wait: ble_runtime_query30
- script.execute:
id: ble_runtime_query0F
ble_device_nr: 1
- script.wait: ble_runtime_query0F
### query deviceinfo if empty
- if:
condition:
- lambda: 'return (id(txt_A03_1).state == "");'
then:
- script.execute:
id: ble_command_simple
ble_device_nr: 1
ble_cmd: 0x04
ble_cmd_parm: 0x01
- script.wait: ble_command_simple
- id: ble_runtime_query
parameters:
ble_device_nr: int
then:
- logger.log:
format: "runtime query: %i"
args: [ 'ble_device_nr' ]
- script.execute:
id: ble_command_simple
ble_device_nr: !lambda return ble_device_nr;
ble_cmd: 0x03
ble_cmd_parm: 0x01
- script.wait: ble_command_simple
- id: ble_runtime_query30
parameters:
ble_device_nr: int
then:
- logger.log:
format: "runtime query 30: %i"
args: [ 'ble_device_nr' ]
- script.execute:
id: ble_command_simple
ble_device_nr: !lambda return ble_device_nr;
ble_cmd: 0x30
ble_cmd_parm: 0x01
- script.wait: ble_command_simple
- id: ble_runtime_query0F
parameters:
ble_device_nr: int
then:
- logger.log:
format: "runtime query 0F: %i"
args: [ 'ble_device_nr' ]
- script.execute:
id: ble_command_simple
ble_device_nr: !lambda return ble_device_nr;
ble_cmd: 0x0F
ble_cmd_parm: 0x01
- script.wait: ble_command_simple
- id: ble_powerout
parameters:
ble_device_nr: int
then:
- lambda: |-
int ble_cmd_t = 0x00;
if ( ble_device_nr == 1 ) {
if ( ! id(switch_powerout_1_1).state && ! id(switch_powerout_1_2).state ) { ble_cmd_t = 0x00; }
if ( id(switch_powerout_1_1).state && ! id(switch_powerout_1_2).state ) { ble_cmd_t = 0x01; }
if ( ! id(switch_powerout_1_1).state && id(switch_powerout_1_2).state ) { ble_cmd_t = 0x02; }
if ( id(switch_powerout_1_1).state && id(switch_powerout_1_2).state ) { ble_cmd_t = 0x03; }
}
id(ble_command_simple).execute(ble_device_nr,0x0E,ble_cmd_t);
if (ble_cmd_t == 0x00) { ESP_LOGD("set_power_out", "Device %i - %s", ble_device_nr,"1 OFF / 2 OFF"); }
if (ble_cmd_t == 0x01) { ESP_LOGD("set_power_out", "Device %i - %s", ble_device_nr,"1 ON / 2 OFF"); }
if (ble_cmd_t == 0x02) { ESP_LOGD("set_power_out", "Device %i - %s", ble_device_nr,"1 OFF / 2 ON"); }
if (ble_cmd_t == 0x03) { ESP_LOGD("set_power_out", "Device %i - %s", ble_device_nr,"1 ON / 2 ON"); }
- id: ble_passthrough
parameters:
ble_device_nr: int
switch_cmd: bool
then:
- logger.log:
format: "PV2 Passthrough %i : %i"
args: [ble_device_nr,switch_cmd]
- script.execute:
id: ble_command_simple
ble_device_nr: !lambda return ble_device_nr;
ble_cmd: 0x0D
ble_cmd_parm: !lambda return switch_cmd;
- id: ble_set_dod
parameters:
ble_device_nr: int
dod: int
then:
- logger.log:
format: "set DOD: %i"
args: [ 'dod' ]
- if:
condition:
lambda: 'return ( dod <= 100 && dod >= 10);'
then:
- script.execute:
id: ble_command_simple
ble_device_nr: !lambda return ble_device_nr;
ble_cmd: 0x0B
ble_cmd_parm: !lambda return dod;
- id: ble_set_discharge_treshold
parameters:
ble_device_nr: int
discharge: int
then:
- logger.log:
format: "set discharge level: %i"
args: [ 'discharge' ]
- if:
condition:
lambda: 'return ( discharge <= 500 && discharge >= 1);'
then:
- script.execute:
id: ble_command_simple
ble_device_nr: !lambda return ble_device_nr;
ble_cmd: 0x0C
ble_cmd_parm: !lambda return discharge;
- id: ble_notify_parse_test
parameters:
ble_device_nr: int
x: char[]
then:
- logger.log:
format: "runtime parse: %i"
args: [ 'ble_device_nr' ]
- lambda: |-
//if (id(internal_console_dbg)) {
ESP_LOGD("parse test", "x[3] = %i", x[3]);
for (auto b : x) {
ESP_LOGD("data test", "%.2x \t %i \t %c", b,b,b);
}
//}
- id: ble_notify_parse
parameters:
ble_device_nr: int
x: char[]
then:
- logger.log:
format: "runtime parse: %i"
args: [ 'ble_device_nr' ]
- lambda: |-
ESP_LOGD("notify_parse", "Device: %i", ble_device_nr);
if (id(internal_console_dbg)) {
ESP_LOGD("parse", "x[3] = %i", x[3]);
for (auto b : x) {
ESP_LOGD("data", "%.2x \t %i \t %c", b,b,b);
}
}
if (id(switch_debug_hexdump).state == true) {
ESP_LOG_BUFFER_HEXDUMP("hexdump", &x[0], x.size(), ESP_LOG_ERROR);
}
if ((std::count (x.begin(), x.end(), '_') == 16) || (std::count (x.begin(), x.begin() + 10, '_') == 3))
{
ESP_LOGD("main", "Data: cmd 0x0F");
int pos = 0;
int soc = 0;
int t1 = 0;
int t2 = 0;
float cv = 0.0;
float cmin = std::numeric_limits<float>::max();
float cmax = std::numeric_limits<float>::min();
float ct = 0.0;
int found = -1;
char delimiter = '_';
std::string xstr;
std::vector<float> cellV;
xstr.assign(x.begin(), x.end()); // copy values from vector into string xstr, deep copy
xstr = xstr + delimiter; // append delimiter to xstr
found = xstr.find(delimiter); // search for position of the first delimiter
while (found != -1) // loop until no more delimiter found
{
if(pos == 0) soc = atoi( xstr.substr(0, found).c_str()); // pos 0 don't care
if(pos == 1) t1 = atoi( xstr.substr(0, found).c_str()); // pos 1 get int value of temperature sensor 1
if(pos == 2) t2 = atoi( xstr.substr(0, found).c_str()); // pos 2 get int value of temperature sensor 2
if((pos >= 3) && (pos <= 16)) // pos 3-16 parse pos for the 14 cell voltages
{
ct = atof( xstr.substr(0, found).c_str()); // get float value of pos x
cellV.push_back(ct);
//ESP_LOGD("cell voltage", ct.c_str());
cv += ct; // add actual value to var cv
if(ct > cmax) cmax = ct; // check for higher value as stored in cmax
if(ct < cmin) cmin = ct; // check for lower value as stored in cmin
}
xstr.erase(xstr.begin(), xstr.begin() + found + 1); // remove parsed string part
found = xstr.find(delimiter); // find next delimiter
pos++; // increment pos
}
/* calculate SoC from cell voltages
cell empty = 3.0 Volt = 0% SoC
cell full = 3.5 Volt = 100% SoC
*/
// float soccalc = (cv/14000 - 3.0) * 200;
float lowlimit = 3.0; // low voltage limit
float highlimit = 3.5; // high voltage limit
float soccalc = 100*((cv/14000)
- highlimit)/(highlimit - lowlimit) + 100; // equation of line with two points (0,lowlimit) (100,highlimit)
ESP_LOGD("cellVoltage","soc: %i, temp1: %i, temp2: %i",soc,t1,t2);
ESP_LOGD("cellVoltage","cell01: %.f, cell 02: %.f, cell 03: %.f, cell 04: %.f", cellV[0], cellV[1], cellV[2], cellV[3]);
ESP_LOGD("cellVoltage","cell05: %.f, cell 06: %.f, cell 07: %.f, cell 08: %.f", cellV[4], cellV[5], cellV[6], cellV[7]);
ESP_LOGD("cellVoltage","cell09: %.f, cell 10: %.f, cell 11: %.f, cell 12: %.f", cellV[8], cellV[9], cellV[10], cellV[11]);
ESP_LOGD("cellVoltage","cell13: %.f, cell 14: %.f", cellV[12], cellV[13]);
char mtopic[48];
for (int i=0; i<14; i++) {
snprintf(mtopic, 48,"b2500/%i/battery/cells/%02d/voltage",ble_device_nr,i+1);
//ESP_LOGD("cellVoltageX","%s : %f", mtopic, cellV[i]);
id(mqtt_client).publish(mtopic,to_string(cellV[i]/1000));
}
snprintf(mtopic, 48,"b2500/%i/battery/cells/sum/voltage",ble_device_nr);
id(mqtt_client).publish(mtopic,to_string(cv/1000));
snprintf(mtopic, 48,"b2500/%i/battery/cells/sum/cmin",ble_device_nr);
id(mqtt_client).publish(mtopic,to_string(cmin/1000));
snprintf(mtopic, 48,"b2500/%i/battery/cells/sum/cmax",ble_device_nr);
id(mqtt_client).publish(mtopic,to_string(cmax/1000));
snprintf(mtopic, 48,"b2500/%i/battery/cells/sum/cavg",ble_device_nr);
id(mqtt_client).publish(mtopic,to_string(cv/14000));
snprintf(mtopic, 48,"b2500/%i/battery/cells/sum/cdiff",ble_device_nr);
id(mqtt_client).publish(mtopic,to_string((cmax-cmin)/1000));
snprintf(mtopic, 48,"b2500/%i/battery/cells/sum/soccalc",ble_device_nr);
id(mqtt_client).publish(mtopic,to_string(soccalc));
snprintf(mtopic, 48,"b2500/%i/battery/temp1",ble_device_nr);
id(mqtt_client).publish(mtopic,to_string(t1));
snprintf(mtopic, 48,"b2500/%i/battery/temp2",ble_device_nr);
id(mqtt_client).publish(mtopic,to_string(t2));
/*
id(bcsoc).publish_state(soc); // SOC from device (%)
id(bcsoccalc).publish_state(soccalc); // SOC calculated from cell voltages (%)
id(bctemp1).publish_state(t1); // Temperature 1 (°C)
id(bctemp2).publish_state(t2); // Temperature 2 (°C)
id(bccvsum).publish_state(cv/1000); // sum of cellvoltages = battery Voltage(V)
id(bccvmin).publish_state(cmin/1000); // lowest cellvoltage (V)
id(bccvmax).publish_state(cmax/1000); // highest cellvoltage (V)
id(bccvdiff).publish_state((cmax-cmin)/1000);
id(bccvavg).publish_state(cv/14000); // avarage cellvoltage (V)
*/
}
else if (x[3] == 0x03) {
ESP_LOGD("main", "Data: runtimeInfo ");
//sensor
// pv_level 1 und 2
/*
[6][7] PV-Eingangsleistung 1 (2Byte)
[8][9] PV-Eingangsleistung 2 (2Byte)
x[Y] | x[Z] << 8;
*/
int pvPower1 = x[6] | x[7] << 8;
int pvPower2 = x[8] | x[9] << 8;
if (ble_device_nr==1) { id(sensor_pv_power_in_1_1).publish_state(pvPower1); id(sensor_pv_power_in_1_2).publish_state(pvPower2); }
// Batterie Stand in %
/*
[10][11] Verbleibende Batteriekapazität in Prozent (2Byte)
x[Y] | x[Z] << 8;
*/
int batRemain = x[10] | x[11] << 8 ;
if (ble_device_nr==1) { id(sensor_bat_remain_1).publish_state(batRemain / 10); }
// Entladen bei weniger als ??? Watt PV Eingang
/*
[19][20] Entladeschwelle(2Byte)
x[Y] | x[Z] << 8;
*/
int disCharge = x[19] | x[20] << 8;
if (ble_device_nr==1) { id(sensor_discharge_treshold_1).publish_state(disCharge); }
// Füllstand des Akkus in Wh
/*
[22][23] Gesamtkapazität der Batterie (1Byte)
x[Y] | x[Z] << 8;
*/
int batCapacity = x[22] | x[23] << 8;
if (ble_device_nr==1) { id(sensor_bat_capacity_1).publish_state(batCapacity); }
// Ausgangsleistung in Watt
/*
[24][25] Ausgangsleistung 1(1Byte)
[26][27] Ausgangsleistung 2(1Byte)
x[Y] | x[Z] << 8;
*/
int powerOut1 = x[24] | x[25] << 8;
int powerOut2 = x[26] | x[27] << 8;
if (ble_device_nr==1) { id(sensor_power_out_1_1).publish_state(powerOut1); id(sensor_power_out_1_2).publish_state(powerOut2); }
// Geräte Version ( Firmware ? )
/*
[12] B2500 Geräteversion (1Byte)
0-255 ( ~ anzeige als /100 )
*/
float dev_version = x[12];
if (ble_device_nr==1) { id(sensor_device_version_1).publish_state(dev_version / 100); }
//
/*
[18] Dod (1Byte)
0-100 Prozentualer Anteil der Entladeleistung an der Nennleistung
*/
int dod_level = x[18];
if (ble_device_nr==1) { id(sensor_dod_1).publish_state(dod_level); }
// binary sensor / bool
// pv 1 und 2 in