Sending a picture with Telegram

I’m looking for some help because i’m starting to loose faith that I can resolve it.

It sounds easy. Send a picture with telegram.

The story:
I set up a security cam outside my house. The software i’m running is Agent DVR and it runs on my Windows server 2016. It takes a picture when it sees a person,dog or cat. I want so sent that picture with Home assistant to my phone with Telegram.

The first thing I tried to do is to connect a nfs share to my HA. My thinking was that it would be easyer to have the pictures localy avalible in HA but I can’t get it to work because I keep getting errors. “Error running command: mount -t nfs3 Windowsserver2016:/camerabeelden /media, return code: 255”. Looking in other topics i’m not the only one with this problemen so I gave up.

What i’m thinking of right now is to use the API of Agent DVR to get information into Homeassistant but I have no clue how to start. What I want to do is the following.

  1. Get the picture name :
    http://Windowsserver2016:8090/q.json?cmd=getcameragrabs&oid=1&ot=2
    The result I get:
    {“total”:72,“results”:[{“name”:“2022-01-02_15-33-58_492.jpg”,“time”:“1641145734680.49”,“tags”:“person”},{“name”:“2022-01-02_15-33-55_250.jpg”,“time”:“1641145734649.47”,“tags”:“person”},{“name”:“2022-01-02_15-33-53_713.jpg”,“time”:“1641145734629.48”,“tags”:“person”},{“name”:“2022-01-02_14-48-38_844.jpg”,“time”:“1641145734615.47”,“tags”:“person”},{“name”:“2022-01-02_14-48-36_991.jpg”,“time”:“1641145734601.43”,“tags”:“person”}], “last”:637767461346014346}
  2. Extract the picture name:
    2022-01-02_15-33-58_492.jpg
  3. Use this jpg into the next url:
    http://Windowsserver2016:8090/grabimage.jpg?oid=1&fn=2022-01-02_15-33-58_492.jpg"

This url I can then use in a automation:

alias: '-Test- Telegram - Stuur me een foto wanneer er beweging is.'
description: ''
trigger:
  - platform: mqtt
    topic: Agent/cameras/achtertuin/deepstack
condition: []
action:
  - service: telegram_bot.send_photo
    data:
      url: >-
        http://192.168.2.8:8090/grabimage.jpg?oid=1&fn=2022-01-02_15-33-58_492.jpg
mode: single

Is this posible what I want to do?
Is it posible to use variables in automations?
Is there maby a easyer way to do this?

I made some progress but it’s really janky.

First thing I did is to change the naming convention that Agent DVR uses for the pictures. It now uses a counter.

Second thing is that I uses a sync program that simply copies the pictures taken to the /media map in Home Assistant. This sync action takes about 8 sec.

Third, I learned about helpers so I created a counter. “counter.foto_teller_2” I put this counter on the same number as Agent DVR names his picture. This way they are sort of synced up.

4th This is the automation i’m using.

alias: '-Test- Telegram - Stuur me een bericht wanneer er beweging is. '
description: ''
trigger:
  - platform: mqtt
    topic: Agent/cameras/achtertuin/deepstack
condition: []
action:
  - delay:
      hours: 0
      minutes: 0
      seconds: 15
      milliseconds: 0
  - service: counter.increment
    target:
      entity_id: counter.foto_teller_2
  - service: telegram_bot.send_message
    data:
      message: Beweging in de tuin
  - service: telegram_bot.send_photo
    data:
      file: /media/{{ states('counter.foto_teller_2')}}.jpg
mode: single

It sort of works but it doesn’t eswell. When there is movement Agents takes more then one picture. So it the picture naming gets out of sinc.

There must be a smarter/beter way to do it.