rpeyron / blelt2mqtt

a daemon to decode LT Thermometer devices and send through MQTT to Domoticz / Home Assistant
5 stars 0 forks source link

endless loop ? #1

Open Molino69 opened 1 month ago

Molino69 commented 1 month ago

Thanks a lot for bringing this solution. I was struggeling with bluepy too, though I got it working somehow with simplepyble. But this code works better. I notice though that the code connects to my first DEVICE (I have 3 devices defined in the config file), and never disconnects to proceed to the next device. Seems like it enters in an endless loop. Are these lines maybe the reason for this endless loop :

            while c.is_connected:
                await asyncio.sleep(0.1)

Should it be "if c.is_connected:" and then wait for 5 or 10 seconds to receive a notification instead ?

rpeyron commented 1 month ago

Hello,

That is the first time I am using the async python module, and I may not use it correctly. The intention was to create one task per device, each one with endless loop, and all running simultaneously. When reading again the doc, I may have missed the asyncio.create_task in line await asyncio.gather(*[asyncio.create_task(deviceConnect(definition)) for definition in config.DEVICES])

Anyway, your issue has made me see that I did not commit the last change I made on this project: as this first version was draining the battery very hard, I introduced a wait parameter that disconnects the devices once a measure has been done. As a side effect, it should fix the issue you are encountering. I have committed it, please try and tell me if it fixes the problem. You will need to change your configuration file to add the wait parameter.

If it is not enough, you can try to change the line 177 with the one above with asyncio.create_task

Please note that I had not much luck in the end with those cheap thermometers, as one died very shortly, and even with the addition of the wait argument, the other one's battery was drained too fast (lasting only a few days), so I am not using those anymore, and unfortunately I am not able to test any more changes on this script.

Molino69 commented 1 month ago

Grand merci, Remi. Hmm, the battery lifetime is a concern. Probably the connecting process to the device, then activating notification and reading the values consumes a lot of energy. Certainly if the device is not deconnecting. I tried to understand which processor is being used in the device, but I couldn't find it. Probably a reflash of the firmware and make the BLE notifying as standard setting, would be the solution. Not sure. Anyway, I will give it a try and experiment a little bit with the code (though I only recently started to use Python). If it's ok with you, I'll report the results here on your page ? Cheers, Filip (I'm a Belgian, working in South Korea at the moment)

Molino69 commented 1 month ago

Hello Remi, I've been playing with the code and got it working somehow now... The idea is that the python program tries to connect to the devices in a sequential way, once connected it waits for receiving the notification during a certain waiting time (this time is defined in the config.py file), and after a timeout or after receiving the notification (which is send to MQTT server), the program continues to the next device or the program stops after the last device. I plan to use cron to run the program for example every 30 minutes or so... Let's see how fast the battery drains. Next thing will be to try incorporating the devices in HA (I have HA running in a docker container on raspberry pi 4B). Attached is the code I'm using now... ble-lt-thermometer-v2.zip

Molino69 commented 1 month ago

Oh, in the idea to get your project and your effort being found with search engines like Google, let me add here the reference that I found on the PCB of the device and some pictures too. So this Bluetooth device is a temperature and humidity sensor with a square LCD screen and with inscription "0726B V6" on the PCB. It's being sold on Aliexpress and on the box it says "CX-0726S" and the provider suggests to use the app QAQA. Attached are some pictures of the device. 20240515_183820 20240515_183838 20240515_195531 20240615_223450

rpeyron commented 1 month ago

Hello, thanks for the info, and of course I am OK and interested in your results. I have checked my PCB version, and it is the v5 version of the 0726B board (bought 2 years ago). So maybe the battery issue has been fixed in your version.

If I am right, the auto-discovery plugin of domoticz is based on the Home Assistant discovery protocol. So with a bit of luck, the integration in Home Assistant should be pretty easy (but as I am not using HA, I have not tested)

Molino69 commented 1 month ago

hello again, I did some more experimenting with the code, did some finetuning : sometimes a timeout error occurred and the device remained connected. Latest python code update is here below in the zip file. I defined the sensor in the configuration.yaml file of Homeassistant as here below :

mqtt:

  sensor:
      - name: "LT_1B0D T"
        device_class: "temperature"
        unique_id: "LT1B0DT"
        state_topic: "lt_temp/LT_1B0D/state"
        value_template: "{{ value_json.temperature if value_json.temperature is defined else states('sensor.LT1B0DT')}}"
        unit_of_measurement: "°C"
      - name: "LT_1B0D H"   
        device_class: "humidity"
        unique_id: "LT1B0DH"
        state_topic: "lt_temp/LT_1B0D/state"
        unit_of_measurement: "%"
        value_template: "{{ value_json.humidity if value_json.humidity is defined else states('sensor.LT1B0DH')}}"
      - name: "LT_1B0D B"
        device_class: "battery"
        unique_id: "LT1B0DB"
        state_topic: "lt_temp/LT_1B0D/state"
        unit_of_measurement: "%"
        value_template: "{{ value_json.battery if value_json.battery is defined else states('sensor.LT1B0DB')}}"

(sorry if the indentation is not correct here... I'm not very handy with this editor)

With this definition in Homeassistant, there is no further need to handle the discovery in the code. So I could simplify the code a bit more. As mentioned before, I use crontab to run the code every 30 minutes. This is the line I wrote via "crontab -e" :

10,40 bash ~/blelt2mqtt/blelt.sh >> ~/blelt2mqtt/blelt.logs 2>&1

It lauches the shell script ever 10 and ever 40 minutes after the hour (00:10, 00:40, 01:10, 01:40,...)

Note that my shell script in fact now only starts the python code, which seems to an unnecessary step, but I preferred to keep that intermediate step because now it's possible to do more things in that script if you want. This is my content of the blelt.sh script now :

python3 ~/blelt2mqtt/ble-lt-thermometer.py 2>&1 >> ~/blelt2mqtt/blelt.logs

So far everything is working good...

ble-lt-thermometer.zip

Thanks a lot Remi for your work and your support.