RPGPIO not importing. Says can only be run on Raspberry Pi, but that's what I'm using

I added a GPIO switch in the configuration yaml like this:

switch:
  - platform: rpi_gpio
    ports:
      21:Downstairs LR

But when I start, the log gives the following error

2020-12-02 16:43:09 ERROR (MainThread) [homeassistant.bootstrap] Error setting up integration switch - received exception
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/config_entries.py", line 827, in async_forward_entry_setup
    result = await async_setup_component(self.hass, domain, self._hass_config)
  File "/usr/src/homeassistant/homeassistant/setup.py", line 57, in async_setup_component
    return await setup_tasks[domain]  # type: ignore
  File "/usr/src/homeassistant/homeassistant/setup.py", line 64, in async_setup_component
    return await task  # type: ignore
  File "/usr/src/homeassistant/homeassistant/setup.py", line 174, in _async_setup_component
    processed_config = await conf_util.async_process_component_config(
  File "/usr/src/homeassistant/homeassistant/config.py", line 823, in async_process_component_config
    platform = p_integration.get_platform(domain)
  File "/usr/src/homeassistant/homeassistant/loader.py", line 424, in get_platform
    cache[full_name] = self._import_platform(platform_name)
  File "/usr/src/homeassistant/homeassistant/loader.py", line 429, in _import_platform
    return importlib.import_module(f"{self.pkg_path}.{platform_name}")
  File "/usr/local/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/usr/src/homeassistant/homeassistant/components/rpi_gpio/__init__.py", line 2, in <module>
    from RPi import GPIO  # pylint: disable=import-error
  File "/usr/local/lib/python3.8/site-packages/RPi/GPIO/__init__.py", line 23, in <module>
    from RPi._GPIO import *
RuntimeError: This module can only be run on a Raspberry Pi!

I’ve tried installing rpi.gpio_common using apt install.

I tried installing rpi.gpio using pip install. I’m fairly sure I have it installed, but it’s not working.

I have HA installed using docker on a Ras Pi 4. The base OS is Raspberry Pi OS, and I’m pretty sure its the 32-bit version, which from what I was reading is important it seems.

What can I do to troubleshoot this more?

This may help you

This may be useful in the future, but it seems to be a step ahead of where I currently am. I can’t even get HA to recognize GPIO pins at all. It doesn’t seem to be an issue of the pins themselves not working secondary to a pull-up/down issue. But HA doesn’t even seem to be loading the RPi.GPIO modules. But it was helpful, because my rpi does default to pull-down, and I tried changing like this suggested. But that doesn’t solve my current issue.

Well, since you’re using docker…is a different OS essentially. You need to give the docker container access to the gpio. The easiest would be to just make the container privileged to give it full access to the raspbian OS. The next way would be to only give it access to the gpio devices.

I don’t use the docker image so I’m not 100% on this. But the docker container is going to have to get access to the gpio device somehow. I assume it wouldn’t be much different than this:

(look at the zwave section)

You should be able to ssh into the container and try to run this manually without having to restart everything. Can just do python3, then try to call from RPi._GPIO import * and see how it goes.

1 Like

Perfect!! This was exactly the issue and I don’t know why I didn’t think about this. Of course the docker container does not have access. But if I run it with --privileged it works exactly as expected.

Thanks so much!

I have the identical problem with my RPI4b which I initially setup according the recommended installation method which seems to use Docker. I would strongly suspect that you are right about giving the automatically created Docker container access to the GPIO pins. But I have no idea how to do this as the docker container is not a self-managed one. Any clues what to do please? Here is the first part of the installation information page:

version: 2020.12.1
installation_type: Home Assistant OS
dev: false
hassio: true
docker: true
virtualenv: false
python_version: 3.8.6
os_name: Linux
os_version: 5.4.79-v7l
arch: armv7l
timezone: Australia/Melbourne

So I’m not sure I fully know how you installed. But it seems you need to access the pi through SSH. Stop the HA docker container, then restart it in privileged mode. That’s all I did.

Find out the name of the HA docker instance by running the following on the pi

docker container ls -a

Find where it says names, then stop that one.

docker stop NAME

Then restart. I don’t know what the ‘basic install’ docker command is. But this is what I use for mine, and should probably get you close to what you need.

docker run -d --init --name="homeassistant" --privileged -v /home/pi/Documents/HA:/config -v /ect/localtime:/etc/localtime:ro -e "TZ=America/New_York" -p 8123:8123 --device /dev/ttyUSB0:/dev/ttyUSB0 --device /dev/ttyUSB1:/dev/ttyUSB1 --res
tart unless-stopped --net=host homeassistant/home-assistant:stable

A couple things to note. Change your timezone. I created the Documents/HA folder where I store my config stuff, change that as needed. I also pass through the USB0 and USB1 devices because I use a dongle that has both Z-wave and Zigbee radios (I use zigbee). I’m pretty sure if you use --net=host then the -p 8123:8123 does nothing. But I haven’t confirmed that, and it works, and I haven’t had time to mess with it yet.

The important part (to my understanding) for GPIO access is the --privileged. I guess it is also wise to note that lots of places on the web say doing so is a “bad idea” because it basically gives root access from docker to your device (the pi). I’ve decided that risk is worth it with HA, especially as I don’t have my pi access the internet anyway. But you may want to read around to see if it’s actually a problem for you or not.

Hope that helps, I’ve never installed HA according to the “recommended installation method” so I don’t know exactly what that entails.

Thanks for your reply. I didn’t get anywhere with that approach as I get the message

-bash: docker: command not found

Here is what I meant by the ‘recommended installation method’


I now suspect that I have used the 64 bit image but it says on the page above that you need the 32-bit image for GPIO access. Maybe that’s the problem. There doesn’t seem to be any infornation avaialble on the HA interface which tells you which of 32 or 64 bit you have in fact installed. So I will start all over again with the 32-bit.

SUCCESS!! It looks like that was it. So you do have to install the 32-bit images to have GPIO access. I know they say that but I didn’t remember which image I installed many weeks ago and - correct me if I am wrong - there is nothing in HA that tells you which one you have installed.