birdie1 / victron

Victron bluetooth and serial communication at a python script. With MQTT and HomeAssistant support.
GNU General Public License v3.0
125 stars 16 forks source link

No values published to log or MQTT from Victron Smartshunt #2

Closed oedo808 closed 2 years ago

oedo808 commented 2 years ago

I'm able to connect on both bluetooth and bluetooth-ble to a victron smartshunt but I can't seem to get any data out of it. The only MQTT topics published are the HASS config topics. I've tried with hass set to True and False and it's the same.

Here is my BLE config

devices:
    - name: Shunt1
      type: smartshunt
      protocol: bluetooth-ble
      mac: D1:43:04:AA:84:9A

My MQTT config:

mqtt:
    host: 192.168.0.51
    port: 1883
    base_topic: victron
    hass: True

and an example of some of the logs:

[DEBUG  ] (2021-12-30 00:39:33,047) victron_ble.py::80 Shunt1: Will reconnect at 2021-12-30 00:40:33.046711
[INFO   ] (2021-12-30 00:40:34,165) victron_ble.py::47 Shunt1: Connecting...
[INFO   ] (2021-12-30 00:40:35,217) victron_gatt_ble.py::41 Shunt1: Connect successful!
[DEBUG  ] (2021-12-30 00:40:37,031) victron_gatt_ble.py::68 Shunt1: [D1:43:04:AA:84:9A] Resolved services
[DEBUG  ] (2021-12-30 00:40:37,035) victron_gatt_ble.py::72 Shunt1: Read value from characteristic <gatt.gatt_linux.Characteristic object at 0xb39c6628>
[DEBUG  ] (2021-12-30 00:40:37,293) victron_gatt_ble.py::72 Shunt1: Read value from characteristic <gatt.gatt_linux.Characteristic object at 0xb39c6760>
[DEBUG  ] (2021-12-30 00:40:37,308) victron_gatt_ble.py::72 Shunt1: Read value from characteristic <gatt.gatt_linux.Characteristic object at 0xb39c6130>
[DEBUG  ] (2021-12-30 00:40:37,323) victron_gatt_ble.py::72 Shunt1: Read value from characteristic <gatt.gatt_linux.Characteristic object at 0xb39c6d30>
[DEBUG  ] (2021-12-30 00:40:37,339) victron_gatt_ble.py::72 Shunt1: Read value from characteristic <gatt.gatt_linux.Characteristic object at 0xb39c6250>
[DEBUG  ] (2021-12-30 00:40:37,354) victron_gatt_ble.py::72 Shunt1: Read value from characteristic <gatt.gatt_linux.Characteristic object at 0xb39c6460>
[DEBUG  ] (2021-12-30 00:40:37,369) victron_gatt_ble.py::72 Shunt1: Read value from characteristic <gatt.gatt_linux.Characteristic object at 0xb39c6100>
[DEBUG  ] (2021-12-30 00:40:37,384) victron_gatt_ble.py::72 Shunt1: Read value from characteristic <gatt.gatt_linux.Characteristic object at 0xb39c6688>
[DEBUG  ] (2021-12-30 00:40:37,399) victron_gatt_ble.py::72 Shunt1: Read value from characteristic <gatt.gatt_linux.Characteristic object at 0xb39c6160>
[DEBUG  ] (2021-12-30 00:40:37,417) victron_gatt_ble.py::72 Shunt1: Read value from characteristic <gatt.gatt_linux.Characteristic object at 0xb39c6070>
[DEBUG  ] (2021-12-30 00:40:37,432) victron_gatt_ble.py::72 Shunt1: Read value from characteristic <gatt.gatt_linux.Characteristic object at 0xb39c60d0>
[INFO   ] (2021-12-30 00:40:37,592) victron_gatt_ble.py::53 Shunt1: Disconnect successful!
[DEBUG  ] (2021-12-30 00:40:37,605) victron_ble.py::80 Shunt1: Will reconnect at 2021-12-30 00:41:37.605105

I'm running on a Pi Zero W

Distributor ID: Raspbian
Description:    Raspbian GNU/Linux 11 (bullseye)
Release:        11
Codename:       bullseye

SmartShunt 500A/50mV Firmware: 4.08 Bluetooth enabled = true Bluetooth GATT service enabled = true

I've swapped from my desired base topic to the example one in config.yml. The Pi is sitting less than a foot away from the Shunt (I could connect when it was further away plugged into mains, but it would never read the characteristics so I moved it to my inverter).

I'm going to keep playing with it more tomorrow and add some extra debug lines for troubleshooting.

