anyway to have BLE turned off (really turned off) and only turned on for sensor reading and then turned off again?
Hi, i had problems with bt temp sensors not giving new temp updates. I have updated with @Magalex his code and restarted but same problem still happens:( I have HA running on a Pi3, is it mabye smarter to collect the bt data through a ESP32?
To help, we need more information about what is happening on your system (logs, bt interface status, etc.)
Personally, I ran into a problem that was not related to the work of the integration discussed here. My problem is probably related to the operation of drivers and/or hardware (I have mac mini 2011).
I will describe my workaround. It will suit you if the execution of the commands sudo modprobe -r btusb
and then sudo modprobe btusb
(instead of btusb
in the case of the module built into raspberry pi this may be hci_uart
, but I’m not sure) will bring bluetooth to working condition.
- Add a binary sensor to the configuration (
mi_t_1
andmi_h_1
andmi_t_2
andmi_h_2
must be replaced with the names of your sensors). This sensor will go into the ON state when there are no readings from the bt-sensors for two minutes (you can change 120 to another value in seconds):
binary_sensor:
- platform: template
sensors:
bt_stuck:
friendly_name: "BT"
value_template: >-
{% set btvalues = [
states.sensor.mi_t_1.last_updated,
states.sensor.mi_t_2.last_updated,
states.sensor.mi_h_1.last_updated,
states.sensor.mi_h_2.last_updated]%}
{{ (as_timestamp(now())-as_timestamp(btvalues | max)) > 120 }}
device_class: problem
icon_template: >-
{% if is_state("binary_sensor.bt_stuck", "on") %}
mdi:bluetooth-off
{% else %}
mdi:bluetooth
{% endif %}
- Add the shell command to the configuration:
shell_command:
restart_bt: touch ~/btrestart.txt
- Create the btdown.sh script in the home folder (check the path to the file from the (2) in the
flagfile
variable):
#!/bin/bash
flagfile="/home/homeassistant/btrestart.txt"
if [ -f "$flagfile" ]
then
rm -rf $flagfile
/usr/sbin/modprobe -r btusb
/usr/sbin/modprobe btusb
else
:
fi
exit 0
- Add the root cron job (change the
/home/aleksey/btdown.sh
to the path to script from (3)):
# m h dom mon dow command
* * * * * /home/aleksey/btdown.sh
- Add automation to the configuration, which, when the bt_stuck sensor is triggered, will notify us and create a text flag-file, caught by the cron job:
- alias: BT restart
description: ''
trigger:
- entity_id: binary_sensor.bt_stuck
from: 'off'
platform: state
to: 'on'
condition: []
action:
- service: shell_command.restart_bt
- data:
message: BT restarted!
service: persistent_notification.create
As a result, after 2-3 minutes after stopping the sensors, the kernel module reloads and my bluetooth restores working state.
Personally, I have no experience with ESP32.
Added the binary_sensor and shell_command.
For the script file, cron job i will need to go to where the Pi is installed(my parents).
Going to try it this weekend!
@Magalex Is it an idea to make a custom component that can be installed with HACS, with a readme how to get it running. People now have to look through this topic, and combine two gists to get all the files together. If you don’t have time to do this, I’m willing to do this. But as you (and others) did the hard work, I thought I first ask you.
Somebody can than later try to convert it to a standard component. But I think that’s still some work, as the communication will probably have to be moved to a pypi package.
So, let me know if you want me to setup a custom component for HACS.
This question has already been raised earlier, and I wrote that I do not know the Home Assistant structure well enough to make the component. I am often on business trips, so I can not yet devote enough time to this. In addition, I still expect that @tsymbaliuk as the source of the idea of a passive method will finish this component himself (he wrote about this desire).
But since quite a long time has passed (3.5 months), I am inclined to support you, @Ernst, in your effort. Create a git repo, I will help than I can to do everything as expected.
Ok, I will do that coming week. I do have experience in creating an integration in home assistant, just finished my first (accepted) integration. But I’m not an experienced programmer, so it takes a bit more time. But it works well, even a config flow is included.
@tsymbaliuk If you have plans to set something up in a short time, let me know. I don’t want to interfere with your plans, of course. If you’re planning something, I will wait for that.
Hi, @Magalex!
Firstly, I would like to thank you for your component! Xiaomi Mijia works just great on Raspberry P4 with build-in Bluetooth. Though, the only problem which appears is Bluetooth tracking person stops working. Is there a possible way to fix it?
Thanks!
Are there any error messages in the log? I can not say anything about this problem. Which tracker do you use? bluetooth_le_tracker
or bluetooth_tracker
?
Thank you for your quick respond!
To your questions, no, i don’t have any errors in the log.
The tracked phone appears and disappears from time to time. With basic component there is no such problem. I’m using
bluetooth_tracker.
I’m afraid that this problem cannot be fixed at the moment … I suspect that since our component uses the bt-interface during all the time between measurements, and for this a low-level system utility with direct access to the interface is used, we are dealing with a certain conflict… I need to try to reproduce the problem, but now I’m on the road, and I have no opportunity for this. The plans have the task of looking for another method, but it will not be so soon. There is also the possibility that this conflict is fundamental, and will prove insoluble.
For now, I propose to ask the other participants if anyone uses our component in combination with a bluetooth-tracker? Are there anyone with such a combination that works without problems?
Hi Magalex, I followed tsymbaliuk instructions and I replaced sensor,py content with code from my gist. i have Raspberry PI 3 B+ with Raspbian+ Home Assistant (venv, virtual environment).
When I restart Home Assistant I have the following error:
Platform error: sensor
Traceback (most recent call last):
File “/srv/homeassistant/lib/python3.7/site-packages/homeassistant/config.py”, line 755, in async_process_component_config
platform = p_integration.get_platform(domain)
File “/srv/homeassistant/lib/python3.7/site-packages/homeassistant/loader.py”, line 232, in get_platform
f"{self.pkg_path}.{platform_name}"
File “/usr/lib/python3.7/importlib/init.py”, line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File “”, line 1006, in _gcd_import
File “”, line 983, in _find_and_load
File “”, line 967, in _find_and_load_unlocked
File “”, line 677, in _load_unlocked
File “”, line 728, in exec_module
File “”, line 219, in _call_with_frames_removed
File “/home/homeassistant/.homeassistant/custom_components/mitemp_bt/sensor.py”, line 17, in
import pexpect
ModuleNotFoundError: No module named ‘pexpect’
Can you help me understand the error and what to do to eliminate it, please?
Try to comment out this line of code (line 17) - import pexpect
replace to #import pexpect
and restart HA. There are several already unused imports that should be cleaned up … There will be an update soon.
I thank you for the quick reply. I have commented the line 17 and . I no longer have the error but there is no mi_XXXX sensors among the entities. What names should they have?
mi_t_xxxxxxxxxxxxx and mi_h_xxxxxxxxxxxxx. The first time the sensors appear after one or two measurement periods (60 seconds by default).
You can check for relevant processes using shell command ps -A | grep hci
. If there is no hcidump
process, then it makes sense to try shell command hcitool lescan --duplicates
and then hcidump --raw hci
, and then proceed based on the result of their execution.
Using shell command ps -A | grep hci
, the answer is been:
Stop HA. ps -A | grep hci
again. Kill hcitool and hcidump processes, if any (kill -9 <pid>
, where <pid>
is number from the left side of ps -a table.
Then open two shells and run the commands that I wrote in previous answer. What is the result of their execution?
Stopped HA. ps -A | grep hci
again. Answer:
sudo kill -9 2043. Answer:
kill: (2043): No such process
sudo kill -9 2044. Answer:
kill: (2044): No such process
sudo hcitool lescan --duplicates. Answer:
Set scan parameters failed: Input/output error
sudo hcidump --raw hci: Answer (sequence interrupted with ctrl + C):
HCI sniffer - Bluetooth packet analyzer ver 5.50
device: hci0 snap_len: 1500 filter: 0x2
04 3E 2B 02 01 02 01 F8 53 F3 77 36 CD 1F 02 01 04 1B FF 57
01 00 8E D0 A8 03 F4 8A 76 A1 F0 C1 58 A8 5E 36 F5 16 03 CD
36 77 F3 53 F8 B8
04 3E 23 02 01 04 01 F8 53 F3 77 36 CD 17 0A 09 4D 69 20 42
61 6E 64 20 33 03 02 E0 FE 07 16 E0 FE 00 00 00 00 B8
04 3E 28 02 01 02 01 DA F6 97 66 4A 52 1C 03 03 9F FE 17 16
9F FE 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 BA
04 3E 16 02 01 04 01 DA F6 97 66 4A 52 0A 09 FF E0 00 01 03
CA 79 6F F9 BB
04 3E 28 02 01 02 01 DA F6 97 66 4A 52 1C 03 03 9F FE 17 16
9F FE 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 BB
04 3E 16 02 01 04 01 DA F6 97 66 4A 52 0A 09 FF E0 00 01 03
CA 79 6F F9 BA
04 3E 28 02 01 02 01 DA F6 97 66 4A 52 1C 03 03 9F FE 17 16
9F FE 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 B8
Hmm… In your hcidump output, I do not see temperature/humidity data packets…
Try this:
hcidump --raw | grep -A 1 "02 01 06"
And tell me if there are any lines. And post here two/three longest from them (like these):
> 04 3E 25 02 01 00 00 21 0B 31 34 2D 58 19 02 01 06 15 16 95
FE 50 20 AA 01 D1 21 0B 31 34 2D 58 0D 10 04 E1 00 4A 01 BD
--
> 04 3E 25 02 01 00 00 CB 0A 31 34 2D 58 19 02 01 06 15 16 95
FE 50 20 AA 01 35 CB 0A 31 34 2D 58 0D 10 04 C8 00 58 01 AD
And immediately another questions - how far is the sensor from HA host? This is the same sensor as on the photo here?
The sensor is the same on the photo. Now I do the other test and let you know.
i have tried the cmd:
sudo hcidump --raw | grep -A 1 “02 01 06”
and the answer:
In the file sensors.yaml, i have this:
- platform: mitemp_bt
# mac: '58:2D:34:35:5C:86'
# name: Xiaomi T_U
# force_update: true
# median: 1
# monitored_conditions:
# - temperature
# - humidity
# - battery