roleoroleo / sonoff-hack

Custom firmware for Sonoff GK-200MP2B camera
GNU General Public License v3.0
200 stars 45 forks source link

pulling the motion_detection_image off MQTT and into a notification (home assistant) #104

Closed keiranharris closed 1 month ago

keiranharris commented 1 year ago

Hi, i appreciate this is not a bug - but ive ran dry with my googling, and figure as you wrote the code there is a fair chance you will know the direction to point me in.

im running your hack and loving it. Today i ventured into MQTT world (first time ive needed it, although im a long term home assistant user). I went here, to get motion detect images from these sonoff cams, sent to my telegram.

My early automation is below - and it works just fine - as in when i walk past the cam, my telegram alerts me, with a simple message:
image

but how do i get the motion_detection_image snapshot (that i can see is being published) off the MQTT message bus, and into my notifiy automation action above? image

thanks in advance! great work BTW! Keiran.

roleoroleo commented 1 year ago

I don't know exactly how to help you. But if you are a python user, you could read the code of the mqtt cam in homeassistant/core Maybe this component uses the same approach.

keiranharris commented 1 year ago

Ok thanks i can try on the weekend. BTW - i have seen other people, on non sonoff cams, solve this by 1/ invoking the home assistant cam snapshot function 2/ writing that image to the local filesystem somewhere 3/ then picking up that latest image off the disk, to embed in a notification message..... but given you ALREADY put an image on the bus, this seems inefficient to me.

Out of curiosity, outside of home assistant (which sounds like you might not use?), do you do this? Use that image you put on the mqtt bus, and do something with it? I guess im looking to confirm that i actually can use that image.

darkxst commented 1 year ago

You should be able to get the image from the MQTT message from the trigger.payload variable in your automation. However telegram can only read images from a file or URL so you probably going to have to save it anyway.

If you are using the MQTT camera integration, then the following will probably work to get the image also: https://www.home-assistant.io/integrations/generic/#sharing-a-camera-feed-from-one-home-assistant-instance-to-another

josecarre commented 1 year ago

@keiranharris here you have my automation just in case it helps you. When motion is detected it send me a telegram msg with the image 😃 :

alias: Send Sonoff camera snapshot to Telegram when motion is detected
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.yi_hack_s_93abe8_motion_detection
    from: "off"
    to: "on"
condition: []
action:
  - service: camera.snapshot
    data:
      filename: /config/www/tmp/{{filename}}.jpg
    target:
      entity_id: camera.yi_hack_s_93abe8_cam
  - service: notify.pixel
    data:
      message: Movimiento detectado
      data:
        message: Movimiento detectado
        photo:
          - file: /config/www/tmp/{{filename}}.jpg
            caption: 🚨 Movimiento detectado en salón {{filename}}
variables:
  filename: snapshot-{{now().strftime("%d%m-%H%M%S")}}
mode: single

It just use motion detection from MQTT, not the image, the image is pulled from the calm with home assistant snapshot service. I could reuse the one coming from MQTT but it must be because of what @darkxst says, I dont remember now because I did this some time ago, but it works :)

github-actions[bot] commented 3 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.