Python script as command line sensor error

Hello community,

I have created the following script after coming across an earlier posting which grabs weather information.

The script when run from a terminal using python3 executes fine and outputs the result as expected.

#!/usr/bin/python
# -*- coding: utf-8 -*-

import json
from subprocess import PIPE, run
 
def out(command):
    result = run(command, stdout=PIPE, stderr=PIPE, universal_newlines=True, shell=True)
    return result.stdout
 
url = "https://www.accuweather.com/en/au/brisbane/24741/minute-weather-forecast/24741"
my_output = out("wget -q -O- -U 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)' '{}' | awk '/minuteCastForecast = (.*);/' | sed -r 's#.*minuteCastForecast = (.*);#\\1#g'".format(url))
my_json = json.loads(my_output)
print(my_json['summary'])
print(my_json['updated'])

Executing in HA from developer tools gives two errors

“Warning loading script weatheralert.py: Line None: Prints, but never reads ‘printed’ variable.”

Is the issue with the print command? I have another script which presents the same issue.

Thanks.

did you try

#!/usr/bin/env python3

Hi @Jan.Schmidt,

That didn’t seem to make any difference. Get “command failed” in log for the sensor and same "Prints, but never reads ‘printed’ variable.” when calling from HA.

This error also shows when calling from HA

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/python_script/__init__.py", line 194, in execute
    exec(compiled.code, restricted_globals, local)
  File "weatheralert.py", line 4, in <module>
ImportError: __import__ not found

sensor as below.

  - platform: command_line
    name: Weather Alert
    command: 'python3 /usr/share/hassio/homeassistant/python_scripts/weatheralert.py'

Did you ever manage to resolve this? I’m having the same issue Python script works in terminal, but fails in command line sensor