Python script problems

Hi,

I am testing adding python scripts functionality to automation and did following as first steps:

  1. Added following to configuration.yaml
python_script:
logger:
  default: info
  1. Created a folder config/python_scripts and created a test.py file
logger.info("***********************************************")
logger.info("Python script triggered from automation")
logger.info("***********************************************")
  1. Created a simple automation using editor which got saved under automations.yaml as
- id: '1609068305805'
  alias: SMS Test
  description: ''
  trigger:
  - platform: time
    at: '12:00:00'
  condition: []
  action:
  - service: python_script.test
    data: {}
  mode: single

Then used the “executed” link in automation for manually triggering the automation. On “Supervisor ==> System” console nothing shows up in logs.

Did I do something wrong in above steps or looking at wrong place?
Thanks.

… what’s in the python script

As per point 2:

2 Likes

I donno how tf i missed that.

@PanMat Make sure you restart after adding a python script. Once the service exists, you can adjust the script without restarting.

1 Like

@petro thanks for the feedback. I did restart the HA, in automation editor drop down menu (under Call Service) there was a new entry “python_script.test” which I selected in step-3.

PS: I did not know that restart was not required once the service is created, learnt something new :grinning:

Then everything should be working and if you have ‘debug’ or ‘info’ set as your log level, you should get those 3 lines in your logs.

1 Like

I am running HA as “unsupervised” on a dedicated Ubuntu 20.10 machine. Perhaps there is a way I can check log files with grep option independently outside HA interface to confirm if these three lines are present in the logs cos I do not see them under HA logs. Do you know which log files I should be checking for this on Ubuntu machine?

Another question, if my python script had imported a new package then normally I would install it using pip before running the script. How does that work in HA?

It will be in your homeassistant.log file. It’s in your config folder.

That does not work. Python script environment is gimped to only allow specific actions. Importing is not an option.

@petro the output below suggests that python script is working

config $ cat home-assitant.log | grep "Python script triggered from automation"
2020-12-27 03:37:43 INFO (SyncWorker_31) [homeassistant.components.python_script.test.py] Python script triggered from automation

But within HA interface the logs do not even show this entry, in the snippet below about 50-second time window seems blanked out.

20-12-27 11:37:11 INFO (MainThread) [supervisor.api.proxy] Home Assistant WebSocket API request initialize
20-12-27 11:37:11 INFO (MainThread) [supervisor.api.proxy] WebSocket access from a0d7b954_vscode
20-12-27 11:37:11 INFO (MainThread) [supervisor.api.proxy] Home Assistant WebSocket API request running
20-12-27 11:37:13 INFO (MainThread) [supervisor.homeassistant.core] Detect a running Home Assistant instance
20-12-27 11:38:09 INFO (MainThread) [supervisor.api.proxy] Home Assistant WebSocket API connection is closed
20-12-27 11:38:09 INFO (MainThread) [supervisor.api.proxy] Home Assistant WebSocket API request initialize
20-12-27 11:38:09 INFO (MainThread) [supervisor.api.proxy] WebSocket access from a0d7b954_vscode

Is this a UI or log related display issue? If so how to rectify it as I use the logs within HA UI as reference for all work.
Thanks for your input, this has been very helpful.

That’s the supervisor logs, you’re looking at the wrong logs.

1 Like

LOL, I promise to slap myself when no one is looking :grinning:

LOL, no need. There’s 80 billion logs in HA, so it’s not surprising when it’s hard to tell which one is for the home assistant container.

@PanMat - If you are looking to do a number of Python scripts, and want to get more advanced (ie: full imports etc), then I would recommend looking at AppDaemon. It was suggested to me when I first started out, and have never looked back. No need to restart etc, and it’s out of the way of the typical HA functions as it runs as it’s own process, with it’s own logs etc (but gets all events from HA, and can call back into HA).

@sillyfrog your timing is impeccable, I just started reading the details at https://appdaemon.readthedocs.io/ when your message arrived. Thanks for confirming as it seems like the right direction :clap:

Have a great holidays and a new year!!

1 Like

There’s also python in hacs

Thanks Stefan. As of now AppDaemon seems to be clicking for me, still have lot more to learn but got my scripts working which has added flexibility to add more functionality.

@ReneTode examples/tutorials posted on Github were spot on and thanks for putting content together. :clap:

2 Likes