Problem triggering pythons script via shell command

I can’t get it working to trigger a custom python script with an command line parameter via a shell_command. I’m always getting errors like this, but I can’t find any help what it means:

2020-03-04 17:49:47 ERROR (MainThread) [homeassistant.components.shell_command] Error running command: `/bin/bash -c 'python3 /home/pi/home-assistant/python_scripts/script.py 1'`, return code: 2
NoneType: None

I’ve tried it on several different ways (I think it’s a problem about how to target the python binary) but neither of these are working:

my_script: '/usr/local/lib/python3.7 /home/pi/home-assistant/python_scripts/my_script.py 1'
my_script: '/usr/local/bin/python3 /home/pi/home-assistant/python_scripts/my_script.py 1'
my_script: '/usr/bin/python3 /home/pi/home-assistant/python_scripts/my_script.py 1'
my_script: '/usr/local/bin/python /home/pi/home-assistant/python_scripts/my_script.py 1'
my_script: '/usr/local/bin/python /home/pi/home-assistant/python_scripts/my_script.py 10'
my_script: /bin/bash -c 'python3 /home/pi/home-assistant/python_scripts/my_script.py 1'
my_script: /bin/bash -c '/usr/local/lib/python3.7 /home/pi/home-assistant/python_scripts/my_script.py 2'
my_script: /bin/bash -c '/usr/local/bin/python3 /home/pi/home-assistant/python_scripts/my_script.py 3'
my_script: /bin/bash -c '/usr/bin/python3 /home/pi/home-assistant/python_scripts/my_script.py 4'
my_script: /bin/bash -c '/usr/local/bin/python /home/pi/home-assistant/python_scripts/my_script.py 5'
my_script: /bin/bash -c '/usr/local/bin/python /home/pi/home-assistant/python_scripts/my_script.py 6'

I’m using the home assistant docker container. When I try to execute the script from within the container it is working properly. For example like this:

pi@raspberrypi:~ $ docker exec -it hass /bin/bash
bash-5.0# /usr/local/bin/python python_scripts/my_script.py 9

All the topics I found here mentioned that the problem was to better target the python cli but I guess I’ve tried everythin I could… :smiley:

Any ideas on what I’m doing wrong?

If you are using docker, your home-assistant folder is in /config, so you should use

/config/python_scripts/my_script.py

as the path to the script file. The cli inside the docker container cannot see outside of the container, it only sees the /config folder which is mapped to the /home/home-assistant folder on your host when you start the container.

1 Like

Also, python should already be defined in the environment path of the container so using:

'python /config/python_scripts/my_srcipt.py 1'

as the command should be enough, but the full path to the python interpreter inside the container is /usr/local/bin/python, so this should also work:

'/usr/local/bin/python /config/python_scripts/my_srcipt.py 1'

Thanks! This works!
I don’t know why I did not think about this…

So the calling is working, but the passing of a parameter of the event is not:

my_command: '/usr/local/bin/python /config/python_scripts/my_script.py {{myparam}}'

  - service: shell_command.my_command
    data_template:
      myparam: '{{ trigger.event.data["args"] }}'

I receive the data from a telegram command and it is working if I e.g. just send it back via telegram, so the data is contained in the variable, but somehow it cant be passed to the script. The log just says:

2020-03-05 19:21:05 ERROR (MainThread) [homeassistant.components.shell_command] Error running command: `/usr/local/bin/python /config/python_scripts/my_script.py {{myparam}}`, return code: 1
NoneType: None

What is suppposed to trigger the command, and where does the payload (myparam) come from?

It’s coming from a telegram command. This is the complete automation:

- id: telegram-to-python
  alias: My Automation
  trigger:
    platform: event
    event_type: telegram_command
    event_data:
      command: /mycommand
  action:
  - service: shell_command.my_command
    data_template:
      myparam: '{{ trigger.event.data["args"] }}'
#  - service: shell_command.my_command
#    data:
#      myparam: '3,9'
  - service: notify.telegram_me
    data_template:
      message: 'Test {{ trigger.event.data["args"] }}'

If I send e.g. “/mycommand 2” via telegram, I get back “Test 2”, so the variable is working fine. If I use the out commented shell script call, the 3,9 is also sent properly to the script. But the usage with “data_template” is not working.

Try enclosing the payload in quotes to see if that’s the problem.

my_command: '/usr/local/bin/python /config/python_scripts/my_script.py "{{myparam }}" '

Nope, unfortunately it’s the same problem when I try this. :frowning:

Are you sure that the data in trigger.event.data["args"] is plaintext or is it in any other format, i.e. JSON etc?

Ah you’re right! It was an array! So using

'{{ trigger.event.data["args"][0] }}'

fixed it! Since I received the proper value in the “debugging” message I didn’t look into it further. I once even tried casting it to a string but that didn’t help.
But great that it works now, thanks. :slight_smile:

1 Like