mrlt8 / docker-wyze-bridge

WebRTC/RTSP/RTMP/LL-HLS bridge for Wyze cams in a docker container
GNU Affero General Public License v3.0
2.69k stars 174 forks source link

Camera Motion webhooks with Home Assistant #1044

Open domolys opened 1 year ago

domolys commented 1 year ago

(Using docker-wyze-bridge v2.5.1 with Home Assistant) This issue is concerning the wiki

The webhook automation trigger object in Home Assistant does not contain a dictionary of key value pairs for the headers of the incoming request. (GET/HEAD)

So, actually, it is not possible to use the image url and title that are available in the request header, using MOTION_WEBHOOKS :

X-Title: Motion detected on cam-name
X-Attach: https://...

The only way to use MOTION_WEBHOOKS option with Home Assistant is to create a different Webhook ID trigger for each camera in an automation to know from witch camera the motion is detected.

It's possible to use the MOTION_WEBHOOKS with Home Assistant, but not the way the doc is written for Home Assistant: https://github.com/mrlt8/docker-wyze-bridge/wiki/Camera-Motion#webhooks

Example of an automation triggered by two differents webhook_id:

description: "WYZE-BRIDGE-MOTION_WEBHOOKS"
mode: single
trigger:
  - platform: webhook
    allowed_methods:
      - POST
      - PUT
      - GET
      - HEAD
    local_only: true
    webhook_id: MY-CAM-1-random_id1239743908
  - platform: webhook
    allowed_methods:
      - POST
      - PUT
      - GET
      - HEAD
    local_only: true
    webhook_id: MY-CAM-2-random_id7139244517

Part of config used in Wyze Bridge for triggering the automation above:

CAM_OPTIONS:
  - CAM_NAME: MY-CAM-1
    MOTION_WEBHOOKS: http://home-assistant-host:8123/api/webhook/MY-CAM-1-random_id1239743908
  - CAM_NAME: MY-CAM-2
    MOTION_WEBHOOKS: http://home-assistant-host:8123/api/webhook/MY-CAM-2-random_id7139244517 

[WyzeBridge] [MOTION] Motion detected on my-cam-1 A motion event will trigger the Home Assistant automation above, but the only available trigger data for the user is something like this:

{'platform': 'webhook', 'webhook_id': 'MY-CAM-1-random_id1239743908', 'data': <MultiDictProxy()>, 'query': <MultiDictProxy()>, 'description': 'webhook', 'id': '0', 'idx': '0', 'alias': None}

...otherwise, the addon rocks!

mrlt8 commented 1 year ago

Thanks for the report. Do we need to post data to the endpoint? Would it work with a query parameter? e.g.:

MOTION_WEBHOOKS: http://home-assistant-host:8123/api/webhook/MY-CAM-1-random_id1239743908?some_key=some_value&anotherKey=otherValue
domolys commented 1 year ago

Yes it works, receiving for trigger.query:

<MultiDictProxy('some_key': 'some_value', 'anotherKey': 'otherValue')>

So receiving for: trigger.query.some_key: some_value trigger.query.anotherKey: otherValue

Thanks!

mrlt8 commented 1 year ago

In theory, you should be able to reuse the same web hooks endpoint with multiple cameras by passing the camera name variable {cam_name} as a query parameter:

MOTION_WEBHOOKS: http://0.0.0.0:123/webhooks/endpoint?camera={cam_name}

I've also added {img} variable in the dev branch, so you will also be able to pass the snapshot url with something like:

MOTION_WEBHOOKS: http://0.0.0.0:123/webhooks/endpoint?camera={cam_name}&snapshot={img}
domolys commented 1 year ago

Tested passing variables as query parameter with the same webhook endpoint:

MOTION_API: true
CAM_OPTIONS:
  - CAM_NAME: Cam 1
    MOTION_WEBHOOKS: http://0.0.0.0:8123/api/webhook/random_id?camera={cam_name}&snapshot={img}
  - CAM_NAME: Cam 2
    MOTION_WEBHOOKS: http://0.0.0.0:8123/api/webhook/random_id?camera={cam_name}&snapshot={img}
  - CAM_NAME: Cam 3
    MOTION_WEBHOOKS: http://0.0.0.0:8123/api/webhook/random_id?camera={cam_name}&snapshot={img}

Setup of Home Assistant automation trigger:

platform: webhook
allowed_methods:
  - GET
  - HEAD
local_only: true
webhook_id: random_id

Both triggers trigger.query.camera and trigger.query.snapshot are receiving the variables values

Home Assistant webhook endpoint: http://your-home-assistant:8123/api/webhook/<webhook_id> Home Assistant webhook trigger doc: https://www.home-assistant.io/docs/automation/trigger/#webhook-trigger

Thank you!

mrlt8 commented 1 year ago

Awesome, will merger this into the main branch! I believe you should be able to configure the main MOTION_WEBHOOKS with the variables instead of configuring each one separately.

kakins commented 11 months ago

Question about this. Not sure if I'm doing something wrong. I grab the snapshot variable from the webhook event, along with all of the other header values, and use that to try and construct a request to AWS to get the image. I keep getting 403s.

mrlt8 commented 11 months ago

The AWS urls will expire after some time so you may need to cache them or pull a new snapshot from the bridge.

kakins commented 2 months ago

Has this stopped working for anyone else? It stopped for me sometime in June, I just now have gotten the time to investigate. Did Wyze change something with their API? I've never had CamPlus, but maybe now I have to have it?

It doesn't seem like any motion events are being fired for me at all. I think the logs used to write when motion was detected right? Now I don't see any, just that the motion events are enabled.

I haven't changed anything with my config.

image

kakins commented 2 days ago

Any update on this? It's been a little while now and I still can't get motion webhooks working

ENABLE_AUDIO: true
ON_DEMAND: true
WB_AUTH: false
MOTION_API: true
MQTT: true
CAM_OPTIONS:
  - CAM_NAME: Front Doorbell
    AUDIO: true
    MOTION_WEBHOOKS: ...

This just seems to no longer work. If not, the docs need to be updated.