HTU21D when using HA inside docker on Ubuntu

Hello community,

I am running Home Assistant inside a (the official) docker container on a Raspberry Pi 3B+. The operating system is Ubuntu 20.04 for Raspberry Pi.
I have connected a HTU21D temperature/moisture sensor via the i2c bus.
The sensor shows up under the expected address:

ubuntu@raspi:~$ i2cdetect -y 1
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: – – – – – – – – – – – – –
10: – – – – – – – – – – – – – – – –
20: – – – – – – – – – – – – – – – –
30: – – – – – – – – – – – – – – – –
40: 40 – – – – – – – – – – – – – – –
50: – – – – – – – – – – – – – – – –
60: – – – – – – – – – – – – – – – –
70: – – – – – – – –

I’m not entirely sure which user runs the docker process, so I added the user under which i created/started the docker container to the i2c group.
I passed the i2c device to the docker container with " --device /dev/i2c-1 " and added " - platform: htu21d " to the sensor section in my configuration.yaml.

The HA logs report:

Logger: homeassistant.components.htu21d.sensor
Source: /usr/local/lib/python3.8/site-packages/i2csense/init .py:58
Integration: htu21d (Removed integration - Home Assistant),

Bad reading: 5

and

Logger: homeassistant.components.htu21d.sensor
Source: components/htu21d/sensor.py:45
Integration: htu21d (Removed integration - Home Assistant),

HTU21D sensor not detected in bus 1

Any help would be highly appreciated!

Best regards
FanHi

I got same error with a RPI4 with Raspberry Pi OS Lite. i2cdetect shows a couple sensors:

pi@9f55bbfd:~/rpi-netboot $ i2cdetect -y 1
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: – – – – – – – – – – – – –
10: – – – – – – – – – – – – – – – –
20: – – – 23 – – – – – – – – – – – –
30: – – – – – – – – – – – – – – – –
40: 40 – – – – – – – – – – – – – – –
50: – – – – – – – – – – – – – – – –
60: – – – – – – – – – – – – – – – –

But log shows error:

2021-07-06 19:54:25 ERROR (SyncWorker_1) [homeassistant.components.htu21d.sensor] Bad reading: 5

2021-07-06 19:54:25 ERROR (MainThread) [homeassistant.components.htu21d.sensor] HTU21D sensor not detected in bus 1

Although there’s another I2C luminosity sensor bh1750 that is working fine and showing on Homeassistant UI

sensor:
  - platform: htu21d
    name: tank_htu21d
  - platform: bh1750
    name: tank_bh1750

Hi,

i haven’t really found a solution - but a workaround.
Some other threads suggest, that the i2c library used by HA is the problem.
(see https://community.home-assistant.io/t/htu21d-on-raspberry-pi/33132)
Some of the threads which discuss similar behaviour are a couple years old and there has not been any fix since - so I looked for an alternative solution.

I built the HTU21D sample from https://github.com/leon-anavi/rpi-examples

I then created a cron job which runs above sample program every minute and writes the output to a textfile which I then passed through to my docker container.
I then created a command_line sensor which reads the content of the file.
After slightly rewriting the output of the sample program to output a json format, everything now works fine.