fl4p / batmon-ha

Add-on for Home Assistant to connect JK, JBD, Daly, ANT, SOK and Supervolt BMS via Bluetooth
MIT License
307 stars 60 forks source link

BMS repeatedly becomes unavailable and fails to reconnect. #42

Closed UV-PWRD closed 1 year ago

UV-PWRD commented 1 year ago

Thank you for this add-on, it's great, though I'm having some issues with stability.

I'm using a 4GB Rpi4, running Debian 11, with HA on top in docker (running some other docker images also to help with off-grid inverter things) and I cannot get the BMS to stay connected for long periods.

The Host is located within half a meter of the BMS, which is a JK 150a device.

The BMS entities will become "unavailable" and I have an automation to restart the add-on after 5mins of this, sometimes it's still not able to reconnect and you must wait longer.

There's nothing else connecting to the BMS that I'm aware of, the log shows this. The reconnect on its own but other times it will not. It disconnects regularly.

Watchdog is on, keep_alive is on, sample period is set to 1.

It will time out 120s trying to reconnect and then must be restarted again.

I can automate around this but would like to know why it fails and how to prevent it.

INFO [sampling.py:100] JKBt(3C:A5:51:86:02:84) times: connect=0.00s fetch=0.29s INFO [sampling.py:68] 14kWh_Lifpo4 result@2022-12-13T13:04:32.725557 BmsSample(U=53.9V,I=18.04A,P=973W,q=191.8Ah/277.0,mos=29.1°C) INFO [sampling.py:75] 14kWh_Lifpo4 volt=3364,3358,3390,3373,3378,3369,3353,3389,3358,3358,3372,3391,3385,3358,3351,3356 temp=[23.6, 22.5] INFO [sampling.py:100] JKBt(3C:A5:51:86:02:84) times: connect=0.00s fetch=0.28s WARNING [bt.py:19] BMS JKBt(3C:A5:51:86:02:84) disconnected! INFO [sampling.py:41] connecting bms JKBt(3C:A5:51:86:02:84) INFO [sampling.py:47] connected bms JKBt(3C:A5:51:86:02:84)! INFO [sampling.py:68] 14kWh_Lifpo4 result@2022-12-13T13:04:38.282262 BmsSample(U=53.9V,I=17.83A,P=961W,q=191.8Ah/277.0,mos=29.1°C) INFO [sampling.py:75] 14kWh_Lifpo4 volt=3367,3357,3389,3380,3370,3372,3352,3386,3363,3354,3375,3391,3383,3360,3349,3357

When it fails it looks like this until my automation kicks in.

