-
Notifications
You must be signed in to change notification settings - Fork 22
/
ESP32_LFP_BLE_jk-bms-can.yaml
1770 lines (1622 loc) · 87.3 KB
/
ESP32_LFP_BLE_jk-bms-can.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
# JK-BMS-CAN ( PYLON, Seplos, GoodWe, SMA and Victron CAN bus protocol )
# esp32_ble_jk-bms-can.yaml is free software: you can redistribute it
# and/or modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation, either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/gpl.html>.
# v1.17.5 MrPablo : "Auto Charge Voltage Control" function rewritten, EOC cycle threshold added to reduce premature triggering of float phase
# V1.17.4 MrPablo : Added "SMA" to CAN BMS names, added function "Auto Charge Voltage Control" to avoid OVP alarms and improve balancing, categorised sensors, set time source to SNTP, min battery voltage based on BMS value, added "Last Complete Charge" timestamp, renamed daily energy sensors and added input number display option
# V1.17.3 Sleeper85 : Renumbering cells, Added “JK-BMS ESP32 Restart” switch, adding Total Daily Energy sensors, set jk_bms update interval to 3s, set default log level to INFO, improvement of comments
# V1.17.2 MrPablo : Added function "Auto Charge/Discharge Current Control" to avoid OVP/UVP alarms
# V1.17.1 Sleeper85 : New Cut-Off Current/Voltage Charging Logic for LFP with the participation of @shvmm
# V1.16.6 Sleeper85 : Selectable CAN settings + Adding inverter_offset_v + Improved CAN ID 0x355, sending 100% only at the end of the absorption phase, adding bytes [04:05] and [06:07] + Automatic calculation of the number of battery modules + Save and Restore slider values
# V1.16.5 Sleeper85 : Add Preventive Alarms Logic, CAN ID 0x356: send average temperature of T1/T2, new "Discharging current max" slider
# V1.16.4 Sleeper85 : Improved Charging Logic for ESP32 startup/reboot and Float charge, Add CAN ID 0x356 bytes [06:07] cycles for Sofar, Change switch name
# V1.16.3 Sleeper85 : ID 0x379 will be sent when choosing protocol 2 or 4 (Battery Capacity for Victron, Sol-Ark and Luxpower)
# V1.16.2 Sleeper85 : Split the "Charge/Discharge values" section and added instructions for "Stop Discharging" + Set "esp-idf" framework by default
# V1.16.1 Sleeper85 : Slider charging_current max value = ${charge_a}, Improved Alarm/Charging/Discharging Logic, Improved CAN protocol and Victron support
# V1.15.5 Sleeper85 : Improved code and set api "reboot_timout" to "0s" by default (no reboot without HA)
# V1.15.4 Sleeper85 : Improved documentation for API, Web Server and WiFi settings
# V1.15.3 Sleeper85 : Add 'CAN Protocol Settings' and new CAN ID based on the SMA and Victron protocol (alpha)
# V1.15.2 Sleeper85 : Improved Alarm handling, all alarms will set charge/discharge current to 0A and set 'Charging Status' to Alarm
# V1.15.1 Sleeper85 : New CANBUS script with CANBUS Status in HA, stop sending CAN messages if the inverter is not responding (fix WDT reboot issues)
# V1.14.3 Sleeper85 : Improved documentation + Charging Voltage tips for Deye
# V1.14.2 Sleeper85 : Improve 'Charging Voltage' behavior
# V1.14.1 Sleeper85 : Add 'Float charge function'
# V1.13.6 Sleeper85 : Add 'Absorption time' and 'Absorption Offset V.' slider
# V1.13.5 Sleeper85 : Set CAN manufacter to "PYLON" for improve compatibility with Deye and other inverters
# V1.13.4 Sleeper85 : Improve 'Charge Status' behavior + add 'Rebulk Offset V.' slider
# V1.13.3 uksa007 : Improve compatibility with Deye and other inverters
# V1.13.2 uksa007 : Send Max Temperature of T1, T2 to inverter
# V1.13.1 uksa007 : Fix compile issues with new version of ESPhome 2023.4.0, set rebulk offset to 2.5
substitutions:
# +--------------------------------------+
# name that will appear in esphome and homeassistant.
name: jk-bms-ble-can
# +--------------------------------------+
# | Bluetooth Settings |
# +--------------------------------------+
# Please use "JK02_24S" if you own a old JK-BMS < hardware version 11.0 (hardware version >= 6.0 and < 11.0)
# Please use "JK02_32S" if you own a new JK-BMS >= hardware version 11.0 (f.e. JK-B2A8S20P hw 11.XW, sw 11.26)
# Please use "JK04" if you have some old JK-BMS <= hardware version 3.0 (f.e. JK-B2A16S hw 3.0, sw. 3.3.0)
protocol_version: JK02_32S
mac_address: C8:47:8C:10:7E:AB
# +--------------------------------------+
# | Battery Charge Settings |
# +--------------------------------------+
# This is max charging amps eg 100A, for Bulk - Constant Current charging(CC), should be at least 10A less than BMS change current protection, 0.5C max
# 100A * 50V = 5000W
charge_a: "100"
# Float Voltage : corresponds to the voltage at which the battery would be maintained at the end of the absorption phase. (53.6V = 3.35V/Cell for 16S battery)
float_v: "53.6"
# Bulk / Absorption Voltage : corresponds to the Bulk voltage that will be used to charge the battery. (55.2V = 3.45V/Cell for 16S battery)
bulk_v: "55.2"
# Rebulk voltage, voltage less than FLOAT at which BMS requests rebulk. (52.8V = 3.3V/Cell for 16S battery)
rebulk_v: "52.8"
# +--------------------------------------+
# | Use the settings below with caution |
# +--------------------------------------+
# Inverter offset, allows you to correct the inverter charging voltage
# For example, with my Deye, if the requested charging voltage is 55.2v the Deye effective charging voltage will be 55.1v
# An offset of 0.1 provides an effective charging voltage of 55.2v
inverter_offset_v: "0.0"
# Auto charge current control will automatically reduce the charging current as a cell reaches the BMS "cell_voltage_overvoltage_recovery".
# If unsure, leave the below settings as default, this will ensure charge current will taper correctly.
# Factor to control the end of the charge current curve. A setting below 2 will extend the curve, above 2 will shorten the curve.
charge_a_factor_curve_end: "2.0"
# Factor to adjust the shape of the charge current curve.
# A setting below 1 will reduce current at a slow rate after the float voltage is reached, then increase as a cell reaches the BMS "cell_voltage_overvoltage_recovery".
# A factor above 1 will reduce current at a fast rate after the float voltage is reached, then slow as a cell reaches the BMS "cell_voltage_overvoltage_recovery".
charge_a_factor_curve_shape: "1.8"
# Factor to adjust the aggression of the auto voltage control logic.
# The higher the setting, the more that requested charge voltage will reduce as cells exceed the target voltage (bulk voltage).
charge_v_factor: "2.0"
# End of charge (EOC) will be triggered if current and max cell voltage meet cut-off thresholds, plus the cycle count exceeds the below threshold.
# As default, this cycle count is set to 60 which is approximately 60 seconds. This will reduce the liklihood of premature EOC.
eoc_cycle_threshold: "60"
# +--------------------------------------+
# | Battery Discharge Settings |
# +--------------------------------------+
# Max discharge amps eg 120, should be at least 10A less than BMS over discharge current protection, 0.5C max
# 120A * 50V = 6000W
discharge_a: "120"
# +--------------------------------------+
# | Use the settings below with caution |
# +--------------------------------------+
# Auto discharge current control will automatically reduce the charging current as a cell reaches the BMS "cell_voltage_undervoltage_recovery".
# If unsure, leave the below settings as default, this will ensure discharge current will taper correctly.
# Factor to control the end of the discharge current curve. A setting below 2 will extend the curve, above 2 will shorten the curve.
discharge_a_factor_curve_end: "2.0"
# Factor to adjust the shape of the charge current curve.
# A setting below 1 will reduce current at a slow rate after the float voltage is reached, then increase as a cell reaches the BMS "cell_voltage_undervoltage_recovery".
# A factor above 1 will reduce current at a fast rate after the float voltage is reached, then slow as a cell reaches the BMS "cell_voltage_undervoltage_recovery".
discharge_a_factor_curve_shape: "1.8"
# Discharge knee voltage, the point at which a cell will reduce in voltage more rapidly. For LiFePO4 cells, this is generally considered to be 3.1v.
discharge_knee_v: "3.1"
# +--------------------------------------+
# | Battery State of Health (SOH) |
# +--------------------------------------+
# Maximum charging cycles is used to calculate the battey SOH, LF280K v3 =8000.0, LF280K v2 =6000.0, LF280=3000.0 (decimal is required)
max_cycles: "6000.0"
# +--------------------------------------+
# | Preventive Alarms Logic |
# +--------------------------------------+
# Information from the EVE LF280K v2 cell datasheet
# LFP Recommended Operating Temperature Range : 10~45°C
# Number of cycles at 25°C : 6000
# Number of cycles at 45°C : 2500
# 60°C : Never charging or discharging
# 0°C : Never charging
# -30°C : Never discharging
# +--------------------------------------+
# High Temp Protection - Temp > OTP : Stop Charging and Discharging
otp: "45.0"
# Low Temp Protection - Temp < UTP : Stop Charging
utp: "1.0"
# +--------------------------------------+
# | Home Assistant Display Settings |
# +--------------------------------------+
# Input numbers can be displayed as a slider or an input box - options are 'slider' or 'box'
input_number_mode: 'slider'
# +--------------------------------------+
# | ESP32 CAN port pins |
# +--------------------------------------+
# GPIO pins your CAN bus transceiver (TJA1050, TJA1051T or SN65HVD230) is connected to the ESP, note! TX->TX and RX->RX.
can_tx_pin: GPIO23
can_rx_pin: GPIO22
# +------------------------------------------------------------------+
# | ** The settings below can be modified according to your needs ** |
# +------------------------------------------------------------------+
external_components_source: github://syssi/esphome-jk-bms@main
# components
# github://syssi/esphome-jk-bms@main
esphome:
name: ${name}
on_boot:
then:
- switch.turn_on: can_switch_charging
- switch.turn_on: can_switch_discharging
- switch.turn_on: can_switch_float
- switch.turn_on: can_switch_auto_charge_current
- switch.turn_on: can_switch_auto_discharge_current
- switch.turn_on: can_switch_auto_charge_voltage
# +--------------------------------------+
# | ESP32 settings |
# +--------------------------------------+
# For a stable Bluetooth connection keep the "esp-idf" framework
esp32:
board: esp32doit-devkit-v1
framework:
type: esp-idf
external_components:
- source: ${external_components_source}
refresh: 0s
logger:
level: INFO
ota:
on_begin:
then:
- lambda: id(enable_bluetooth_connection).turn_off();
- logger.log: "BLE shutdown for flashing"
# Please use the native `api` component instead of the `mqtt` section.
# If you use Home Assistant, the native API is more lightweight.
# If there is no HA server connected to this API, the ESP32 reboots every 15 minutes to try to resolve the problem.
# If you don't use Home Assistant please uncomment the "reboot_timeout: 0s" option.
api:
reboot_timeout: 0s
# If you don't want to use ESPHome's native API you can use MQTT instead.
# In this case don't forget to remove the 'api:' section.
# mqtt:
# broker: !secret mqtt_host
# username: !secret mqtt_username
# password: !secret mqtt_password
# id: mqtt_client
# In the event of problems with the WiFi network, the ESP32 will reboot every 15 minutes to try to resolve the problem.
# If we don't want to connect the ESP32 to the WiFi network please remove the 4 lines below.
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
domain: !secret domain
captive_portal:
#web_server:
# port: 80
# log: false
# ota: false
# +--------------------------------------+
# | ** Don't make changes below this ** |
# +--------------------------------------+
globals:
- id: can_ack_counter
type: int
restore_value: no
initial_value: '0'
- id: charge_status
type: std::string
restore_value: no
initial_value: '"Wait"'
- id: alarm_status
type: std::string
restore_value: no
initial_value: '"NoAlarm"'
- id: can_msg_counter
type: int
restore_value: no
initial_value: '0'
- id: eoc
type: bool
restore_value: no
initial_value: "false"
- id: eoc_timestamp
type: time_t
restore_value: true
initial_value: ''
output:
- platform: gpio
pin: 2
id: led
inverted: true
light:
- platform: binary
output: led
id: blue_led
name: "Blue LED"
internal: true
# +--------------------------------------+
# | JK-BMS BLE connection |
# +--------------------------------------+
esp32_ble_tracker:
on_ble_advertise:
then:
- lambda: |-
if (x.get_name().rfind("JK-", 0) == 0) {
ESP_LOGI("ble_adv", "New JK-BMS found");
ESP_LOGI("ble_adv", " Name: %s", x.get_name().c_str());
ESP_LOGI("ble_adv", " MAC address: %s", x.address_str().c_str());
ESP_LOGD("ble_adv", " Advertised service UUIDs:");
for (auto uuid : x.get_service_uuids()) {
ESP_LOGD("ble_adv", " - %s", uuid.to_string().c_str());
}
}
ble_client:
- mac_address: ${mac_address}
id: client0
jk_bms_ble:
- ble_client_id: client0
protocol_version: ${protocol_version}
throttle: 3s
id: bms0
# enable_fake_traffic: true
# +--------------------------------------+
time: # Enable time component to reset energy at midnight
- platform: sntp
id: sntp_time
select:
- platform: template
name: CAN BMS Name
id: can_bms_name
options:
- "GENERAL"
- "PYLON"
- "GOODWE"
- "SEPLOS"
- "SMA"
restore_value: true
initial_option: "PYLON"
optimistic: true
entity_category: config
set_action:
- logger.log:
format: "Chosen option: %s"
args: ["x.c_str()"]
- platform: template
name: CAN Protocol
id: can_protocol
options:
- "GENERAL"
- "PYLON 1.2"
- "PYLON +"
- "SMA"
- "VICTRON"
restore_value: true
initial_option: "PYLON +"
optimistic: true
entity_category: config
set_action:
- logger.log:
format: "Chosen option: %s"
args: ["x.c_str()"]
binary_sensor:
- platform: jk_bms_ble
balancing:
id: equalizing
name: "${name} balancing"
charging:
name: "${name} charging"
discharging:
name: "${name} discharging"
online_status:
name: "${name} Online Status"
# +--------------------------------------+
- platform: template
name: "${name} CANBUS Status"
id: can_bus_status
switch:
- platform: jk_bms_ble
charging:
id: bms_switch_charging
name: "${name} BMS Charge switch"
discharging:
id: bms_switch_discharging
name: "${name} BMS Discharge switch"
balancer:
id: bms_switch_balancing
name: "${name} BMS Balance switch"
# +--------------------------------------+
- platform: ble_client
ble_client_id: client0
id: enable_bluetooth_connection
name: "${name} Enable Bluetooth connection"
# +--------------------------------------+
- platform: template
name: "${name} CAN Charge enabled"
id: can_switch_charging
optimistic: true
- platform: template
name: "${name} CAN Discharge enabled"
id: can_switch_discharging
optimistic: true
- platform: template
name: "${name} CAN Force Bulk (top bal)"
id: can_switch_force_bulk
optimistic: true
- platform: template
name: "${name} CAN Float charge enabled"
id: can_switch_float
optimistic: true
- platform: template
name: ${name} CAN Automatic Charge Current
id: can_switch_auto_charge_current
optimistic: true
entity_category: config
- platform: template
name: ${name} CAN Automatic Discharge Current
id: can_switch_auto_discharge_current
optimistic: true
entity_category: config
- platform: template
name: ${name} CAN Automatic Charge Voltage
id: can_switch_auto_charge_voltage
optimistic: true
entity_category: config
sensor:
- platform: jk_bms_ble
jk_bms_ble_id: bms0
min_cell_voltage:
id: min_cell_voltage
name: "${name} min cell voltage"
max_cell_voltage:
id: max_cell_voltage
name: "${name} max cell voltage"
min_voltage_cell:
id: min_voltage_cell
name: "${name} min voltage cell"
max_voltage_cell:
id: max_voltage_cell
name: "${name} max voltage cell"
delta_cell_voltage:
id: delta_cell_voltage
name: "${name} delta cell voltage"
average_cell_voltage:
name: "${name} average cell voltage"
cell_voltage_1:
id: cell_v_01
name: "${name} cell voltage 01"
cell_voltage_2:
id: cell_v_02
name: "${name} cell voltage 02"
cell_voltage_3:
id: cell_v_03
name: "${name} cell voltage 03"
cell_voltage_4:
id: cell_v_04
name: "${name} cell voltage 04"
cell_voltage_5:
id: cell_v_05
name: "${name} cell voltage 05"
cell_voltage_6:
id: cell_v_06
name: "${name} cell voltage 06"
cell_voltage_7:
id: cell_v_07
name: "${name} cell voltage 07"
cell_voltage_8:
id: cell_v_08
name: "${name} cell voltage 08"
cell_voltage_9:
id: cell_v_09
name: "${name} cell voltage 09"
cell_voltage_10:
id: cell_v_10
name: "${name} cell voltage 10"
cell_voltage_11:
id: cell_v_11
name: "${name} cell voltage 11"
cell_voltage_12:
id: cell_v_12
name: "${name} cell voltage 12"
cell_voltage_13:
id: cell_v_13
name: "${name} cell voltage 13"
cell_voltage_14:
id: cell_v_14
name: "${name} cell voltage 14"
cell_voltage_15:
id: cell_v_15
name: "${name} cell voltage 15"
cell_voltage_16:
id: cell_v_16
name: "${name} cell voltage 16"
cell_voltage_17:
id: cell_v_17
name: "${name} cell voltage 17"
disabled_by_default: true
cell_voltage_18:
id: cell_v_18
name: "${name} cell voltage 18"
disabled_by_default: true
cell_voltage_19:
id: cell_v_19
name: "${name} cell voltage 19"
disabled_by_default: true
cell_voltage_20:
id: cell_v_20
name: "${name} cell voltage 20"
disabled_by_default: true
cell_voltage_21:
id: cell_v_21
name: "${name} cell voltage 21"
disabled_by_default: true
cell_voltage_22:
id: cell_v_22
name: "${name} cell voltage 22"
disabled_by_default: true
cell_voltage_23:
id: cell_v_23
name: "${name} cell voltage 23"
disabled_by_default: true
cell_voltage_24:
id: cell_v_24
name: "${name} cell voltage 24"
disabled_by_default: true
cell_resistance_1:
name: "${name} cell resistance 01"
cell_resistance_2:
name: "${name} cell resistance 02"
cell_resistance_3:
name: "${name} cell resistance 03"
cell_resistance_4:
name: "${name} cell resistance 04"
cell_resistance_5:
name: "${name} cell resistance 05"
cell_resistance_6:
name: "${name} cell resistance 06"
cell_resistance_7:
name: "${name} cell resistance 07"
cell_resistance_8:
name: "${name} cell resistance 08"
cell_resistance_9:
name: "${name} cell resistance 09"
cell_resistance_10:
name: "${name} cell resistance 10"
cell_resistance_11:
name: "${name} cell resistance 11"
cell_resistance_12:
name: "${name} cell resistance 12"
cell_resistance_13:
name: "${name} cell resistance 13"
cell_resistance_14:
name: "${name} cell resistance 14"
cell_resistance_15:
name: "${name} cell resistance 15"
cell_resistance_16:
name: "${name} cell resistance 16"
#cell_resistance_17:
# name: "${name} cell resistance 17"
#cell_resistance_18:
# name: "${name} cell resistance 18"
#cell_resistance_19:
# name: "${name} cell resistance 19"
#cell_resistance_20:
# name: "${name} cell resistance 20"
#cell_resistance_21:
# name: "${name} cell resistance 21"
#cell_resistance_22:
# name: "${name} cell resistance 22"
#cell_resistance_23:
# name: "${name} cell resistance 23"
#cell_resistance_24:
# name: "${name} cell resistance 24"
total_voltage:
id: total_voltage
name: "${name} total voltage"
current:
id: current
name: "${name} current"
power:
id: power
name: "${name} power"
charging_power:
id: charging_power
name: "${name} charging power"
discharging_power:
id: discharging_power
name: "${name} discharging power"
power_tube_temperature:
id: power_tube_temperature
name: "${name} power tube temperature"
temperature_sensor_1:
id: temperature_sensor_1
name: "${name} temperature sensor 1"
temperature_sensor_2:
id: temperature_sensor_2
name: "${name} temperature sensor 2"
state_of_charge:
id: state_of_charge
name: "${name} state of charge"
capacity_remaining:
id: capacity_remaining_ah
name: "${name} capacity remaining"
total_battery_capacity_setting:
id: battery_capacity
name: "${name} total battery capacity setting"
charging_cycles:
id: charging_cycles
name: "${name} charging cycles"
total_charging_cycle_capacity:
name: "${name} total charging cycle capacity"
total_runtime:
name: "${name} total runtime"
balancing_current:
name: "${name} balancing current"
errors_bitmask:
id: errors_bitmask
name: "${name} errors bitmask"
# +--------------------------------------+
# | Total Daily Energy |
# +--------------------------------------+
- platform: total_daily_energy
name: "${name} BMS Daily Charging Energy"
power_id: charging_power
unit_of_measurement: 'kWh'
state_class: total_increasing
device_class: energy
accuracy_decimals: 3
filters:
# Multiplication factor from W to kW is 0.001
- multiply: 0.001
- platform: total_daily_energy
name: "${name} BMS Daily Discharging Energy"
power_id: discharging_power
unit_of_measurement: 'kWh'
state_class: total_increasing
device_class: energy
accuracy_decimals: 3
filters:
# Multiplication factor from W to kW is 0.001
- multiply: 0.001
# +--------------------------------------+
# | Uptime sensor |
# +--------------------------------------+
- platform: uptime
name: ${name} Uptime Sensor
id: uptime_sensor
update_interval: 60s
entity_category: diagnostic
on_raw_value:
then:
- text_sensor.template.publish:
id: uptime_human
state: !lambda |-
int seconds = round(id(uptime_sensor).raw_state);
int days = seconds / (24 * 3600);
seconds = seconds % (24 * 3600);
int hours = seconds / 3600;
seconds = seconds % 3600;
int minutes = seconds / 60;
seconds = seconds % 60;
return (
(days ? to_string(days) + "d " : "") +
(hours ? to_string(hours) + "h " : "") +
(minutes ? to_string(minutes) + "m " : "") +
(to_string(seconds) + "s")
).c_str();
# +--------------------------------------+
# | Template sensor |
# +--------------------------------------+
- platform: template
name: ${name} Requested Charge Voltage
id: requested_charge_voltage
unit_of_measurement: V
device_class: voltage
- platform: template
name: "${name} Last Complete Charge"
device_class: timestamp
id: eoc_timestamp_template
lambda: return id(eoc_timestamp);
on_value:
then:
- text_sensor.template.publish:
id: eoc_timestamp_human
state: !lambda |-
char str[19];
strftime(str, sizeof(str), " %H:%M %d %b %Y", localtime(&id(eoc_timestamp)));
return {str};
entity_category: diagnostic
# +--------------------------------------+
# | Auto Charge Current Control |
# +--------------------------------------+
# First, an initial charge current is calculated based upon the maximum cell voltage but only if the Auto Charge Current switch is enabled.
- platform: copy
source_id: max_cell_voltage
name: ${name} Initial Charge Current
id: auto_ccl_initial
unit_of_measurement: A
device_class: current
internal: true
filters:
- lambda: !lambda |-
// Variables
double cell_float_v = (id(float_voltage).state / id(cell_count).state);
// Auto Charge Current function
if (id(can_switch_auto_charge_current).state) {
if (id(max_cell_voltage).state > id(cell_ovpr).state) return 0.0;
else return max(0.0,(- pow(${charge_a_factor_curve_end}, pow( max(0.0, ((x - cell_float_v) / (id(cell_ovpr).state - cell_float_v))),${charge_a_factor_curve_shape}))+2) * id(charging_current).state);
}
else return id(charging_current).state;
# Second, an exponential moving average is calculated. This will be used to smooth the transition between different initial charge current values.
- platform: copy
source_id: auto_ccl_initial
name: ${name} Moving Average Charge Current
id: auto_ccl_moving_average
unit_of_measurement: A
device_class: current
internal: true
filters:
- exponential_moving_average:
alpha: 0.15
send_every: 1
send_first_at: 1
# Third, if the instantaneous initial charge current value is lower than the moving average, use that, otherwise use the moving average.
# This means that cell voltage spikes will be reacted to quickly, but reduced cell voltages will not, preventing oscillation of requested current.
- platform: copy
source_id: auto_ccl_initial
name: ${name} Requested Charge Current
id: auto_ccl
unit_of_measurement: A
device_class: current
accuracy_decimals: 1
internal: false
filters:
- lambda: !lambda |-
return (round(min(id(auto_ccl_moving_average).state, x) * 10) / 10);
# +--------------------------------------+
# | Auto Discharge Current Control |
# +--------------------------------------+
# First, an initial discharge current is calculated based upon the minimum cell voltage but only if the Auto Discharge Current switch is enabled.
- platform: copy
source_id: min_cell_voltage
name: ${name} Initial Discharge Current
id: auto_dcl_initial
unit_of_measurement: A
device_class: current
internal: true
filters:
- lambda: !lambda |-
// Auto Discharge Current function
if (id(can_switch_auto_discharge_current).state) {
if (id(min_cell_voltage).state < id(cell_uvpr).state) return 0.0;
else return max(0.0,(- pow(${discharge_a_factor_curve_end}, pow( max(0.0, ((x - ${discharge_knee_v}) / (id(cell_uvpr).state - ${discharge_knee_v}))),${discharge_a_factor_curve_shape}))+2) * id(discharging_current).state);
}
else return id(discharging_current).state;
# Second, an exponential moving average is calculated. This will be used to smooth the transition between different initial discharge current values.
- platform: copy
source_id: auto_dcl_initial
name: ${name} Moving Average Discharge Current
id: auto_dcl_moving_average
unit_of_measurement: A
device_class: current
internal: true
filters:
- exponential_moving_average:
alpha: 0.15
send_every: 1
send_first_at: 1
# Third, if the instantaneous initial discharge current value is lower than the moving average, use that, otherwise use the moving average.
# This means that cell voltage drops will be reacted to quickly, but increased cell voltages will not, preventing oscillation of requested current.
- platform: copy
source_id: auto_dcl_initial
name: ${name} Requested Discharge Current
id: auto_dcl
unit_of_measurement: A
device_class: current
accuracy_decimals: 1
internal: false
filters:
- lambda: !lambda |-
return (round(min(id(auto_dcl_moving_average).state, x) * 10) / 10);
# +--------------------------------------+
# | Auto Charge Voltage Control |
# +--------------------------------------+
- platform: template
name: ${name} Auto Charge Voltage
id: auto_cvl
unit_of_measurement: V
device_class: voltage
internal: true
lambda: !lambda |-
// Variables
float charge_v_factor = ${charge_v_factor};
float cell_bulk_v = (id(bulk_voltage).state / id(cell_count).state);
float target_total_v = 0;
float excess_v = 0;
float offset_v = 0;
float dyn_chg_v = 0;
// Check feature enabled or if any cells are at / above bulk voltage
if ((!id(can_switch_auto_charge_voltage).state) | (id(max_cell_voltage).state < cell_bulk_v)){
return id(bulk_voltage).state;
} else {
// Create array of cell voltages
float cell_array[] = {
id(cell_v_01).state, id(cell_v_02).state, id(cell_v_03).state, id(cell_v_04).state,
id(cell_v_05).state, id(cell_v_06).state, id(cell_v_07).state, id(cell_v_08).state,
id(cell_v_09).state, id(cell_v_10).state, id(cell_v_11).state, id(cell_v_12).state,
id(cell_v_13).state, id(cell_v_14).state, id(cell_v_15).state, id(cell_v_16).state,
id(cell_v_17).state, id(cell_v_18).state, id(cell_v_19).state, id(cell_v_20).state,
id(cell_v_21).state, id(cell_v_22).state, id(cell_v_23).state, id(cell_v_24).state,
};
// Calculate actual total voltage
for(int i = 0; i < id(cell_count).state; i++) {
target_total_v += cell_array[i];
};
// Add cell voltage offset to target total voltage if possible
for(int i = 0; i < id(cell_count).state; i++) {
if (cell_array[i] >= cell_bulk_v) {
excess_v = cell_array[i] - cell_bulk_v;
offset_v = (pow(excess_v,2))*(charge_v_factor*-100);
target_total_v += offset_v;
}
};
// Use minimum of dynamic charge voltage or bulk voltage
dyn_chg_v = min(id(bulk_voltage).state, target_total_v);
// Return automatic charge voltage
return ceil(dyn_chg_v * 10) / 10;
}
update_interval: 1s
text_sensor:
- platform: jk_bms_ble
errors:
name: "${name} errors"
total_runtime_formatted:
name: "${name} total runtime formatted"
entity_category: diagnostic
# +--------------------------------------+
# | Template text sensors |
# +--------------------------------------+
- platform: template
name: ${name} Uptime Human Readable
id: uptime_human
icon: mdi:clock-start
entity_category: diagnostic
- platform: template
name: ${name} Last Complete Charge Human Readable
id: eoc_timestamp_human
icon: mdi:clock-start
entity_category: diagnostic
- platform: template
name: "${name} Charging Status"
id: charging_status
number:
- platform: jk_bms_ble
jk_bms_ble_id: bms0
balance_trigger_voltage:
id: balance_trigger_voltage
name: "${name} balance trigger voltage"
cell_count:
id: cell_count
name: "${name} cell count"
total_battery_capacity:
name: "${name} total battery capacity"
cell_voltage_overvoltage_protection:
id: cell_ovp
name: "${name} cell voltage overvoltage protection"
cell_voltage_overvoltage_recovery:
id: cell_ovpr
name: "${name} cell voltage overvoltage recovery"
cell_voltage_undervoltage_protection:
id: cell_uvp
name: "${name} cell voltage undervoltage protection"
cell_voltage_undervoltage_recovery:
id: cell_uvpr
name: "${name} cell voltage undervoltage recovery"
balance_starting_voltage:
name: "${name} balance starting voltage"
voltage_calibration:
name: "${name} voltage calibration"
current_calibration:
name: "${name} current calibration"
power_off_voltage:
name: "${name} power off voltage"
max_balance_current:
name: "${name} max balance current"
max_charge_current:
name: "${name} max charge current"
max_discharge_current:
name: "${name} max discharge current"
# +--------------------------------------+
# | Slider / Box |
# +--------------------------------------+
- platform: template
name: "${name} Bulk voltage"
id: "bulk_voltage"
step: 0.1
min_value: 54.0
max_value: 58.4
restore_value: true
mode: "${input_number_mode}"
initial_value: "${bulk_v}"
unit_of_measurement: V
icon: mdi:battery-charging
optimistic: true
entity_category: config
- platform: template
name: "${name} Float voltage"
id: "float_voltage"
step: 0.1
min_value: 52.8
max_value: 58.4
restore_value: true
mode: "${input_number_mode}"
initial_value: "${float_v}"
unit_of_measurement: V
icon: mdi:battery-charging
optimistic: true
entity_category: config
- platform: template
name: "${name} Rebulk V."
id: "rebulk_voltage"
step: 0.1
min_value: 51.2
max_value: 53.6
restore_value: true
mode: "${input_number_mode}"
initial_value: "${rebulk_v}"
unit_of_measurement: V
icon: mdi:sine-wave
optimistic: true
entity_category: config
- platform: template
name: "${name} Absorption Offset V."
id: "absorption_offset"
step: 0.05
min_value: 0
max_value: 0.3
restore_value: true
mode: "${input_number_mode}"
initial_value: 0.1
unit_of_measurement: V
icon: mdi:sine-wave
optimistic: true
entity_category: config
- platform: template
name: "${name} Charging current max"
id: "charging_current"
step: 1
min_value: 0
max_value: "${charge_a}"
restore_value: true
mode: "${input_number_mode}"
initial_value: "${charge_a}"
unit_of_measurement: A
icon: mdi:current-dc
optimistic: true
entity_category: config
- platform: template
name: "${name} Discharging current max"
id: "discharging_current"
step: 1
min_value: 0
max_value: "${discharge_a}"
restore_value: true
mode: "${input_number_mode}"
initial_value: "${discharge_a}"
unit_of_measurement: A
icon: mdi:current-dc
optimistic: true
entity_category: config
button:
- platform: jk_bms_ble
retrieve_settings:
name: "${name} retrieve settings"
entity_category: config
retrieve_device_info:
name: "${name} retrieve device info"
entity_category: config
- platform: restart
name: "JK-BMS ESP32 Restart"
entity_category: config
# +--------------------------------------+
# | Start CAN Handling |
# +--------------------------------------+
canbus: # 0x305 - Inverter ACK - SMA/LG/Pylon/Goodwe reply
- platform: esp32_can
tx_pin: ${can_tx_pin}
rx_pin: ${can_rx_pin}
can_id: 4
bit_rate: 500kbps
on_frame:
- can_id: 0x305
then:
- light.toggle:
id: blue_led
- lambda: |-
id(can_ack_counter) = 0; // Reset ACK counter
id(can_bus_status).publish_state(true); // Set CANBUS Status to ON
ESP_LOGI("main", "received can id: 0x305 ACK");
interval:
- interval: 120s
then:
- lambda: id(can_ack_counter) = 0; // Reset ACK counter for test inverter ACK
- interval: 100ms
then:
- if:
condition:
lambda: |-
if (id(can_ack_counter) < 20) { // Inverter ACK ? => CANBUS ON
id(can_ack_counter)++; // CANBUS ACK counter ++
id(can_msg_counter)++; // CANBUS MSG counter ++
return true; // Condition OK
}
else if (id(can_bus_status).state == false) { // CANBUS already OFF ?
return false; // Nothing to do
}
else {
id(can_bus_status).publish_state(false); // Set CANBUS Status to OFF
ESP_LOGI("main", "No rx can 0x305 reply, Inverter not connected/responding...");
return false; // Condition NOK
}
then:
- if: # 0x359 - Protection Alarms, Warning and Flags ( PYLON / PYLON + )
condition:
lambda: return ((id(can_msg_counter) == 1) & ((id(can_protocol).active_index() == 1) | (id(can_protocol).active_index() == 2)));
then: