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
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.