INFO [sampling.py:75] 14kWh_Lifpo4 volt=3360,3360,3377,3380,3370,3363,3354,3373,3362,3354,3367,3404,3377,3357,3351,3352 temp=[24.1, 23.1] INFO [sampling.py:100] JKBt(3C:A5:51:86:02:84) times: connect=0.00s fetch=0.30s WARNING [bt.py:19] BMS JKBt(3C:A5:51:86:02:84) disconnected! INFO [sampling.py:41] connecting bms JKBt(3C:A5:51:86:02:84) INFO [jikong.py:114] normal connect failed, connecting with scanner INFO [jikong.py:114] normal connect failed, connecting with scanner INFO [jikong.py:114] normal connect failed, connecting with scanner INFO [jikong.py:114] normal connect failed, connecting with scanner INFO [jikong.py:114] normal connect failed, connecting with scanner INFO [jikong.py:114] normal connect failed, connecting with scanner INFO [jikong.py:114] normal connect failed, connecting with scanner INFO [jikong.py:114] normal connect failed, connecting with scanner INFO [jikong.py:114] normal connect failed, connecting with scanner INFO [jikong.py:114] normal connect failed, connecting with scanner INFO [jikong.py:114] normal connect failed, connecting with scanner INFO [jikong.py:114] normal connect failed, connecting with scanner ERROR [main.py:110] MQTT message publish timeout (last 120s ago), exit INFO [jikong.py:114] normal connect failed, connecting with scanner INFO [jikong.py:114] normal connect failed, connecting with scanner INFO [jikong.py:114] normal connect failed, connecting with scanner INFO [jikong.py:114] normal connect failed, connecting with scanner INFO [jikong.py:114] normal connect failed, connecting with scanner s6-rc: info: service legacy-services successfully started INFO [main.py:68] BT Discovery: INFO [main.py:71] - no devices found - INFO [bt.py:91] BMS JKBt(3C:A5:51:86:02:84) keep alive enabled INFO [main.py:178] connecting mqtt XXXX@core-mosquitto INFO [main.py:201] Fetching 1 BMS + 0 others serially, period=1.00s, keep_alive=True INFO [sampling.py:41] connecting bms JKBt(3C:A5:51:86:02:84) INFO [main.py:99] mqtt watchdog loop started with timeout 120.0s INFO [sampling.py:47] connected bms JKBt(3C:A5:51:86:02:84)! INFO [sampling.py:64] 14kWh_Lifpo4 subscribing for {'charge': True, 'discharge': True} switch change INFO [mqtt_util.py:297] subscribe homeassistant/switch/14kWh_Lifpo4/charge/set INFO [mqtt_util.py:297] subscribe homeassistant/switch/14kWh_Lifpo4/discharge/set INFO [sampling.py:68] 14kWh_Lifpo4 result@2022-12-13T13:28:35.218390 BmsSample(U=54.0V,I=20.35A,P=1099W,q=195.6Ah/277.0,mos=28.6°C) INFO [sampling.py:75] 14kWh_Lifpo4 volt=3368,3363,3396,3383,3384,3372,3357,3393,3365,3360,3377,3422,3388,3363,3354,3358

fl4p commented 1 year ago

Did you try using an USB Bluetooth dongle instead of the RPI on-board?

UV-PWRD commented 1 year ago

I

Did you try using an USB Bluetooth dongle instead of the RPI on-board?

No, I don't have one on hand. Is there something wrong with the onboard unit?

I have also seen this error, where the host must be restarted to resolve it. Let me know if you can't zoom in, it was a screenshot and the only way I could get a copy of it at the time.

Screenshot_20221218-195816_Home Assistant

fl4p commented 1 year ago

The RPI onboard unit is fine, however I experienced Bluetooth Low Energy stack implementation to be a buggy on some platforms. This seems to be related to some low level driver issues, as with the error you posted.

UV-PWRD commented 1 year ago

The RPI onboard unit is fine, however I experienced Bluetooth Low Energy stack implementation to be a buggy on some platforms. This seems to be related to some low level driver issues, as with the error you posted.

If I get a bluetooth dongle, how do I change the BatMon add-on to use it instead?

fl4p commented 1 year ago

If i recall correctly, the RPI will automatically use the USB dongle instead of the onboard unit. You can disable the onboard bluetooth as described here:

$ echo "dtoverlay=disable-bt" | sudo tee -a /boot/config.txt
UV-PWRD commented 1 year ago

If i recall correctly, the RPI will automatically use the USB dongle instead of the onboard unit. You can disable the onboard bluetooth as described here:

$ echo "dtoverlay=disable-bt" | sudo tee -a /boot/config.txt

Thanks, I'm still yet to buy a dongle as I'm not convinced it's the issue.

Using my phone and the BMS, I still have bluetooth drop outs, I think the BMS itself is not a fantastic bluetooth module.

I've also moved the Rpi out of the solar cabinet as I suspected heat issues and the platform is now far more stable, it still disconnects bluetooth but I the automation to restart is now far more effective.

UV-PWRD commented 1 year ago

I'm running a HA instance on a RPI4B 4GB with 2 LiFePO4 batteries in parallel and 2 16s 48V 200A Daly BMSs. I encounter the same erratic disconnect issues as described above, drop outs of the BT connections to the built-in BT module of the RPI. The connection to the BT app of Daly, the Smart BMS on an iPhone is stable, as far as I can tell. Is there any chance to get this up and running in HA with a reliable, uninterrupted performance? I will also try an external BT dongle, as suggested, but am wondering, why that should be more stable as the list of high performance adapters on https://www.home-assistant.io/integrations/bluetooth/ explicitly names the Raspberry Pi 4B (CYW43455) configuration as suitable... Else I might try to switch to the UART-USB interface. Sorry, I am not a programmer, just a user. Thanks a lot for all your efforts!

