Thanks for looking into this fanuch, really appreciate your help! Your input has put me on the right track and I’m mostly done with the script - see the outstanding issue below.
To recap, I have a IP camera that akes snapshots and records video clips upon detecting motion and stores them on an FTP server. The default file paths are not so user-friendly and my goal is to improve them before uploading them to Google Drive (see the samples below).
The starting point of my automation is the folder_watcher component with the following config watching for the snapshots/videos to be posted an FTP share by the camera:
folder_watcher:
- folder: /share/ipc/
patterns:
- '*.jpg'
- '*.mp4'
Then it’s getting picked up by the following automation:
- id: '1599477490330'
alias: Outdoor Cam Rec GDrive Upload upon Motion
description: ''
trigger:
- event_type: folder_watcher
platform: event
condition:
- condition: or
conditions:
- condition: template
value_template: '{{ ''moved'' in trigger.event.data.event_type }}'
- condition: template
value_template: '{{ ''created'' in trigger.event.data.event_type }}'
- condition: or
conditions:
- condition: template
value_template: '{{ ''.jpg'' in trigger.event.data.file }}'
- condition: template
value_template: '{{ ''.mp4'' in trigger.event.data.file }}'
action:
# Added a delay in case the issue was related to the file being locked by the FTP service upon storage and attempting to run mv too fast, didn't help
- delay: 00:00:01
# Counting the number of motion detection events in the UI for troubleshooting
- data: {}
entity_id: input_number.entrance_motion_counter
service: input_number.increment
# Running the bash script to improve the naming of files
- data_template:
path: '{{ trigger.event.data.path }}'
service: shell_command.ipc_gdrive_prep
# Logging the path to .jpg/.mp4 reporting by folder_watcher to troubleshoot things
- data_template:
message: '{{ trigger.event.data.path }}'
service: system_log.write
mode: queued
max: 10
shell_command.ipc_gdrive_prep is called the following way:
ipc_gdrive_prep: bash /config/ipc_gdrive_prep.sh {{ path }}
and consists of:
#!/bin/bash
# Expected $1 input in case of .mp4_ (for some reason the camera stores with this file extension first and folder watcher neven catches the moment of changing to the proper file extension, so we'll have to strip the underscore off below):
# /share/ipc/Entrance/2020-09-07/001/dav/17/17.26.58-17.27.42[M][0@0][0].mp4_
# Expected output in case of .mp4_:
# Entrance_2020-09-07_16_11_19.mp4
# Expected $1 input in case of .jpg:
# /share/ipc/Entrance/2020-09-07/001/jpg/16/11/22[M][0@0][0].jpg
# Expected output in case of .jpg:
# Entrance_2020-09-07_16_11_22.jpg
dst="/share/ipc_gdrive_prep"
file_ext="$(echo ${1%%_} | awk -F'.' '{print $NF}')"
camera_name="$(echo ${1%%_} | awk -F'/' '{print $4}')"
date="$(echo ${1%%_} | awk -F'/' '{print $5}' )"
if [ "$file_ext" == "mp4" ]; then
file_name="$(echo ${1%%_} | awk -F'/' '{print $NF}' | tr '.' '_' )"
time="${file_name%-*}"
renamed="$camera_name"'_'"$date"'_'"$time"'.'"$file_ext"
mv -f "${1%%_}" "$dst/$renamed"
#echo "Test instruction: ${1%%_}" "$dst/$renamed"
elif [ "$file_ext" == "jpg" ]; then
time="$(echo ${1%%_} | awk -F'/' '{print $8}' )_$(echo ${1%%_} | awk -F'/' '{print $9}' )_$(echo ${1%%_} | awk -F'/' '{print $10}' | cut -c1-2)"
renamed="$camera_name"'_'"$date"'_'"$time"'.'"$file_ext"
mv -f "${1%%_}" "$dst/$renamed"
#echo "Test instruction: ${1%%_}" "$dst/$renamed"
else
exit 42
fi
The camera always stores the .jpg first and then .mp4. This script works perfectly fine with SSH, but not within the above mentioned HA automation.
The first occurence of the automation (.jpg) works just fine. However, when it runs the second time (.mp4), I get “Error running command: bash /config/ipc_gdrive_prep.sh {{ path }}
, return code: 1” in the logs. Tried editing the “mp4” branch of if statement in above mentioned script to match the “jpg” condition, but that hasn’t solved the issue.
Any thoughts on how to trouleshoot this further?