IP Camera Snapshot Automation

Nope. I just grab a single image now as Synology NAS is recording all video anyway so building a snapshot database inside Hass not really necessary.

- id: ID8
  alias: 'Pool Gate Open'
  trigger:
      platform: state
      entity_id: binary_sensor.door_window_sensor_158d00019fac86
      from: 'off'
      to: 'on'
  action:
    - service: tts.google_say
      entity_id:
        - media_player.kitchen_home
        - media_player.gaming_room_home
      data_template:
        message: "The pool gate just opened"
    - service: camera.snapshot
      data:
          entity_id: camera.pool_deck
          filename: '/config/www/images/pool_gate.jpg'
    - service: notify.pushbullet
      data:
          data:
            file: '/config/www/images/pool_gate.jpg'
          title: "Pool Gate was opened"
          message: "Image of pool gate attached"
2 Likes

I struggled mightily with this, too. Turns out this is what works for my notify.smtp setup. Your mileage may vary:

- service: notify.gmail
  data:
    message: Gameroom door was opened
    title: We've been attacked
    data:
      html: <h4>Check the gameroom</h4>
      images:
      - /config/www/snapshots/new/gameroom.jpg

indent preformatted text by 4 spaces

I had already made a node-red flow using json that worked. I would never have figured out the yaml with a converter. JSON to YAML converter

I’ve done a script + automation, with ffmpeg, which creates a small video from the CCTV feed when someone goes past the motion detector which is next to the CCTV.

The video is then sent to me via Telegram.

Do you want me to share the script?

2 Likes

Hi Metropolis, please share your script because I’m sure there are more people (like me) who want this too.

Yes please, trying to do this exact thing and having trouble.

under “shell_command”
ipcam2_mp4: bash -x /config/shell_scripts/ipcam2_mp4.sh >> /config/temp/ipcam2.log 2>&1

under “automation”

- alias: IPCam2 Snapshot
  trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_1
    to: 'on'
  action:
  - service: shell_command.ipcam2_mp4
  - service: notify.ha_telegram
    data:
      message: ''
      data:
        video:
          - file: /config/temp/output.mp4

and the script itself : shell_scripts/ipcam2_mp4.sh

#!/bin/bash
date

folder=/config/temp
id=$(date +"%y-%m-%d_%H-%M-%S")
rtsp_url=rtsp://admin:[email protected]:554/12
frames=100
fps=5

mkdir $folder
ffmpeg -y -i $rtsp_url -r $fps -frames:v $frames -vcodec copy $folder/$id.mp4
ls -lh $folder/$id.mp4 $folder/output.mp4
cp $folder/$id.mp4 $folder/output.mp4
ls  -lh $folder/$id.mp4 $folder/output.mp4
find $folder -type f -name '*.mp4' -mtime +30 -exec rm {} \;

Please let me know if something isn’t clear :wink:

4 Likes

i got my automation working using this:

- id: '1532581029802'
  alias: Cam Picture Notification
  trigger:
    - platform: state
      entity_id: input_boolean.doorbell_boolean
      to: 'on'
#    - platform: state
#      entity_id: binary_sensor.front_door_camera_motion
#      to: 'on'
  action:
    - service: camera.snapshot
      data:
        entity_id: camera.front_door
        filename: '/config/www/images/frontdoor.jpg'
    - service: notify.home_assistant
      data:
        data:
          file: '/config/www/images/frontdoor.jpg'
        title: "Motion near the door"
        message: "Motion detected at door {{now().strftime('%H:%M %d-%m-%Y')}}"
5 Likes

Thanks for the example scripts! Do you guys also delete the screenshots after e.g. a week to prevent the sd card gets full? Or do you have another solution for this?

My automation only produces 1 file it keeps rewriting over the same file

@goprojojo see my script above, that’s what is being done, deleting mini-videos older than 30 days

2 Likes

Hi, thank you for the code. It helped me to finally show my FTP based CCTV DVR snapshots on my UI. One question, each camera snapshot is saved with a different first part ie “ch01_XXX.jpg”, “ch02_XXX.jpg” etc. How do I get this automation to use a condition based on the first 4 characters to run? I want to run a different automation based on the file names…

alias: Kguard FTP File Path Update
trigger:

  • event_data:
    event_type: created
    event_type: folder_watcher
    platform: event
    condition: []
    action:
  • data_template:
    file_path: ’ {{ trigger.event.data.path }} ’
    entity_id: camera.kguard_test_active
    service: camera.local_file_update_file_path

