Hikvision Still Image - Telegram

Tags: #<Tag:0x00007f739738b9d8>

I have an automation that would be used to send a still image from my Hikvision camera when motion is detected. Motion is handled by the Hikvision binary sensor.

- id: '1573838226495'
  alias: Line Crossed
  description: ''
  trigger:
  - entity_id: binary_sensor.study_camera_line_crossing
    from: 'off'
    platform: state
    to: 'on'
  condition: []
  action:
  - data:
      caption: Motion Detected
      disable_notification: false
      target: 'xxxxxxxx'
      url: http://admin:[email protected]/ISAPI/Streaming/channels/101/picture
    service: telegram_bot.send_photo

The automation works fantastically when triggered manually, within a few seconds i get the telegram notification with my image.
When the motion alert is triggered under normal situation, i get the below error:

Status code 503 (retry #1) loading http://admin:[email protected]/ISAPI/Streaming/channels/101/picture

I am unsure where to start checking next for a solution.

The way I’ve actually gone is this:

  - service: camera.snapshot
    data:
      entity_id: camera.front
      filename: '/config/www/CCTV_Snapshots/Front.jpg'
  - data:
      caption: Motion Detected
      disable_notification: false
      target: 'xxxxxxxx'
      url: '/config/www/CCTV_Snapshots/Front.jpg'
    service: telegram_bot.send_photo

[disclaimer]
I get an email sent, I do not use telegram, so not checked your code for telegram, only replaced the URL with the one of the snapshot

Thank you for the feedback.
Giving this a shot tonight.

After updating the automation it still appears to be quite unreliable. The notification worked twice out of seven triggers.

I am getting the below error randomly when motion is detected.

Error sending file: Image_process_failed. Args: (XXXXXX, <_io.BufferedReader name='/share/security/studyCam/StudyCam.jpg'>), kwargs: {'caption': 'Motion Detected', 'parse_mode': 'HTML', 'disable_notification': False, 'disable_web_page_preview': None, 'reply_to_message_id': None, 'reply_markup': None, 'timeout': None}

Are you running out of resources? What’s your setup? Do you do any video processing? (Stream, motion detection, tensorflow) out of HA ?
I had issues when doing some of this with cameras setup with the full resolution in HA. Set the cameras to channel 2 (low res) which is much easier on the system. The camera still records at full res

This is another way of doing it. I don’t call the send_photo service…just notify, but attach a photo. It is what I do and I never have issues:

  action:
  - data:
      data:
        photo:
        - caption: Motion In Driveway
          url: http://admin:[email protected]/ISAPI/Streaming/channels/101/picture
      message: Motion in Driveway
    service: notify.telegramgroup

This was cobbled together from other peoples examples, but it works.
HTH

EDIT
My action is the only part of the automation that is different from the OP’s automation (save the sensor name of course) That is why I only posted the part from action down.

1 Like

@lolouk44 - It may be a resource issue, i will assign i bit more to the VM just to be sure.

@danbutter - I will give that a shot now and see how it goes.

I have just noticed that when the sending fails, the saved jpg is only 1kb.
I am able to replicate this same behavior if i move the automation over to NodeRed.

It would seem like the camera.snapshot is the issue here.

Odd. Mine works flawlessly. Have you tried to set the camera with the 2nd channel (low res) to see if the issue is still there? It would not surprise me if the high res was just too much at times of your box is also busy doing something else

I just realized I should have given you more info on my last post.
My action is the only part of the automation that is different from the original automation you posted (save the sensor name of course). That is why I only posted the part from action down.
So there is no camera.snapshot used. It just pulls from the URL and sends with telegram. I never have anything missed so it seems to work pretty well.

@danbutter Yeah, i figured your way would be my next best bet as it bypasses the snapshot service altogether.
I have doubled the memory allocation for my HASS box and switched to your suggestion for the notifications. I will report back with the result. :slight_smile:

I have changed the automation to the below.

data:
  photo:
    - caption: Motion Detected
      url: 'http://admin:[email protected]/ISAPI/Streaming/channels/101/picture'
message: Motion Detected

However i now get this error on motion

Status code 503 (retry #1) loading http://admin:[email protected]/ISAPI/Streaming/channels/101/picture

Followed by this after 5 attempts

Can't send file with kwargs: {'message': 'Motion Detected', 'caption': 'Motion Detected', 'url': 'http://admin:[email protected]/ISAPI/Streaming/channels/101/picture'}

The odd thing is, it worked fine for three alerts, but not for another 9.

I have narrowed down the issue to the Hikvision Camera not accepting the Username and Password (As visible in the camera logs) when input into an automation. However the automation succeeds 100% of the time if manually triggered. When the automation is triggered by motion the camera rejects the credentials.

It is very weird and easily replicated, I just cannot figure out how to solve it.

Why don’t you set it as a camera in HA then use the snapshot service to capture a picture in your notification?

The camera doesn’t accept the authentication being passed through with the snapshot service.

I am really at a loss for this, currently using a Node Red flow that triggers on the motion state and performs an HTTP request to the camera. But it is still not triggering on its own. The camera is successfully sending all the alerts via email, and it is successfully passing the alerts onto home assistant.

Why do you need to pass the authentication to the snapshot service?
Create a generic IP camera:

- platform: generic
  still_image_url: http://admin:[email protected]/ISAPI/Streaming/channels/101/picture
  name: Driveway
  content_type: image/png

in your automation, something like:

  action:
  - service: camera.snapshot
    data:
      entity_id: camera.driveway
      filename: '/config/www/CCTV_Snapshots/Driveway.jpg'
  - data:
      data:
        photo:
        - caption: Motion In Driveway
          url: /config/www/CCTV_Snapshots/Driveway.jpg
      message: Motion in Driveway
    service: notify.telegramgroup

It is this part that the camera for some reason doesn’t accept anymore.

http://admin:[email protected]/ISAPI/Streaming/channels/101/picture

If i input that with the correct username and password into an incognito browser window it still prompts for authentication.
This did work for around two months back in May. But i cannot for the life of me figure out if it was an HA upgrade or firmware upgrade on the camera that broke it.

If you are trying to use generic ip cam then add the line:
authentication: digest

Shot in the dark, but I had to put my username and password in quotes to make my camera work using ffmpeg. I believe it was special characters that were in my password causing the yaml issues.

Perhaps try:

http://“admin”:“XXXXX”@10.0.0.21/ISAPI/Streaming/channels/101/picture

With both the suggestions above, I am able to still successfully trigger the automation manually, but when it is triggered by motion i get a 1Kb file saved by the snapshot service.