ZoneMinder create video after trigger and send to messenger

Hi,

i’ve created the following workflow. When someone is ringing the bell or the front door gets opened or movement gets detected, Home Assistant tells Zoneminder to create a video of that event. After the video is created it will be send via telegram to my smartphone. The shell script needs a little bit of improveness of someone who can script better than me. Here is my home assistant config:

Automation.yaml

- alias: Create Video Frontdoor
  trigger:
    platform: state
    entity_id:
      - binary_sensor.meq1486266_state
      - binary_sensor.motiondetect01_state
    to: 'on'
  action:
    - service: script.create_video

Script.yaml

script:
  create_video:
    alias: 'Script Sequence Creating Video'
    sequence:
      - service: switch.turn_on
        entity_id: switch.zm_trigger_flur
      - delay: 00:00:30
      - service: switch.turn_off
        entity_id: switch.zm_trigger_flur
      - service: shell_command.create_video

switch.yaml

switch:
  platform: telnet
  switches:
    zm_trigger_flur:
      resource: 'localhost'
      port: 6802
      command_on: '4|on|123|textstring1|textstring2'
      command_off: '4|off|123|textstring1|textstring2'

shell_command.yaml

shell_command:
  create_video: 'ssh [email protected] /home/user/home-assistant/shell_scripts/create_video_flur.sh'

create_video_flur.sh

#!/bin/sh

curl -d "username=user&password=password&action=login&view=console" -c /home/user/cookies.txt http://localhost/zm/index.php
zm_eventid=$(curl -s -b cookies.txt -XGET "http://localhost/zm/api/events/index/MonitorId:4.json" | rev | cut -d ',' -f34 | rev | cut -d ':' -f3 | cut -c2- | rev | cut -c2- | rev)
video=$(sudo /usr/bin/zmvideo.pl -e $zm_eventid --format mp4)
video_sound=$(ffmpeg -y -loglevel panic -i $video -f lavfi -i anullsrc -c:v copy -c:a aac -shortest -strict -2 video-sound_flur.mp4)
curl -s _X POST "https://api.telegram.org/bot111111111:AAAAAAAAAAAAAAAAAAAAAAAA/sendVideo" -F chat_id=-11111111 -F video="@video-sound_flur.mp4"

Let me explain create_video_flur.sh:

  • Line 1 (curl -d …): Creating Cookie for ZoneMinder API Login (https://zoneminder.readthedocs.io/en/stable/api.html)
  • Line 2: Uses cookie to read last eventid for monitor id 4 (please, somebody can simplify this cut, rev, cut, … part)
  • Line 3: Line uses event id from zoneminde to create the video (zmvideo.pl is included in zoneminder installation)
  • Line 4: Because Zoneminder doesnt record any audio, this line is for add a silent audio line to the video beacause without that, telegram converts the video to gif
  • Line 5: Send that video file to telegram user

Requirements:

Hope somebody can use it.

7 Likes

just about to implement and test, but can I ask what version of zoneminder you are using?
I’ve just moved to 0.76.2 and seeing a lot of ZM issues, so wondered what version and if you are seeing any API or JSON issues?
thanks for sharing this too :slight_smile:

My zoneminder installation is on 1.30.4 and my home assistant on 0.76.2. I have no issues with zoneminder. Feel free to ask questions.

Now that zoneminder seems to have a propper api, could this be greatly simplified? :wink:

@marcus87 have searched for this very long! Thank you for your tutorial!

Any news on the simplified approach ?