Help is much appreciated. Also, something I came across. For the folder_watcher component, using a patterns “ch0*.jpg” does not work, but “ch0.jpg” does work: (with the extra “*”

  • folder: /config/www/kguard_ftp/FTP/Motion
    patterns:
    • ch0.jpg’

Hope that helps someone, took me a while to figure out :slight_smile:

Hi, I’ve read all the topic and I’ve tested some of the suggestion that I’ve read and this automation is that it’s working in my HA:

- id: fotogaraje
  alias: Pta garaje abierta tiempo
  trigger:
  - entity_id: cover.sonoffgarage
    for: 0:05:00
    platform: state
    to: open
  condition: []
  action:
  - data:
      entity_id: camera.garaje
      filename: /config/www/cam_captures/garaje_{{ now ().year }}_{{ now ().month
        }}_{{ now ().day }}_{{ now ().hour }}_{{ now ().minute }}.jpg
    service: camera.snapshot
  - delay: 00:00:05
  - alias: ''
    data:
      message: Puerta garaje abierta mucho tiempo con foto
    service: telegram_bot.send_message
  - data:
      file: /config/www/cam_captures/garaje_{{ now ().year }}_{{ now ().month }}_{{
        now ().day }}_{{ now ().hour }}_{{ now ().minute }}.jpg
    service: telegram_bot.send_photo

Thanks a lot to everybody

1 Like

Hi there,

I’m trying to achieve the same thing, but my snapshot isn’t being sent through.
The snapshot is created fine, and it can be viewed in a browser.

I am testing it with both HTML5 and pushbullet notifications.

also - it works fine if I don’t add the time/date stamp to the filename.

Any help would be greatly appreciated!

Here is my code:

- id: 'cam_pic'
  alias: 'cam picture notification'
  trigger:
    - platform: state
      entity_id: sensor.hall_motion_sensor
      to: 'on'
  action:
    - service: camera.snapshot
      data:
        entity_id: camera.fire_security
        filename: '/config/www/images/hall_{{ now ().hour }}_{{ now ().minute }}.jpg'

#    - service: notify.pushbullet
#      data:
#        title: 'Motion in the hall'
#        message: "Motion detected in the hall {{now().strftime('%H:%M %d-%m-%Y')}}"
#        target: 'device/Google Pixel 2'
#        data:
#          file_url: "https://MYDOMAIN.duckdns.org/local/images/hall_{{ now ().hour }}_{{ now ().minute }}.jpg"
    
    
    - service: notify.hassionotify      
      data:
        title: 'Motion in the hall'
        message: "Motion detected in the hall {{now().strftime('%H:%M %d-%m-%Y')}}"
        data:
          tag: alert
          url: 'https://MYDOMAIN.duckdns.org'
          image: 'https://MYDOMAIN.duckdns.org/local/images/hall_{{ now ().year }}_{{ now ().month
        }}_{{ now ().day }}_{{ now ().hour }}_{{ now ().minute }}.jpg'
          vibrate:
            - 300
            - 100
            - 400

How does it work for you with DuckDNS?
I also use duckdns but the picture only available on my local machine but not at https://MYDOMAIN.duckdns.org/local/images/

Do I need to open ports on my router or something else?
Also when I remove the time/date stamp I still don’t get the image. It can be viewed on the browser though.

Thank you so much for your scripts. It took me a very look time to get these to work, but I’m pleased to say with your samples I have managed to do so. Thank you.

How did you manage to get the screenshot with HTML5? Could you post your working script?

1 Like

Hi @goprojojo

Configuration.yaml

shell_command:
front_camera_shell_script: bash -x /config/shell_scripts/front_camera_shell_script.sh >> /config/shell_scripts/front_camera_shell_script.log 2>&1
garden_camera_shell_script: bash -x /config/shell_scripts/garden_camera_shell_script.sh >> /config/shell_scripts/garden_camera_shell_script.log 2>&1

Automations.yaml

  • id: ‘1543786507745’
    alias: Front_Door_Camera_Shell_Script
    trigger:
    • entity_id: alarm_control_panel.ha_alarm
      from: armed_away
      platform: state
      to: triggered
      condition:
    • condition: state
      entity_id: alarm_control_panel.ha_alarm
      state: triggered
      action:
    • service: shell_command.front_camera_shell_script
  • id: ‘1543799524456’
    alias: Garden_Camera_Shell_Script
    trigger:
    • entity_id: alarm_control_panel.ha_alarm
      from: armed_away
      platform: state
      to: triggered
      condition:
    • condition: state
      entity_id: alarm_control_panel.ha_alarm
      state: triggered
      action:
    • service: shell_command.garden_camera_shell_script

