I am trying to retrieve data from a JSON API. It all works fine on my local machine but I need to import some stuff. The problem is that HA returns an error when I try to import. Is this restricted or maybe working in a diffrent way?
I’m not very experienced with Python but I know some basics.
This is my code:
from urllib.request import Request, urlopen
import json
req = Request("https://api.nanopool.org/v1/eth/reportedhashrate/[key]", headers={'User-Agent': 'Mozilla/5.0'})
data = json.loads(urlopen(req).read().decode());
print(data['data'])
And this is the error HA returns:
2017-07-20 08:47:10 ERROR (Thread-7) [homeassistant.components.python_script.miner_hashrate.py] Error executing script: __import__ not found
Traceback (most recent call last):
File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/components/python_script.py", line 110, in execute
exec(compiled.code, restricted_globals, local)
File "miner_hashrate.py", line 1, in <module>
ImportError: __import__ not found
It is really bad behaviour. It will be really big and bad decision to not support imports =( Many people will not realise their needs without it. Please think again about import support.
Maybe you can make a user setting - use ‘sandboxed environment’ or not?
Yes I’m now all into Appdaemon. It works great and I can improve my python skills at the same time.
I’m also using hassio now so the installation was really easy.
Can you please explain how you did it via Appdaemon. I have a similar script to your python script and get the same error as you do. If you could please detail the steps that you followed to achieve via Appdaemon.
Thank you, after trying to do this “the home assistant way” I ended up simply installing python and recreating my command line sensor again. I honestly do not understand why HA makes it so incredibly difficult to run a simple script and import the values as a sensor.