Sht31d sensor not working: i2c-1 error

I am running Home Assistant (0.86.4) under Docker on a Raspberry Pi. I have been trying for several days to get an Adafruit sht31d temperature sensor to work. It works under Python on the Raspberry Pi: will happily output temperature and humidity to the command line, so I know that things like i2c are working properly at the basic hardware level. However, Home Assistant gives me the following error:

Error while setting up platform sht31
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/entity_platform.py", line 128, in _async_setup_platform
    SLOW_SETUP_MAX_WAIT, loop=hass.loop)
  File "/usr/local/lib/python3.6/asyncio/tasks.py", line 358, in wait_for
    return fut.result()
  File "/usr/local/lib/python3.6/concurrent/futures/thread.py", line 56, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/sensor/sht31.py", line 54, in setup_platform
    sensor = SHT31(address=i2c_address)
  File "/usr/local/lib/python3.6/site-packages/Adafruit_SHT31.py", line 61, in __init__
    self._device = i2c.get_i2c_device(address, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/Adafruit_GPIO/I2C.py", line 64, in get_i2c_device
    return Device(address, busnum, i2c_interface, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/Adafruit_GPIO/I2C.py", line 97, in __init__
    self._bus = Adafruit_PureIO.smbus.SMBus(busnum)
  File "/usr/local/lib/python3.6/site-packages/Adafruit_PureIO/smbus.py", line 105, in __init__
    self.open(bus)
  File "/usr/local/lib/python3.6/site-packages/Adafruit_PureIO/smbus.py", line 130, in open
    self._device = open('/dev/i2c-{0}'.format(bus), 'r+b', buffering=0)
FileNotFoundError: [Errno 2] No such file or directory: '/dev/i2c-1'

When I ssh to the command line, /dev/i2c-1 is present. I have tried the various fixes suggested in these pages including this one. Nothing seems to work. I donā€™t think there is anything wrong with my configuration.yaml file which sticks closely to the HA recommendation, but here it is just in case:

sensor:
  - platform: sht31
    name: Living room ambience
    i2c_address: 0x44
    monitored_conditions:
      - temperature
      - humidity 

Does anyone have any suggestions?

Can you see that device from within the Docker container? That is where Home Assistant is located.

Thanks, @anon34565116. No, I have not been able to identify it within the docker container. I have searched in the /var/lib/docker/overlay2 directories, without turning anything up. Iā€™m too much of newbie with Docker to know where the device operations take place or indeed how to adjust the containerā€™s equivalent of config.txt (if that is what is required). Any pointers would be very welcome.

I am just learning HA & Docker.

ā€œdocker container lsā€ should list all the Docker containers.

Yes, I can list the containers, but it doesnā€™t help me to locate /dev/ or /boot/config.txt for purposes of a particular container.

I am also running Portainer under Docker, and have used the console which it provides for the Home Assistant container to run pip3 install adafruit-circuitpython-sht31d. It seems to carry out most of the operation, but ends up with a series of error messages, which leads me to conclude that possibly this is not the correct way to tackle the problem. I also note that within the docker container at
/var/lib/docker/overlay2/xxxxxxxxxxxxxxx/diff/usr/local/lib/python3.6/site-packages
some at least of the necessary Python files seem to be present (such as Adafruit_SHT31.py and i2csense).

How to get i2c-1 running within the container is what is eluding me! :roll_eyes:

This is incredibly frustrating. I have tried just about everything, including complete re-installation on a fresh SD card, different Raspberry Pis (3 B, 3 B+, 3 A+), different versions of HA, re-installations of Python, all of the suggestions for tinkering with the config.txt file and the legacy rpi-i2c.conf files. I still get the same stubborn error:
FileNotFoundError: [Errno 2] No such file or directory: '/dev/i2c-1' (as above).

The sensor did work on an old installation of mine, where I used Noobs. I havenā€™t been using Noobs recently because of its lack of upgradability, but surely this shouldnā€™t make any difference if the core is the same (in my case 4.14.98-v7+)?

Very grateful for any suggestions, folks!

This link looks interesting. Perhaps NOOBS had this enabled by default.

You just need to enable with raspi-config & reboot.

Thanks @anon34565116 for your suggestion.
In fact, i2c is enabled, and the device is working perfectly well. I get the following before and after a restart of home-assistant on docker:

    pi@raspberrypi:~ $ ls -l /dev/i2c*
    crw-rw---- 1 root i2c 89, 1 Mar  8 20:09 /dev/i2c-1

And yet Home Assistant still tells me No such file or directory: ā€˜/dev/i2c-1ā€™. Further to that, if I use Python at the command line I can read the values from the sensor with no difficulty, proving that the device is working as it should. It is only Home Assistant that insists that the device is not there!

I wondered for a while if it was to do with permissions/ownership of the device, but that has been yet another blind alley.

Perhaps it cannot be seen within the homeassistant container then. Or user / group permissions to the device are causing issues.

Iā€™ve checked groups and permissions and they all seem fine.

I donā€™t think the device runs within the container: the container makes use of the device on the system, I think.
I have just re-checked. The user is pi, the home-assistant user is pi, and pi belongs to groups which include docker and i2c.

1 Like

SOLVED! Thanks to a suggestion from my son, I have identified the problem. It seems Docker needs specific settings to communicate with system devices, and so it is essential to include either the device management option --device=xxx or the broader --privileged= option when issuing the run instruction for the image. For other newbs like myself, do the following if you have home assistant up and running. If not, go to the third line.

$ docker stop home-assistant
$ docker rm home-assistant
$ docker run --init -d --privileged --name="home-assistant" -v /PATH_TO_YOUR_CONFIG:/config -v /etc/localtime:/etc/localtime:ro --net=host homeassistant/raspberrypi3-homeassistant

See here and here for more information.

It might help to include a note on this in the instruction pages for setting up sensors of this sort.