front_door_camera_shell_script.sh

#!/bin/bash
date

folder=/config/temp
id=$(date +“%y-%m-%d_%H-%M-%S”)street_camera
rtsp_url=“rtsp://192.168.0.102/user=_password=*_channel=1_stream=0.sdp”
frames=300
fps=5

mkdir $folder
ffmpeg -rtsp_transport tcp -i $rtsp_url -r $fps -frames:v $frames -vcodec copy $folder/$id.mp4
ls -lh $folder/$id.mp4 $folder/street_camera.mp4
cp $folder/$id.mp4 $folder/street_camera.mp4
ls -lh $folder/$id.mp4 $folder/street_camera.mp4
find $folder -type f -name ‘*.mp4’ -mtime +30 -exec rm {} ;

garden_camera_shell_script.sh

#!/bin/bash
date

folder=/config/temp
id=$(date +“%y-%m-%d_%H-%M-%S”)garden_camera
rtsp_url=“rtsp://192.168.0.104/user=_password=_channel=1_stream=0.sdp”
frames=300
fps=5

mkdir $folder
ffmpeg -rtsp_transport tcp -i $rtsp_url -r $fps -frames:v $frames -vcodec copy $folder/$id.mp4
ls -lh $folder/$id.mp4 $folder/garden_camera.mp4
cp $folder/$id.mp4 $folder/garden_camera.mp4
ls -lh $folder/$id.mp4 $folder/garden_camera.mp4
find $folder -type f -name ‘*.mp4’ -mtime +30 -exec rm {} ;

I should point out, I am new to Hass.io and I relay a LOT on other people’s input. Finally to get this working I had to keep looking for the errors inside the log file, eventually after many hours of Googling and trying to understand what the errors meant I got it to work. It’s not easy. Far from it.

3 Likes

Thanks so much.
This post helped me a lot with a android IP webcam. But I run it directly the longest time - Hoped it the bash file takes care of the timeout wich I had to use first.
maybe with a wait template… but I dont know.

I also had problems with files with files more then 2mb
So i used

-t 4 -b:v 2048k -vcodec libx264 -fs 2048k


Edit:
Said and done - I changed your Script therefore a little more

shell_command:
  ipcam_mp4: bash -x /home/homeassistant/.homeassistant/ffmpeg/ipcam2_mp4.sh >> /home/homeassistant/.homeassistant/ffmpeg/ipcam2.log 2>&1

sensor:
  - platform: mqtt
    name: "readytosend"
    state_topic: ipcam/done
    expire_after: 20
    qos: 0

automation:
 [TRIGGER]
    action:
    - service: shell_command.ipcam_mp4
    - wait_template: "{{ is_state('sensor.readytosend', '1') }}"
      timeout: '00:00:40'
      continue_on_timeout: 'true'
    - service: telegram_bot.send_video
      data:
        file: /home/homeassistant/.homeassistant/ffmpeg/output.mp4
        message: "/vid"

___________________________________________________________________________
pcam2_mp4.sh
___________________________________________________________________________


#!/bin/bash
date

folder=/home/homeassistant/.homeassistant/ffmpeg
id=$(date +"%y-%m-%d_%H-%M-%S")
rtsp_url=http://user:pass@[androidIPwebcam]:8080/video?profile=0
duration=4
bitrate=2048k
codec=libx264
filesize=2048k

mkdir $folder
ffmpeg -y -i $rtsp_url -t $duration -b:v $bitrate -vcodec $codec -fs $filesize $folder/$id.mp4
ls -lh $folder/$id.mp4 $folder/output.mp4
cp $folder/$id.mp4 $folder/output.mp4
mosquitto_pub -u MQTTUSER -P MQTTPASS -t 'ipcam/done' -m "1"
ls  -lh $folder/$id.mp4 $folder/output.mp4
find $folder -type f -name '*.mp4' -mtime +30 -exec rm {} \;
1 Like

@Underknowledge: Do you mind explaining how you set the status of sensor.readytosend ?

Sure
You see the 'mosquitto_pub ’ in the shell script?
This publishes a message and the sensor reacts to it. Therefore the wait template gets satisfied