Any help is much appreciated!

oedo808 commented 2 years ago

In bluetooth the logs are far more verbose, but it's still not publishing anything to the victron topic.

Here is an example of one of the HomeAssistant topics that shows there the victron topic should be:

{"unique_id": "Shunt1_Used Energy_victron", "name": "Shunt1 Used Energy", "device_class": "current", "unit_of_measurement": "Ah", "state_class": "measurement", "json_attributes_topic": "victron/Shunt1/Used Energy", "state_topic": "victron/Shunt1/Used Energy", "device": {"identifiers": ["victron_Shunt1"], "manufacturer": "Victron", "model": "Currently not available via BLE Serial: Currently not available via BLE", "name": "Shunt1", "sw_version": "Currently not available via BLE"}}

image MQTT_Explorer_p9QXrmtj9W

oedo808 commented 2 years ago

I wound up implementing characteristic_read_value_failed in victron_gatt_ble.py to try to find out why characteristic_value_updated was never called and got something interesting: read failed on characteristic 65970ffe-4bda-4c1e-af4b-551c4cf74769 | merror: Operation failed with ATT error: 0x0e After that the other characteristic reads fail because of error merror: Not connected

65970ffe-4bda-4c1e-af4b-551c4cf74769 isn't in the victron_smartshunt_ble.py MAP so I guess maybe it's an undocumented characteristic.

Googling the ATT error is interesting:

ATT_ERR_UNLIKELY   0x0e

    The attribute request that was requested has encountered an error that was very unlikely, and therefore could not be completed as requested.
oedo808 commented 2 years ago

So it's nothing to do with an undocumented characteristic. I added a few lines to only call read_value on anything within the victron_smartshunt_ble.py MAP object and failed on one that is in there with the same error.

[DEBUG  ] (2022-01-06 22:23:10,960) victron_gatt_ble.py::85 Undocumented characteristic: 65970ffe-4bda-4c1e-af4b-551c4cf74769                                                                                                                                                                                          -
[DEBUG  ] (2022-01-06 22:23:10,964) victron_gatt_ble.py::85 Undocumented characteristic: 65970383-4bda-4c1e-af4b-551c4cf74769                                                                                                                                                                                          #
[DEBUG  ] (2022-01-06 22:23:10,967) victron_gatt_ble.py::85 Undocumented characteristic: 65970382-4bda-4c1e-af4b-551c4cf74769
[DEBUG  ] (2022-01-06 22:23:10,975) victron_gatt_ble.py::85 Undocumented characteristic: 6597edec-4bda-4c1e-af4b-551c4cf74769
[DEBUG  ] (2022-01-06 22:23:10,979) victron_gatt_ble.py::85 Undocumented characteristic: 6597ed7d-4bda-4c1e-af4b-551c4cf74769
[WARNING] (2022-01-06 22:23:11,078) victron_gatt_ble.py::68 read failed on characteristic 6597eeff-4bda-4c1e-af4b-551c4cf74769 | merror: Operation failed with ATT error: 0x0e
[DEBUG  ] (2022-01-06 22:23:11,087) victron_gatt_ble.py::82 Shunt1: Read value from characteristic:6597eeff-4bda-4c1e-af4b-551c4cf74769;value:None
[WARNING] (2022-01-06 22:23:11,103) victron_gatt_ble.py::68 read failed on characteristic 6597eeff-4bda-4c1e-af4b-551c4cf74769 | merror: Not connected
[WARNING] (2022-01-06 22:23:11,132) victron_gatt_ble.py::68 read failed on characteristic 6597ed8c-4bda-4c1e-af4b-551c4cf74769 | merror: Not connected
[DEBUG  ] (2022-01-06 22:23:11,137) victron_gatt_ble.py::82 Shunt1: Read value from characteristic:6597ed8c-4bda-4c1e-af4b-551c4cf74769;value:None
[WARNING] (2022-01-06 22:23:11,146) victron_gatt_ble.py::68 read failed on characteristic 6597ed8c-4bda-4c1e-af4b-551c4cf74769 | merror: Not connected
[WARNING] (2022-01-06 22:23:11,158) victron_gatt_ble.py::68 read failed on characteristic 6597ed8e-4bda-4c1e-af4b-551c4cf74769 | merror: Not connected
[DEBUG  ] (2022-01-06 22:23:11,162) victron_gatt_ble.py::82 Shunt1: Read value from characteristic:6597ed8e-4bda-4c1e-af4b-551c4cf74769;value:None
[WARNING] (2022-01-06 22:23:11,170) victron_gatt_ble.py::68 read failed on characteristic 6597ed8e-4bda-4c1e-af4b-551c4cf74769 | merror: Not connected
[WARNING] (2022-01-06 22:23:11,186) victron_gatt_ble.py::68 read failed on characteristic 6597ed8d-4bda-4c1e-af4b-551c4cf74769 | merror: Not connected
[DEBUG  ] (2022-01-06 22:23:11,193) victron_gatt_ble.py::82 Shunt1: Read value from characteristic:6597ed8d-4bda-4c1e-af4b-551c4cf74769;value:None
[WARNING] (2022-01-06 22:23:11,203) victron_gatt_ble.py::68 read failed on characteristic 6597ed8d-4bda-4c1e-af4b-551c4cf74769 | merror: Not connected
[WARNING] (2022-01-06 22:23:11,216) victron_gatt_ble.py::68 read failed on characteristic 65970fff-4bda-4c1e-af4b-551c4cf74769 | merror: Not connected
[DEBUG  ] (2022-01-06 22:23:11,221) victron_gatt_ble.py::82 Shunt1: Read value from characteristic:65970fff-4bda-4c1e-af4b-551c4cf74769;value:None
[WARNING] (2022-01-06 22:23:11,229) victron_gatt_ble.py::68 read failed on characteristic 65970fff-4bda-4c1e-af4b-551c4cf74769 | merror: Not connected
[DEBUG  ] (2022-01-06 22:23:11,232) victron_gatt_ble.py::85 Undocumented characteristic: 6597ffff-4bda-4c1e-af4b-551c4cf74769
[DEBUG  ] (2022-01-06 22:23:11,237) victron_gatt_ble.py::77 service.uuid:306b0001-b081-4037-83dc-e59fcc3cdfd0
[DEBUG  ] (2022-01-06 22:23:11,241) victron_gatt_ble.py::77 service.uuid:97580001-ddf1-48be-b73e-182664615d8e
[DEBUG  ] (2022-01-06 22:23:11,244) victron_gatt_ble.py::77 service.uuid:68c10001-b17f-4d3a-a290-34ad6499937c
[DEBUG  ] (2022-01-06 22:23:11,247) victron_gatt_ble.py::77 service.uuid:00001801-0000-1000-8000-00805f9b34fb
[INFO   ] (2022-01-06 22:23:12,517) victron_gatt_ble.py::55 Shunt1: Disconnect successful!
oedo808 commented 2 years ago

