core icon indicating copy to clipboard operation
core copied to clipboard

Xiaomi BLE plant sensor entities unavailable

Open nightmare1111 opened this issue 1 year ago • 40 comments

The problem

I have a Xiaomi BLE plant sensor . I had it working then this week some entities became unavailable, temp still works but no moisute fertility or lux. All these sensors are still broadcasting to the flower care android app.

What version of Home Assistant Core has the issue?

2023.4.2

What was the last working version of Home Assistant Core?

No response

What type of installation are you running?

Home Assistant OS

Integration causing the issue

Xiaomi ble

Link to integration documentation on our website

https://www.home-assistant.io/integrations/xiaomi_ble/

Diagnostics information

No response

Example YAML snippet

No response

Anything in the logs that might be useful for us?

Logger: homeassistant.components.recorder.util
Source: components/recorder/table_managers/statistics_meta.py:315
Integration: Recorder (documentation, issues)
First occurred: 4:25:40 PM (1 occurrences)
Last logged: 4:25:40 PM

Error executing query: (sqlite3.IntegrityError) UNIQUE constraint failed: statistics_meta.statistic_id [SQL: UPDATE statistics_meta SET statistic_id=? WHERE statistics_meta.statistic_id = ? AND statistics_meta.source = ?] [parameters: ('sensor.hanging_basket_temperature', 'sensor.plant_sensor_5d9a_temperature', 'recorder')] (Background on this error at: https://sqlalche.me/e/20/gkpj)
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 1964, in _exec_single_context
    self.dialect.do_execute(
  File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/default.py", line 748, in do_execute
    cursor.execute(statement, parameters)
sqlite3.IntegrityError: UNIQUE constraint failed: statistics_meta.statistic_id

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/recorder/util.py", line 129, in session_scope
    yield session
  File "/usr/src/homeassistant/homeassistant/components/recorder/statistics.py", line 695, in update_statistics_metadata
    statistics_meta_manager.update_statistic_id(
  File "/usr/src/homeassistant/homeassistant/components/recorder/table_managers/statistics_meta.py", line 315, in update_statistic_id
    ).update({StatisticsMeta.statistic_id: new_statistic_id})
  File "/usr/local/lib/python3.10/site-packages/sqlalchemy/orm/query.py", line 3284, in update
    self.session.execute(
  File "/usr/local/lib/python3.10/site-packages/sqlalchemy/orm/session.py", line 2229, in execute
    return self._execute_internal(
  File "/usr/local/lib/python3.10/site-packages/sqlalchemy/orm/session.py", line 2124, in _execute_internal
    result: Result[Any] = compile_state_cls.orm_execute_statement(
  File "/usr/local/lib/python3.10/site-packages/sqlalchemy/orm/bulk_persistence.py", line 1524, in orm_execute_statement
    return super().orm_execute_statement(
  File "/usr/local/lib/python3.10/site-packages/sqlalchemy/orm/context.py", line 253, in orm_execute_statement
    result = conn.execute(
  File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 1414, in execute
    return meth(
  File "/usr/local/lib/python3.10/site-packages/sqlalchemy/sql/elements.py", line 486, in _execute_on_connection
    return connection._execute_clauseelement(
  File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 1638, in _execute_clauseelement
    ret = self._execute_context(
  File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 1842, in _execute_context
    return self._exec_single_context(
  File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 1983, in _exec_single_context
    self._handle_dbapi_exception(
  File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 2326, in _handle_dbapi_exception
    raise sqlalchemy_exception.with_traceback(exc_info[2]) from e
  File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 1964, in _exec_single_context
    self.dialect.do_execute(
  File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/default.py", line 748, in do_execute
    cursor.execute(statement, parameters)
sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) UNIQUE constraint failed: statistics_meta.statistic_id
[SQL: UPDATE statistics_meta SET statistic_id=? WHERE statistics_meta.statistic_id = ? AND statistics_meta.source = ?]
[parameters: ('sensor.hanging_basket_temperature', 'sensor.plant_sensor_5d9a_temperature', 'recorder')]
(Background on this error at: https://sqlalche.me/e/20/gkpj)

Additional information

The entities were unavailable after deleteing the integration and reinstaling it the unavailable entities are no longer visible at all

nightmare1111 avatar Apr 11 '23 15:04 nightmare1111

Hey there @jc2k, @ernst79, mind taking a look at this issue as it has been labeled with an integration (xiaomi_ble) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of xiaomi_ble can trigger bot actions by commenting:

  • @home-assistant close Closes the issue.
  • @home-assistant rename Awesome new title Renames the issue.
  • @home-assistant reopen Reopen the issue.
  • @home-assistant unassign xiaomi_ble Removes the current integration label and assignees on the issue, add the integration domain after the command.

(message by CodeOwnersMention)


xiaomi_ble documentation xiaomi_ble source (message by IssueLinks)

home-assistant[bot] avatar Apr 11 '23 15:04 home-assistant[bot]

+1 Same problem. Core 2023.4.4

VendettaPrime avatar Apr 14 '23 20:04 VendettaPrime

Same issue when upgraded from Core 2023.3.6 to Core 2023.4.x (still occurs on 2023.4.4) but with BLE Mi Temperature and Humidity Monitor 2

123jjck avatar Apr 15 '23 05:04 123jjck

Do you all have the SQL error seen in the first post?

If you do, what database are you using? SQLite? Maria?

Jc2k avatar Apr 15 '23 07:04 Jc2k

I use MariaDB. In my case - I don't see any errors - just the message that the device is unavailable%. Comes after upgrade Core form 2023.3.5 to 2023.4.2 and after 2023.4.4

VendettaPrime avatar Apr 15 '23 23:04 VendettaPrime

Seems good now thanks

nightmare1111 avatar Apr 17 '23 19:04 nightmare1111

now i have fertility but all other sensors are off, the mi flora app still shows all entities working

nightmare1111 avatar Apr 21 '23 21:04 nightmare1111

I have the same problem since today. Around 9 am I lost the connection, it came back around 1 pm and lost it again at 3 pm. Does anyone have a solution?

jip98 avatar Apr 23 '23 17:04 jip98

Did you install Home Assistant OS 10 recently? There are some Bluetooth issues reported with the 10.0 release. If so, you might try to move back to 9.5.

https://github.com/home-assistant/operating-system/issues/2485

Ernst79 avatar Apr 23 '23 17:04 Ernst79

Yes, I did. I hope that's the problem. I will wait for a fix and if it takes too long go back to 9.5. Thank you!

jip98 avatar Apr 23 '23 17:04 jip98

I tried it at 10.1 and problem persist. At 9.5 (2023.4.6) is all working well.

kocicky avatar May 01 '23 18:05 kocicky

That is because https://github.com/home-assistant/operating-system/issues/2485 isn’t solved yet. Please post all relevant info in that topic, as this has to be solved in Home Assistant OS.

Ernst79 avatar May 01 '23 20:05 Ernst79

+1 mismo problema...

apalau88 avatar May 04 '23 14:05 apalau88

+1 Same problem. Core 2023.4.4

I'm not using Home Assistant OS, I use Home Assistant Supervisor install on rpi4. Host OS - Debian. Core 2023.5.2 - problem still here

VendettaPrime avatar May 06 '23 23:05 VendettaPrime

Without further info it will be inpossible to tell what is wrong. If you did not install HA OS 10.1, it could be wifi interference, bad range, failing Bluetooth dongle. Try to enable debug logging for the Bluetooth integration to see if you receive data of the sensor at all.

Ernst79 avatar May 07 '23 07:05 Ernst79

I think I face the same error but with a different device and a newer version of Home Assistant 2023.6.3 with Supervisor 2023.06.2 and Operating System 10.3 on a Raspberry Pi 4:

2023-06-24 10:41:43.083 ERROR (Recorder) [homeassistant.components.recorder.util] Error executing query: (sqlite3.IntegrityError) UNIQUE constraint failed: statistics_meta.statistic_id
[SQL: UPDATE statistics_meta SET statistic_id=? WHERE statistics_meta.statistic_id = ? AND statistics_meta.source = ?]
[parameters: ('sensor.schalter_schlafzimmer_bett_battery_3', 'sensor.philips_rwl021_battery_3', 'recorder')]
(Background on this error at: https://sqlalche.me/e/20/gkpj)
Traceback (most recent call last):
  File "/usr/local/lib/python3.11/site-packages/sqlalchemy/engine/base.py", line 1968, in _exec_single_context
    self.dialect.do_execute(
  File "/usr/local/lib/python3.11/site-packages/sqlalchemy/engine/default.py", line 920, in do_execute
    cursor.execute(statement, parameters)
sqlite3.IntegrityError: UNIQUE constraint failed: statistics_meta.statistic_id

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/recorder/util.py", line 130, in session_scope
    yield session
  File "/usr/src/homeassistant/homeassistant/components/recorder/statistics.py", line 680, in update_statistics_metadata
    statistics_meta_manager.update_statistic_id(
  File "/usr/src/homeassistant/homeassistant/components/recorder/table_managers/statistics_meta.py", line 316, in update_statistic_id
    ).update({StatisticsMeta.statistic_id: new_statistic_id})
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/sqlalchemy/orm/query.py", line 3275, in update
    self.session.execute(
  File "/usr/local/lib/python3.11/site-packages/sqlalchemy/orm/session.py", line 2232, in execute
    return self._execute_internal(
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/sqlalchemy/orm/session.py", line 2127, in _execute_internal
    result: Result[Any] = compile_state_cls.orm_execute_statement(
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/sqlalchemy/orm/bulk_persistence.py", line 1619, in orm_execute_statement
    return super().orm_execute_statement(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/sqlalchemy/orm/context.py", line 293, in orm_execute_statement
    result = conn.execute(
             ^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/sqlalchemy/engine/base.py", line 1413, in execute
    return meth(
           ^^^^^
  File "/usr/local/lib/python3.11/site-packages/sqlalchemy/sql/elements.py", line 483, in _execute_on_connection
    return connection._execute_clauseelement(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/sqlalchemy/engine/base.py", line 1637, in _execute_clauseelement
    ret = self._execute_context(
          ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/sqlalchemy/engine/base.py", line 1846, in _execute_context
    return self._exec_single_context(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/sqlalchemy/engine/base.py", line 1987, in _exec_single_context
    self._handle_dbapi_exception(
  File "/usr/local/lib/python3.11/site-packages/sqlalchemy/engine/base.py", line 2344, in _handle_dbapi_exception
    raise sqlalchemy_exception.with_traceback(exc_info[2]) from e
  File "/usr/local/lib/python3.11/site-packages/sqlalchemy/engine/base.py", line 1968, in _exec_single_context
    self.dialect.do_execute(
  File "/usr/local/lib/python3.11/site-packages/sqlalchemy/engine/default.py", line 920, in do_execute
    cursor.execute(statement, parameters)
sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) UNIQUE constraint failed: statistics_meta.statistic_id
[SQL: UPDATE statistics_meta SET statistic_id=? WHERE statistics_meta.statistic_id = ? AND statistics_meta.source = ?]
[parameters: ('sensor.schalter_schlafzimmer_bett_battery_3', 'sensor.philips_rwl021_battery_3', 'recorder')]
(Background on this error at: https://sqlalche.me/e/20/gkpj)

gotaslug avatar Jun 25 '23 05:06 gotaslug

similar issue ... out of 10 HHCCJCY01 flower sensors 3 are showing up in HA Xiaomi BLE but 7 not.

all sensors use the 3.3.5 firmware alle sensors placed next to same ESPHome Bluetooth Proxie

MonsterD1 avatar Sep 18 '23 08:09 MonsterD1

same problem for flood sensor OC29 It is only creating a signal strength sensor.

canedje avatar Oct 15 '23 19:10 canedje

Same issue 3.3.5 firmware

dexif avatar Dec 17 '23 20:12 dexif

Is there anything in the logs? Try to enable debug logging for the Bluetooth integration and check if you receive data of the sensor. If you do, post the logs here, such that I can check it.

Ernst79 avatar Dec 17 '23 21:12 Ernst79

I have the same problem

Babidi92 avatar Jan 16 '24 17:01 Babidi92

I also have this issue, somewhen in February it stopped being detected

peteh avatar Feb 23 '24 18:02 peteh

Please, post debug logging of the xiaomi-ble integration and of the Bluetooth integration. And from the xiaomi-ble pypi package.

without logs, I can’t help you.

Ernst79 avatar Feb 23 '24 18:02 Ernst79

config_entry-bluetooth-d701cf4b919c4d1a7bffd74bfe6c188b.json

Unfortunately I deleted the Xiaomi BLE Extension in the hope it would rediscover the device. I'm running on the latest Homeassistant running in docker on Ubuntu 22.04 LTS

It looks like the scanning does not pick up the flowercare sensor for some reason. (should have xx:76 in the address)

peteh avatar Feb 23 '24 18:02 peteh

Did you try a new battery?

Ernst79 avatar Feb 23 '24 18:02 Ernst79

I got a bit closer to the issue:

  • The integrated intel controller does not detect the Flowercare sensor anymore but it does detect other devices, e.g. Xiaomi Humidity sensors that someone in the neighborhood seems to have
  • I added a TP Link usb bluetooth controller and the Flowercare sensor showed up immediately after configuring the new bt device in homeassistant bluetooth integration
  • the battery is relatively fresh and it is working on another raspberry pi and the flowercare app

So I guess the issue is with the intel controller for some reason. Any ideas why it might not like my sensor anymore?

peteh avatar Feb 23 '24 19:02 peteh

Most likely some interference of the Bluetooth radio and the WiFi radio or zigby radio. All act on 2.4 GHz and one can block the other.

Ernst79 avatar Feb 23 '24 19:02 Ernst79

I narrowed it down a bit more. It seems to be related to the linux kernel.

latest 6.6 and 6.7 branches seem not to detect Bluetooth LE devices with intel chips.

Downgrading to 6.1 makes it work again.

peteh avatar Feb 23 '24 20:02 peteh

It sometimes helps to do a full system reboot, not only HA. Like power off, power on.

Ernst79 avatar Feb 23 '24 20:02 Ernst79

I had to do this with the different kernels anyway. So I think this is not related. It looks like Intel AX210 drivers are just broken after 6.5/6.6 kernels.

peteh avatar Feb 23 '24 20:02 peteh