Door sensor and MJPEG stream recording

Hi all,

I’m trying to record a rtsp stream from my camera, the idea is to start the recording when the door is opened, and stopped + send to telegram when the door is closed.

I’ve created 3 shell commands:

shell_command:
  clear_recording: rm /config/www/door.mp4
  start_recording: bash /config/www/door.sh
  stop_recording: bash /config/www/stop.sh

door.sh

#!/bin/bash
echo "start recording"
ffmpeg -y -nostdin -i "STREAM_URI" /config/www/door.mp4

stop.sh

#!/bin/bash
killall --user root --ignore-case  --signal INT  ffmpeg

I’ve added both scrips to 2 automations, so when the door is opened, I start the recording, but also when the door is closed, just kill the ffmpeg, wait 2 seconds and then, send the video through telegram

when_door_open:

alias: Sensor de puerta abierto
description: ""
trigger:
  - type: opened
    platform: device
    device_id: device_id
    entity_id: binary_sensor.sensor_puerta
    domain: binary_sensor
    for:
      hours: 0
      minutes: 0
      seconds: 0
condition: []
action:
  - service: shell_command.start_recording
    data: {}
  - service: notify.telegram
    data:
      message: Door is open
mode: single

when_door_close:

alias: Sensor de puerta cerrado
description: ""
trigger:
  - type: not_opened
    platform: device
    device_id: device_id
    entity_id: binary_sensor.sensor_puerta
    domain: binary_sensor
condition: []
action:
  - service: notify.telegram
    data:
      message: Door close
  - service: shell_command.stop_recording
    data: {}
  - delay:
      hours: 0
      minutes: 0
      seconds: 2
      milliseconds: 0
  - service: telegram_bot.send_video
    data:
      url: http://HOME_ASSISTANT_INSTANCE/local/door.mp4
  - delay:
      hours: 0
      minutes: 0
      seconds: 10
      milliseconds: 0
  - service: shell_command.clear_recording
    data: {}
mode: single

Ok, after the explanation. If I execute the bash scripts directy from the terminal, it works like a charm, but if I execute them from Home Assistant automation, it only works the first time I execute the flow, any help??

I see other posts related to that but I didn’t find any solution :frowning:

1 Like

Thanks for your script, it works for me with this little changes:

start.sh

#!/bin/bash

STREAM_URI=rtsp://...
timeout 15m /usr/bin/ffmpeg -y -nostdin -i $STREAM_URI /config/www/door.mp4 > /dev/null 2>&1 &
echo $! > /var/run/ha-ffmpeg.pid

stop.sh

#!/bin/bash

PID=`cat /var/run/ha-ffmpeg.pid`
kill -INT $PID
rm /var/run/ha-ffmpeg.pid

And I added some more delays to Door close automation.

I improved some stuffs.

  • Support multiples recording using same script.
  • Changed FPS size to 10 to reduce video size.
  • Fixed Video to SD Resolution (640x480) is enough to mobile. (reduce video size)
  • Added some encode parameter to allow compatibility with Android/IOS
  • Automatic stop recording using parameter “record_time_second”
  • Save a Backup of video, useful when fail to send video.

recording.sh

#!/bin/bash

recorder_dir="/home/homeassistant/.homeassistant/www/cameras"

if [ $# -eq 0 ]
  then
    echo "No arguments supplied"
	exit 1
fi

if [ $1 == 1 ]; then
rtsp_url="rtsp://user:[email protected]:554/cam/realmonitor?channel=1&subtype=0"
location=front_door
record_time_second=13
fi

if [ $1 == 2 ]; then
rtsp_url="rtsp://user:[email protected]:554/cam/realmonitor?channel=2&subtype=0"
location=garage
record_time_second=13
fi

if [ $1 == 3 ]; then
rtsp_url="rtsp://user:[email protected]:554/cam/realmonitor?channel=3&subtype=0"
location=outside
record_time_second=13
fi

file_name=$recorder_dir/$location.mp4

if [ -f  $file_name ]; then
file_epoch_time=`stat -c "%Y"  $file_name`
bak_file_name=$recorder_dir/${location}_${file_epoch_time}.mp4
mv $file_name $bak_file_name
fi


/usr/bin/ffmpeg -y -nostdin -i  $rtsp_url  -t $record_time_second -vcodec libx264 -profile:v main -level 3.1 -preset medium -crf 23 -x264-params ref=4 -acodec copy -movflags +faststart -filter:v fps=10,"scale=640:480" $file_name > /dev/null 2>&1  

Shell

shell_command:
  start_recording_cam_1: bash /config/www/recording.sh 1

To delete files older than 7 days.
Just add on crontab or any scheduler
find /home/homeassistant/.homeassistant/www/cameras/ -type f -mtime +7 -name '*.mp4' -execdir rm -- '{}' \;