guino / Merkury1080P

Merkury1080P (CW017) Rooting and Customization
83 stars 17 forks source link

curl to trip a home assistant webhook #12

Closed williamkennyAK closed 2 years ago

williamkennyAK commented 2 years ago

I'm not sure of the possibility, but is there a busybox binary build for this device (or link to a howto) to run curl so I can trip a home assistant webhook on motion?

guino commented 2 years ago

@williamkennyAK curl -- I haven't seen any busybox with it. The busybox posted for the SD card ( https://github.com/guino/Merkury720/tree/main/mmc ) has wget which should be able to do web requests similar to curl. This link also has relevant Home Assistant information: https://github.com/guino/BazzDoorbell/wiki/How-do-I-integrate-with-Home-Assistant,-HomeBridge,-Domoticz,-etc

williamkennyAK commented 2 years ago

I ended up going a slightly different route. I used the ffmpeg camera integration with the ffmpeg_motion integration to trigger a motion event. https://www.home-assistant.io/integrations/ffmpeg_motion/

williamkennyAK commented 2 years ago

Even better, I've found an alternate BusyBox that includes wc and nc. Using these I've created a script to post to a home assistant webhook. I'll document it a bit better and submit the code here.

williamkennyAK commented 2 years ago

Here is the shell script I'm using with the included instructions you provided in Comment 2:

!#/bin/sh

#DEBUG_FILE=/mnt/mmc01/output.log
DEBUG_FILE=$1
# Define Webhook to trip
WEBHOOK="driveway_motion"
# Build the PATH to POST
POST_PATH="/api/webhook/${WEBHOOK}"
# Hostname or IP of Home Assistant
HOST=192.168.x.x
# Port Number
PORT=8123
# This can be just about anything for our purposes
BODY="Put here HTML body...."
# Get the Body Length for proper POST form data
BODY_LEN=$( echo -n "${BODY}" | /mnt/mmc01/busybox wc -c )

contains() {
    string="$1"
    substring="$2"
    if test "${string#*$substring}" != "$string"
    then
        return 0    # $substring is in $string
    else
        return 1    # $substring is not in $string
    fi
}

# contains(string, substring)
#
# Returns 0 if the specified string contains the specified substring,
# otherwise returns 1.

IFS='$\n'
echo -n "" > $DEBUG_FILE
while true; do
read -r BUF;
if [ $? -ne 0 ]; then
    sleep 1;
    continue
fi
if contains "$BUF" "--motion detection alarm --"; then
    #echo "motion detected"
        echo -ne "POST ${POST_PATH} HTTP/1.0\r\nHost: ${HOST}\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: ${BODY_LEN}\r\n\r\n${BODY}" | \
        /mnt/mmc01/busybox nc -i 3 ${HOST} ${PORT}
fi
echo $BUF >> $DEBUG_FILE
done

I then create an automation in HA that sets an input boolean and resets it to off 30 seconds later. This then triggers my object detection using the DOODS addon and finally sends me a telegram notification.

Turn this cheap camera into a $300 object detection beast!!!

Later this week I will fork and draw up a pull request.

guino commented 2 years ago

@williamkennyAK you should have been able to use the existing busybox (wget) to perform the post, but as long as it works it doesn't hurt to have more options -- can you post a zip or link of the busybox you used (that works with the script) ?

EDIT: Nevermind - the busybox we've been using has nc so this should work as-is.

williamkennyAK commented 2 years ago

@guino unfortunately, the included wget lacks the ability to use --method. Unless I'm unaware of some wget-fu, this was the only way I could accomplish it.

guino commented 2 years ago

@williamkennyAK that makes sense, I did not notice the wget in busybox was not POST capable. It's odd that homeassistant doesn't allow for 'GET' requests like domoticz, homebridge, etc -- in any case I assume you could also have used mqtt with the mosquito_pub tool provided in the link of the 2nd post above.

Like I said, always nice to have more options.

williamkennyAK commented 2 years ago

V2:

This is a work in progress but here we go:

#!/bin/sh
# notify.sh
# kenny@beardedtux.com
# GitHub: williamkennyAK
#
# Parses ppsapp logs for keyword to trigger local motion notifications
# Generates motion alerts from tuya linux based cameras
# ADD LINK TO REPOS...
#
# usage:
#
# /mnt/mmc01/ppsapp | /mnt/mmc01/notify.sh <keyword> <worker> <logfile> <loglevel>
# * denotes required
# * keyword       - keyword to look for in ppsapp logs
# * worker        - script or binary that executes notifications
# * logdir        - /full/path/to/log/dir
# * loglevel      - 0: silent
#                   1: motion detected only
#                   2: motion detected and worker output
#                   3: EVERYTHING ** use this sparingly as it can build up quickly **
#                      It's main use is to figure out the keyword to use
keyword=$1
worker=$2
workerlog="$3/worker.log"
log="$3/notify.log"
loglevel=$4
while true; do
    read -r BUF;
    case "$BUF" in
        *"${keyword}"*)
            case $loglevel in
                "1")
                    echo "############# MOTION DETECTED #############" >> $log
                    $worker
                    ;;
                "2"|"3")
                    echo "############# MOTION DETECTED #############" >> $log
                    echo "############## WORKER LOGGED ##############" >> $log
                    $worker >> $workerlog
                    ;;
            esac
            ;;
    esac
    case "$loglevel" in
        "3")
            echo "${BUF}" >> $log
            ;;
    esac
done

I then modify custom.sh with this line:

  /mnt/mmc01/ppsapp | /mnt/mmc01/notify motion /mnt/mmc01/post.sh /mnt/mmc01/logs 2 &

With this script you could then use the mosquitto_pub method with this script giving you a bit more control.

My ppsapp denoted a motion trigger in the following way:

[13:17:26.813 INFO pps_tuya_media.c:1808]motion detected upload pic
[13:17:32.874 INFO pps_tuya_media.c:1788]motion detected but in shield time
[13:17:43.041 INFO pps_tuya_media.c:1788]motion detected but in shield time
williamkennyAK commented 2 years ago

Submitted pull request with changes made including web interface for configuration.