I also receive an odd error trying to connect with gatttool, but I might be doing something wrong:

handle: 0x0039, char properties: 0x12, char value handle: 0x003a, uuid: 6597ed8c-4bda-4c1e-af4b-551c4cf74769
...
[D1:43:04:AA:84:9A]      connect
Attempting to connect to D1:43:04:AA:84:9A
Connection successful
[D1:43:04:AA:84:9A][LE]> primary
attr handle: 0x0001, end grp handle: 0x0009 uuid: 00001800-0000-1000-8000-00805f9b34fb
attr handle: 0x000a, end grp handle: 0x000d uuid: 00001801-0000-1000-8000-00805f9b34fb
attr handle: 0x000e, end grp handle: 0x0013 uuid: 68c10001-b17f-4d3a-a290-34ad6499937c
attr handle: 0x0014, end grp handle: 0x001e uuid: 97580001-ddf1-48be-b73e-182664615d8e
attr handle: 0x001f, end grp handle: 0x0028 uuid: 306b0001-b081-4037-83dc-e59fcc3cdfd0
attr handle: 0x0029, end grp handle: 0xffff uuid: 65970000-4bda-4c1e-af4b-551c4cf74769
[D1:43:04:AA:84:9A][LE]> char-read-hnd 3a
Error: Characteristic value/descriptor read failed: Request attribute has encountered an unlikely error
birdie1 commented 2 years ago

Sorry for my late answer. I have it installed at my camper van which I am not using at the moment. I hope to have free time next weekend and have a look if I can reproduce the error.

If I remember correctly I got sometimes "merror: Not connected" when I changed the pin of the victron device but didn't remove and rejoined it at the raspberry bluetooth. Could it be your problem?

oedo808 commented 2 years ago

No worries at all. I was wondering about the bluetooth PIN and it's what I was missing. I feel like a bit of a dunce, I was looking everywhere on how to supply the pin with gatt or another python library and didn't even think to just use something like bluetoothctl to pair. That did it though, I'm reading values and they're coming through via MQTT. Thanks @birdie1!

birdie1 commented 2 years ago

Great to hear that it is working for you now. I will open a little FAQ section in the README and add this as a point.