BeautifulSoup doesn't work in a command line sensor

Hi all

I am trying to build my own scraper to gather information about water supply outages in my neighborhood. I need to search for specific strings inside a span, navigate to its parent link and then look for another string - a bit too complex to solve with the scraper component I guess.

The python script runs perfectly in the virtual environment I configured for hass in my Raspberry Pi. However, I get an error whenever the command line sensor is triggered. Since the only message I get in the logs is “command failed” I tried to simplify the code in order to detect what is wrong with it. I realized that a simple print(‘test’) works as expected, but as soon as BeautifulSoup is imported, the sensor execution fails.

sensor.yaml

  • platform: command_line
    name: Scraper
    command: “python3 /home/homeassistant/.homeassistant/script/script.py”

script.py --> fail

from bs4 import BeautifulSoup
print(‘test’)

I would like to know if there is a right way to import this module. I wouldn’t mind if I cannot use my scraper at all, as long as I understand what is going on. I wish I had enough skills to go deeper in the troubleshooting, I am counting on your help to move on.

Thanks in advance.

1 Like

I have a similar script and I experience the same issue when I use BeautifulSoup.

I get an error saying:

ERROR (Thread-11) [homeassistant.components.sensor.command_line] Command failed: python3 /home/homeassistant/.homeassistant/python_scripts/myscript.py