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.