MotionEye -> HA -> Telegram?

I’m trying to get a snapshot sent from motioneye to HA to telegram. I think HA is getting the image, I’m just not sure how to pass it along to telegram.

Feedback is appreciated!

Motioneye runs:
curl -X POST -F "image=@%f" https://my_ha_address:8123/api/webhook/driveway_motion

My automation is:

- alias: respond on webhook
  trigger:
      platform: webhook
      webhook_id: driveway_motion
  action:
    - service: notify.notify_tzvi
      data_template:
        title: '*Motion Detected!*'
        message: 'Motion has been detected in the driveway {{trigger.data}}'
1 Like

I would like to know the same… Ideally I would like to send a historical snapshot (taken 5 seconds before automation is fired) and send that via Telegram, but learning about the solution of your problem is the first step :grinning:

2 Likes

I am pasting my code below and all the procedures. I hope I wont miss anything. If I do, just tell me what it is, so I will edit my post to keep this clean.

Some notes:

  • Names and aliases and entities and paths etc, have to be changed to match yours. I am just pasting mine…
  • My automations are split in different files, so the indentation is according to that. Dont be confused. All you have to do is to indent accordingly if you dont have your automations split as I do.
  • Deleted usernames or passwords or IPs will be like this: [password]
    You will have to replace the brackets too

Here we go:
First of all, you have to create and whitelist the folder where the snapshots will be created.

configuration.yaml

  whitelist_external_dirs:
    - /home/homeassistant/.homeassistant/www/images

Go to HA and open the side menu. Click on your profile picture and then go to Long-Lived
Access Tokens
and create one. Copy the token and paste it in the step below.

Open motioneye and select your camera. Go to Motion Notifications and choose Run A Command.
paste this:

curl -X POST -k -H “Authorization: Bearer [TOKEN]” -H “Content-Type: application/json” -d ‘{“entity_id”: “[script.stairsmotion]”}’ https://[HomeAssistantIP]:8123/api/services/script/turn_on

Now every time MotionEye will detect motion, it will trigger the script you are about to create:

Script

stairsmotion:
  alias: Stairs Camera Motion Detection ON
  sequence:
  - service: automation.trigger
    entity_id: automation.stairs_camera_snapshot

The above script, triggers an automation. The reason I am triggering the script and not the automation, is because I have more actions in my sequence, but I am only pasting the automation here.
So, the purpose here is to create a script that will do a series of actions and one of them is take a snapshot and also notify you. And now lets create the automation to get the snapshot and the notification:

Snapshot Automation

- alias: 'Stairs Camera snapshot'
  trigger: []
  condition: []
  action:
  - service: camera.snapshot
    data:
      entity_id: camera.stairs
      filename: /home/homeassistant/.homeassistant/www/images/snapshot.jpg
  - condition: state
    entity_id: group.presence_tracker
    state: not_home
  - delay: 00:00:01
  - service: notify.telegram_argy
    data:
      message: 'Motion Detected.'
      data:
        photo:
          file: /home/homeassistant/.homeassistant/www/images/snapshot.jpg
          caption: 'Motion Detected.'

This Automation will create a snapshot and send it to me via Telegram if I am not home.

I know I could just tell you how to send a file using Telegram, but that wouldnt satisfy my need for sharing my method of using MotionEye, HA and Telegram :smiley:

5 Likes

Worked a lot this weekend to improve the functionality of my cameras within HA.

Now I have them integrated with the mjpeg,command_line and mqtt platforms.
I have set up 3 curl commands for each camera to turn on/off the motion detection with the command lines.
Also show them on the HA dash with just my IP and port number for each camera instead of using the embedded url from motioneye. Much simpler.

BUT the camera ID’s on motioneye do not coincide with the command line ID’s!!
e.g. this is not the ID show in motion eye! Took me a while to figure out why is wasn’t working. Process of elimination for each camera.

     switches:
        motion_detection_bedroom:
            command_on: 'curl -k "http://192.168.1.43:7999/1/detection/start" >/dev/null'
            command_off: 'curl -k "http://192.168.1.43:7999/1/detection/pause" >/dev/null'
            command_state: 'curl -s "http://192.168.1.43:7999/1/detection/status" | grep -q ACTIVE'

So I have motion detection off within motioneye and seems to be working by using these commands. Add a toggle/switch to HA and you have much better control.

Also I have used these entities in nodered - state change will send me a telegram now.

I had so many false alarms from the cameras builtin motion detection. The motioneye detection is much more usable.