I have a python script that prints a list containing 2 values [ x, y ] (x being kwh and y being $, which the script pulls from my electricity provider.
I would like to have HA execute this script every month and get those x and y output values into entities so I can put them on a card, graph them, etc.
I got the hello_world python sample from here to work - https://www.home-assistant.io/components/python_script/ to at least execute and show me the result in the system log.
Any advise on where to go next after I add my .py script in /config/python_scripts ? I do need to perform a pip install of a module first.
thanks
petro
(Petro)
August 21, 2019, 7:22pm
2
It’s a stripped environment. For starters, just place the file in and see if it runs from a service call without producing errors. If you import anything, it probably won’t work.
log file entry after I run the “python_script.xcel” service.
2019-08-21 14:50:20 DEBUG (MainThread) [homeassistant.components.websocket_api.http.connection.1869534224] Received {'type': 'call_service', 'domain': 'python_script', 'service': 'xcel', 'service_data': {}, 'id': 21}
2019-08-21 14:50:20 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event call_service[L]: domain=python_script, service=xcel, service_data=>
2019-08-21 14:50:20 WARNING (SyncWorker_17) [homeassistant.components.python_script] Warning loading script xcel.py: Line None: Prints, but never reads 'printed' variable.
2019-08-21 14:50:20 INFO (SyncWorker_17) [homeassistant.components.python_script] Executing xcel.py: {}
2019-08-21 14:50:20 ERROR (SyncWorker_17) [homeassistant.components.python_script.xcel.py] Error executing script: __import__ not found
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/components/python_script/__init__.py", line 182, in execute
exec(compiled.code, restricted_globals, local)
File "xcel.py", line 2, in <module>
ImportError: __import__ not found
2019-08-21 14:50:20 DEBUG (MainThread) [homeassistant.components.websocket_api.http.connection.1869534224] Sending {'id': 21, 'type': 'result', 'success': True, 'result': None}
petro
(Petro)
August 21, 2019, 9:27pm
4
Yep, that means you can’t use the script. You’re trying to import something that ins’t allowed in the environment. So your options are to:
Come up with a different solution. Without using imports.
Move to appdeamon for this automation.
Take your current script and turn it into a custom component.