I couldn't get the UART cable to work, but I guess that depends on which project you're using to try and implement it.

Does this add-on work with UART?

UV-PWRD commented 1 year ago

So even with the HA box outside of the cabinet and much cooler, I am still seeing this.

ERROR [sampling.py:97] 14kWh_Lifpo4 error: Device 3C:A5:51:86:02:84 not discovered ({'37:86:B9:58:BC:83'}) ERROR [main.py:240] 1 exceptions occurred fetching BMSs ERROR [main.py:85] Error (num 1) reading BMS: Device 3C:A5:51:86:02:84 not discovered ({'37:86:B9:58:BC:83'}) ERROR [main.py:86] Stack: Traceback (most recent call last): File "/app/bmslib/jikong.py", line 112, in connect await super().connect(timeout=4) File "/app/bmslib/bt.py", line 22, in connect await self.client.connect(timeout=timeout) File "/usr/lib/python3.10/site-packages/bleak/backends/bluezdbus/client.py", line 198, in connect raise BleakError( bleak.exc.BleakError: Device with address 3C:A5:51:86:02:84 was not found. During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/app/main.py", line 81, in fetch_loop await fn() File "/app/main.py", line 241, in fn raise exceptions[0] File "/app/main.py", line 236, in fn await t File "/app/bmslib/sampling.py", line 32, in call return await self.sample() File "/app/bmslib/sampling.py", line 45, in sample async with bms: File "/app/bmslib/bt.py", line 78, in aenter await self.connect() File "/app/bmslib/jikong.py", line 115, in connect await self._connect_with_scanner(timeout=timeout) File "/app/bmslib/bt.py", line 35, in _connect_with_scanner raise Exception('Device %s not discovered (%s)' % (self.client.address, discovered)) Exception: Device 3C:A5:51:86:02:84 not discovered ({'37:86:B9:58:BC:83'})

fl4p commented 1 year ago

YOu can try to find the BMS with a BLE scan from the command line: https://ukbaz.github.io/howto/beacon_scan_cmd_line.html Does it appear there?

fl4p commented 1 year ago

I had the same experience with Daly. If you turn on the HA Watchdog it'll restart batmon automatically.

Thanks for your appreciation!

UV-PWRD commented 1 year ago

I had the same experience with Daly. If you turn on the HA Watchdog it'll restart batmon automatically.

Thanks for your appreciation!

I wanted to come back and update.

I disabled on board wifi after learning wifi and Bluetooth share the antenna.

I hard wired the Rpi4 and put it back in the solar cabinet, bkuetooth has not disconnected a single time in over a week now.

I think the RPi4 hardware is not up to scratch personally.

fl4p commented 1 year ago

@UV-PWRD Thanks for your reply. I'll reference this in the readme. Never tried disabling Wifi, good thinking

UV-PWRD commented 1 year ago

@UV-PWRD Thanks for your reply. I'll reference this in the readme. Never tried disabling Wifi, good thinking

Adding to this, after 2 weeks of solid connection, it is now flakey again.

I've recently switched the Pi4 to booting from USB SSD and from the moment I added that device the Bluetooth has gone back to behaving the same way it was previously, constantly disconnecting.

I've ordered a USB BT dongle to test, I tend to feel that the combined onboard Wifi and BT are a bit of a joke as neither works well when the other is in use or the system is being asked to do anything else.

It "could" be a power issue though, I have not connected a powered USB hub for the SSD, it's in a USB/M2 adaptor case directly connected to the Pi4. I've hard of lots of issues being power related. My Pi4 came with the power supply from Labists as a kit, it is not the "genuine" power pack, but appears to match the specs.