Hikvision NVR: Send telegram video on line crossing

For anyone wishing to send a snapshot or a short video to telegram after detecting line crossing (motion) event with a Hikvision NVR, here is how you can do it:

folder_watcher:
  - folder: /tmp
    patterns:
      - '*.mp4'
      
input_boolean:
  outdoor_motion_notify_photo:
    name: Notify on outdoor motion (photo)
    icon: mdi:run-fast

  outdoor_motion_notify_video:
    name: Notify on outdoor motion
    icon: mdi:run-fast

binary_sensor:
  - platform: hikvision
    host: 192.168.1.200
    port: 80
    username: !secret hikvision_username
    password: !secret hikvision_password
    customize:
      line_crossing_XXX:
        delay: 15
      line_crossing_YYY:
        delay: 15
      line_crossing_ZZZ:
        delay: 15

automation:
  - alias: Capture outdoor movement photo
    initial_state: true
    mode: parallel
    trigger:
      - platform: state
        entity_id:
          - binary_sensor.line_crossing_XXX
          - binary_sensor.line_crossing_YYY
          - binary_sensor.line_crossing_ZZZ
        from: 'off'
        to: 'on'
    condition:
    - condition: state
      entity_id: input_boolean.outdoor_motion_notify_photo
      state: 'on'
    action:
      - service: telegram_bot.send_photo
        data_template:
          target:
            - !secret telegram_chat_id_XXX
          url: "http://localhost:8123{{ state_attr('camera.' + trigger.entity_id[28:], 'entity_picture') }}"
          caption: >-
            {{"\U0001F3C3"}} Movement in {{ trigger.to_state.attributes.friendly_name }}
          inline_keyboard:
            - "\U0001F4F9 New photo:/snapshot {{ trigger.entity_id[28:] }}"

  - alias: Capture outdoor movement video
    initial_state: true
    mode: parallel
    trigger:
      - platform: state
        entity_id:
          - binary_sensor.line_crossing_XXX
          - binary_sensor.line_crossing_YYY
          - binary_sensor.line_crossing_ZZZ
        from: 'off'
        to: 'on'
    condition:
    - condition: state
      entity_id: input_boolean.outdoor_motion_notify_video
      state: 'on'
    action:
      - service: camera.record
        data:
          entity_id: "camera.{{ trigger.entity_id[28:] }}"
          filename: "/tmp/capture_saturn_{{ as_timestamp(now()) | int }}_{{ trigger.entity_id[28:] }}.mp4"
          lookback: 10
          duration: 15

  - alias: Send outdoor movement video
    initial_state: true
    mode: parallel
    trigger:
      platform: event
      event_type: folder_watcher
      event_data:
        event_type: created
    condition:
      - condition: template
        value_template: "{{ trigger.event.data.file.startswith('capture_saturn_') }}"
    action:
      - delay:
          seconds: 25
      - service: telegram_bot.send_video
        data_template:
          target:
            - !secret telegram_chat_id_XXX
          file: "{{ trigger.event.data.path }}"
          caption: >-
            {{"\U0001F3C3"}} Movement in {{ state_attr('camera.' + trigger.event.data.file[26:-4], 'friendly_name') }}

One of the tricks is to name binary sensors for line crossing as binary_sensor.line_crossing_CAMERA_NAME to have proper captions.

3 Likes

Thanks for the awesome tip @resoai . I have been using your automation for quite some time now but since a month or two for some reason it stopped triggering the “Send” automation. Everything appeared to be fine, files were dumped to tmp, however the automation simply didn’t fire. After some troubleshooting it turned out that apparently something changed in the way Homeassistant was producing those files, so now the final even for the .mp4 file is not of type “created” but it started to appear as moved:

event_type: folder_watcher
data:
  event_type: moved
  path: /tmp/capture_saturn_1661704706_xxxx.mp4.tmp
  file: capture_saturn_1661704706_xxxx.mp4.tmp
  folder: /tmp
  dest_path: /tmp/capture_saturn_1661704706_xxxx.mp4
  dest_file: capture_saturn_1661704706_xxxx.mp4
  dest_folder: /tmp
origin: LOCAL
time_fired: "2022-08-28T16:38:44.117145+00:00"
context:
  id: 01GBJN3QMNFMA1WX7ZW4DMQPC2
  parent_id: null
  user_id: null

Looks like the file now gets initially dumped as a *.tmp file and then it gets renamed to the final filename. I swapped the event type to “moved”, “trigger.event.data.file.startswith” with “trigger.event.data.dest_file.startswith” and “trigger.event.data.file” with “trigger.event.data.dest_file”.
And it works fine now. Just as a tip for the other folks who are using this.

1 Like

And suddenly it’s back to the old behavior, so my previous comment is no longer valid. I reverted to the old code, just exchanged the “created” event for “closed” and removed the 25 seconds delay.

Please share the code, how did you add the registrar itself to HA.