Can't run python script from shell_comand with parameter

Hi there!

I am afraid this might be just a simple quotation error, but by now i also think that i’ve tried all possibilities coming to my mind. So i am stuck on this and some advice would be highly appreciated :slight_smile:

What I want to do?

After motion detection triggers i want to run my aftermotion.py script which cleans up my motion directory avi/jpg when someone is home.

aftermotionclean.py

import os
import time
import glob
import sys
import subprocess

state = sys.argv[1]
#state = data.get('fd_	state')
#now = time.time()
path = "/media/motion"
files_jpg = "*.jpg"
files_avi = "*.avi"
lof_avi = glob.glob(files_avi)
lof_jpg = glob.glob(files_jpg)
latest_file_jpg = max(lof_jpg, key=os.path.getctime)
lof_jpg.remove(latest_file_jpg)
#files_to_zip = []
if state == "home":
#	print(state)
for f in lof_avi:
	file = os.path.join(path, f)
	os.unlink(file)
for f in lof_jpg:
	file = os.path.join(path, f)
	os.unlink(file)		
if state == "not_home":
#	print(state)
subprocess.call(['/home/homeassistant/rclone.sh'])

as you can see i am using some imports which are not supported by python_script that’s why i choose shell_command.

automation.yaml:

- alias: Wohnzimmer Motion Reset

hide_entity: true

trigger:

- platform: state

entity_id: binary_sensor.wohnzimmer_motion

to: 'on'

action:

- delay: 00:00:30
- service: mqtt.publish
data:

topic: /wohnzimmer/motion

payload: 'off'

retain: false

- service: shell_command.aftermotionclean

data_template:
variable: "{{ states.group.FamilyDevices.state }}"

config.yaml:

aftermotionclean: '/usr/bin/python3 ~/.homeassistant/python_scripts/aftermotionclean.py "{{ variable }}"'

Error:

Error running command: `/usr/bin/python3 ~/.homeassistant/python_scripts/aftermotionclean.py "{{ variable }}"`, return code: 2

When i change config.yaml and put in the state directly like:

aftermotionclean: '/usr/bin/python3 ~/.homeassistant/python_scripts/aftermotionclean.py home'

The script runs just fine.

According to the docs:

The commands can be dynamic, using templates to insert values for arguments. When using templates, shell_command runs in a more secure environment which doesn’t allow any shell helpers like automatically expanding the home dir ~ or using pipe symbols to run multiple commands.

I think the issue is you’re using the tilda (~) character, which normally expands to the name of the home directory. But as the docs say, this isn’t supported when using a template for the shell command like you’re doing.

Try changing it to:

aftermotionclean: '/usr/bin/python3 /home/homeassistant/.homeassistant/python_scripts/aftermotionclean.py "{{ variable }}"'
1 Like

ok… they must have edited that documentation in the past few hours :wink:
Thank you very much clearing that up for me - and keeping calm answering!
You were totally right ~ is not working with parameters - obviously.
Sorry for beeing blind! :sunglasses:

No problem. Glad you got it working!

Sorry, I looked - that description was added May 4, 2017. :wink: