Grab CCTV RTSP Camera Picture when a State Change in HA

Hi All,

I need quick help. I want to build a smart doorbell system.

On my conventional mechanical switch, I have looped a Sonoff SV which connects to HA via MQTT.

What I want to do is, when the switch state changes from OFF to ON(meaning doorbell is pressed) I want my CCTV RTSP feed (the camera facing the main gate) to be snapped as static JPEG and sent to my phone via Android Notification. The notification part is sorted out using HA Notify custom component and companion app.

I have motion eye and shinobi both setup, so if there is a way to trigger the image capture using api, please advise.

Thank you.

You’d have a state trigger, based on that switch, calling a snapshot action. Something a bit like this:

automation:
  - alias: Something
    trigger:
      - platform: state
        entity_id: switch.whatever
        to: 'on'
    action:
      - service: camera.snapshot
        data_template:
          entity_id: camera.yourcamera
          filename: '/config/cameras/yourcamera_{{ now().strftime("%Y%m%d-%H%M%S") }}.jpg'

I followed exactly and added the camera.snapshot action. But HA is not creating a snapshot of my camera to the /config/cameras folder. In fact no files are created.
I run HA as HASSIO on proxmox as a VM, and my cameras are connected to HA using rtsp and new stream integration. Can this be a permission issue to write to config folder?
I even went to Developer Tools and run camera.snapshot service to test, but no image snapshot is created. Some guide is appreciated. Thank you.

My Code as below:

- id: '1212121212121'
  alias: Gate CCTV
  description: ''
  trigger:
  - entity_id: switch.door_bell
    from: 'off'
    platform: state
    to: 'on'
  condition:
  - condition: template
    value_template: '{{ (as_timestamp(now()) - as_timestamp(state_attr(''automation.gate_cctv'',
      ''last_triggered'') | default(0)) | int > 7)}}'
  action:
  - data:
      data:
        actions:
        - action: open_gate
          title: Open Small Gate
        image: https://abc.com/jpg
      message: Someone is at the door "{{ now().strftime('%-I:%M %p %d/%m') }}"
    service: notify.android
  - data:
      data_template:
        entity_id: camera.main_gate
        filename: /config/doorbell_cctv/main_gate_{{ now().strftime("%Y%m%d-%H%M%S")
          }}.jpg
    service: camera.snapshot

Did you check the log file for errors or warnings, or any messages at all, from that automation?

Thanks for the tip. It was write permission issue. I added “whitelist_external_dirs:” to my config and now it saves the jpeg. The issue now is the jpeg saved is 0kb jpeg file and cannot be opened as its empty file. Appreciate some guide if someone has faced similar issues in past.