IP Camera Snapshot Automation

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

aha, didnt see that at first. nice, thanks.

I know I must be close, but I can’t seem to get this to work.

here is my script

presence_backyard_notifications_when_detected:
  alias: Occupancy Detection - backyard - Notifications
  sequence:
  - data:
      entity_id: camera.backyard
      filename: /config/www/cam_captures/backyard_{{ now ().hour }}_{{ now ().minute }}.jpg
    service: camera.snapshot
  - delay: 00:00:05
  - service: notify.all_hands
    data:
      message: Motion detected in the backyard at {{ states('sensor.time') }}
      title: Presence Detected at backyard
      data:
        attachment:
          content-type: jpg
          hide-thumbnail: false
          url: 'https://mydomain/local/cam_captures/backyard_{{ now ().hour }}_{{ now ().minute }}.jpg'
        image: 'https://mydomain/local/cam_captures/backyard_{{ now ().hour }}_{{ now ().minute }}.jpg'

It works perfect if I just use a save the snapshot without the variables for date and time (backyard.jpg), but onces I include those, I get no picture attached. I know the URL is good though, just to be sure I added it to the message. When the message sent I could copy the link from the notification and paste it into a browser and see it fine. What am I doing wrong with adding the date and time on it? I have tried single quotes, double quotes, no quotes…

P.S. I have the attachment and the image elements in the data element because I’m sending to two different services in the notify.all_hands btw. That’s why you see that listed twice. again it works if its just “https://mydomain/local/cam_captures/backyard.jpg” and honestly I’d leave it like that except that the snapshot function doesn’t seem to overwrite the file if one exists. This would save me the trouble of cleaning out the folder periodically.

Try like this…

backyard_{{ now().strftime(’%I_%M’) }}.jpg

That’s still a no go. The snapshot is saved, the URL is valid even from outside the house, but the image is not added to the notification. The URL in the message is even clickable and takes you to the image, I don’t get it. Is there a way to just over write the previous file so I don’t need to worry about the time stamp at all?

change “data:” to “data_template:”.

Any time you are using a template you need to use “x_template:” (data_template, service_template, etc). Be informed tho that not everything can be templated so you need to make sure you follow the docs to see which things can be.

2 Likes

thanks that worked

Please help me understand this issue. I used this configuration IP Camera Snapshot Automation as template.

My configuration: front door camera motion detection -> send camera picture via pushbullet

Automation YAML:

- id: send_fd_cam_message
  alias: Send FD Camera Message
  trigger:
    platform: state
    entity_id: binary_sensor.fd_cam_motion
  action:
    service: homeassistant.turn_on
    entity_id: script.send_fd_cam_msg

Script YAML:

  send_fd_cam_msg:
    alias: "Send FD camera jpg message"
    sequence:
      - service: camera.snapshot
        data:
          entity_id: camera.fd_cam
          filename: "/home/homeassistant/.homeassistant/www/cam_images/fdcam_snapshot.jpg"
      - service: notify.pushbullet
        data:
          title: "FD CAM Motion"
          message: "Front Door Camera: Motion Detected"
          data:
            file: "/home/homeassistant/.homeassistant/www/cam_images/fdcam_snapshot.jpg"
          target: "device/GP2"
      - delay:
          seconds: 5

It barely works when this script is invoked through automation. I get following message from Pushbullet:

FD CAM Motion
Front Door Camera:Motion Detected
/home/homeassistant/.homeassistant/www/cam_images/fdcam_snapshot.jpg

No Image

Please note that it ALWAYS works if I invoke that script manually.

Did you solve this ?
I got everything working except that I get a empty (0 Kb) file = no picture

Super cool - but is there not a chance because of the 5 second delay, if the minute ‘wraps’ the automation won’t be able to find the file ?

This is a good point, and the trick is to save the timestamp to an input_text at the start of your automation like this:

service: input_text.set_value
entity_id: input_text.timestamp
data_template:
  value: '{{ now().strftime(''%Y%m%d_%H%M%S'') }}'

and use {{ states("input_text.timestamp") }} in your template.

2 Likes