Announcement

Collapse
No announcement yet.

Capturing Motion detects from Protect

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Capturing Motion detects from Protect

    Are you looking at capturing motion detects from Protects? I was SSHing into my Cloud Key and found the log files that log all motion events. It looks quite easy to capture the motion events although the fastest way would be to load a very small script to do it.When you SSH I used root as the user name and the primary password when you setup the Cloud Key.

    Here is a dump of the log file from a tail -F command. One could just take the verbose data or the detailed data. I think the verbose is much easier as it's realtime where the detailed data is not. I was using the -F flag just incase the file name changes which can happen with log files.

    [CODE]tail -f /srv/unifi-protect/logs/events.cameras.log

    2019-12-02T00:06:52.901Z - verbose: motion.start Living Room [18E829CF3569 @ 10.0.2.156] 1575245206817
    2019-12-02T00:07:12.227Z - verbose: motion.stop Living Room [18E829CF3569 @ 10.0.2.156] 1575245239075
    { clockBestMonotonic: 58506459,
    clockBestWall: 1575245218242,
    clockMonotonic: 58528093,
    clockWall: 1575245232075,
    edgeType: 'stop',
    eventId: 28,
    eventType: 'motion',
    levels: { '1': 50 },
    motionHeatmap: 'heatmap_00000028.png',
    motionSnapshot: 'motionsnap_00000028.jpg' }
    2019-12-02T00:07:12.511Z - info: event saved
    { type: 'motion',
    start: 1575245206817,
    end: 1575245239075,
    score: 50,
    cameraId: '5de37069000a3903e7000405',
    partitionId: null,
    metadata: { objectType: null, objectCoords: null, objectConfidence: null },
    id: '5de4559c03873903e7000851' }
    2019-12-02T00:08:16.569Z - verbose: motion.start Living Room [18E829CF3569 @ 10.0.2.156] 1575245290476
    2019-12-02T00:08:23.749Z - verbose: motion.stop Living Room [18E829CF3569 @ 10.0.2.156] 1575245310566
    { clockBestMonotonic: 58586914,
    clockBestWall: 1575245298696,
    clockMonotonic: 58599583,
    clockWall: 1575245303566,
    edgeType: 'stop',
    eventId: 29,
    eventType: 'motion',
    levels: { '1': 84 },
    motionHeatmap: 'heatmap_00000029.png',
    motionSnapshot: 'motionsnap_00000029.jpg' }
    2019-12-02T00:08:24.018Z - info: event saved
    { type: 'motion',
    start: 1575245290476,
    end: 1575245310566,
    score: 84,
    cameraId: '5de37069000a3903e7000405',
    partitionId: null,
    metadata: { objectType: null, objectCoords: null, objectConfidence: null },
    id: '5de455f0023e3903e7000855' }
    /CODE]
    HomeSeer Version: HS3 Standard Edition 3.0.0.548
    Linux version: Linux auto 4.15.0-72-generic #81-Ubuntu SMP Tue Nov 26 12:20:02 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
    Number of Devices: 484 | Number of Events: 776

    Enabled Plug-Ins: 3.0.0.13: AirplaySpeak | 2.0.61.0: BLBackup
    3.0.0.70: EasyTrigger | 1.3.7006.42100: LiftMaster MyQ
    4.2.3.0: mcsMQTT | 3.0.0.53: PHLocation2 | 0.0.0.47: Pushover 3P
    3.0.0.16: RaspberryIO | 3.0.1.262: Z-Wave

    Z-Net version: 1.0.23 for Inclusion Nodes
    SmartStick+: 6.04 (ZDK 6.81.3) on Server

  • #2
    I haven't had a chance to check yet. It will depend on how the logs are accessed and even then it would depend on polling which won't be super responsive. You could try pushing to one of the syslog plugins to get more real time (not 100% sure the unifi stuff supports syslog, but I would assume they do)
    Thanks,
    Frank

    Comment


    • #3
      Tailing the file as I did above while connected using an SSH had a very quick response. Much quicker than what I get from my Ring doorbell. Then the Ring has to go to the Ring cloud, jump over to Alexa then it notifies HS3 using a routine which triggers a HS3 event.That path takes at least a couple seconds or more.

      So without loading any drivers on the Cloud Key one could monitor using SSH and tail although it would be a bit messy. A simpler way would be a simple script on the CK that just tails then sends it over to the HS3 server. Another way is to send that log file to a syslog server but that may or not be possible.

      Bottom line is it can be done.

      BTW, do you load anything on the CK now or do it another way?
      HomeSeer Version: HS3 Standard Edition 3.0.0.548
      Linux version: Linux auto 4.15.0-72-generic #81-Ubuntu SMP Tue Nov 26 12:20:02 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
      Number of Devices: 484 | Number of Events: 776

      Enabled Plug-Ins: 3.0.0.13: AirplaySpeak | 2.0.61.0: BLBackup
      3.0.0.70: EasyTrigger | 1.3.7006.42100: LiftMaster MyQ
      4.2.3.0: mcsMQTT | 3.0.0.53: PHLocation2 | 0.0.0.47: Pushover 3P
      3.0.0.16: RaspberryIO | 3.0.1.262: Z-Wave

      Z-Net version: 1.0.23 for Inclusion Nodes
      SmartStick+: 6.04 (ZDK 6.81.3) on Server

      Comment


      • #4
        I've been looking for ways to do this and I have found one, although I haven't tested it yet, that will send motion events via MQTT. Nice thing about this is multiple systems can watch for motion events.

        unify-protect-mqtt

        The only downside is that you have to add it onto the CloudKey Plus V2 itself. I don't know what UB's position is on this.

        It's basically parsing the file that I referenced above picking up the start and stop motion events and sending them via MQTT. Everything is done with simple bash shell scripts along with Inotify tools and the mosquitto MQTT client. The main script runs as a service on the CK.

        The mosquitto Server is also required but runs on a different system. Mine runs on my NUC8i3 server used for HS3 and some other processes.
        HomeSeer Version: HS3 Standard Edition 3.0.0.548
        Linux version: Linux auto 4.15.0-72-generic #81-Ubuntu SMP Tue Nov 26 12:20:02 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
        Number of Devices: 484 | Number of Events: 776

        Enabled Plug-Ins: 3.0.0.13: AirplaySpeak | 2.0.61.0: BLBackup
        3.0.0.70: EasyTrigger | 1.3.7006.42100: LiftMaster MyQ
        4.2.3.0: mcsMQTT | 3.0.0.53: PHLocation2 | 0.0.0.47: Pushover 3P
        3.0.0.16: RaspberryIO | 3.0.1.262: Z-Wave

        Z-Net version: 1.0.23 for Inclusion Nodes
        SmartStick+: 6.04 (ZDK 6.81.3) on Server

        Comment


        • #5
          Originally posted by sirmeili View Post
          I haven't had a chance to check yet. It will depend on how the logs are accessed and even then it would depend on polling which won't be super responsive. You could try pushing to one of the syslog plugins to get more real time (not 100% sure the unifi stuff supports syslog, but I would assume they do)
          It seems to support syslog however you need to do all the setup through the console using SSH.

          I checked with UB and they currently don't have the camera event file setup to be able to be sent to an external syslog. In fact currently you have to setup syslog via the command interface.
          Last edited by Timon; December 15th, 2019, 02:58 AM. Reason: More syslog information.
          HomeSeer Version: HS3 Standard Edition 3.0.0.548
          Linux version: Linux auto 4.15.0-72-generic #81-Ubuntu SMP Tue Nov 26 12:20:02 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
          Number of Devices: 484 | Number of Events: 776

          Enabled Plug-Ins: 3.0.0.13: AirplaySpeak | 2.0.61.0: BLBackup
          3.0.0.70: EasyTrigger | 1.3.7006.42100: LiftMaster MyQ
          4.2.3.0: mcsMQTT | 3.0.0.53: PHLocation2 | 0.0.0.47: Pushover 3P
          3.0.0.16: RaspberryIO | 3.0.1.262: Z-Wave

          Z-Net version: 1.0.23 for Inclusion Nodes
          SmartStick+: 6.04 (ZDK 6.81.3) on Server

          Comment


          • #6
            For those that know Bash I'm posting the code used to capture events from a file and post it to MQTT. As it turns out it's actually quite simple to do.

            I've made a few edits to make the code a tad easer to read but other than that it's as you will find it on github. There are a few other things I would change such as the time of the event which I don't think it currently send. I'd also move the initial setup variables out of the actual script and into a config file. The original author used this as a starting base and left it to others to improve on the code.

            My plan is to use this base code for other services to monitor other data and send it to HS via MQTT. An example is picking up temperature and humidity data from remote RPi's.

            Code:
            #!/bin/bash
            
            # Unifi Video Vars
            
            UNIFI_MOTION_LOG=/srv/unifi-protect/logs/events.cameras.log
            
            # MQTT Vars
            MQTT_SERVER="10.0.1.X"
            MQTT_PORT="1883"
            MQTT_TOPIC_BASE="/camera/motion"
            
            MQTT_ON_PAYLOAD="ON"
            MQTT_OFF_PAYLOAD="OFF"
            
            # MQTT User/Pass Vars, only use if needed
            
            #MQTT_USER="YOUR_USERNAME"
            #MQTT_PASS="YOUR_PASSWORD"
            #MQTT_ID="yourid"  ## To make it work with hassio
            
            PREVIOUS_MESSAGE=""
            
            # --------------------------------------------------------------------------------
            
            # Script starts here
            
            # Check if a username/password is defined and if so create the vars to pass to the cli
            
            if [[ -n "$MQTT_USER" && -n "$MQTT_PASS" ]]; then
              MQTT_USER_PASS="-u $MQTT_USER -P $MQTT_PASS"
            else
              MQTT_USER_PASS=""
            fi
            
            # Check if a MQTT_ID has been defined, needed for newer versions of Home Assistant
            
            if [[ -n "$MQTT_ID" ]]; then
              MQTT_ID_OPT="-I $MQTT_ID"
            else
              MQTT_ID_OPT=""
            fi
            
            # --------------------------------------------------------------------------------
            
            # Main loop which processes the incoming stream from the UNIFI_MOTION_LOG
            
            while inotifywait -e modify $UNIFI_MOTION_LOG; do
            
                LAST_MESSAGE=`grep "verbose: motion." $UNIFI_MOTION_LOG | tail -n1 `
            
                if [[ "$PREVIOUS_MESSAGE" == "$LAST_MESSAGE" ]]; then
                    echo " same skipping: $PREVIOUS_MESSAGE"
                else
                    echo "PREVIOUS_MESSAGE: $PREVIOUS_MESSAGE"
                    echo "LAST_MESSAGE: $LAST_MESSAGE"
            
                    PREVIOUS_MESSAGE="$LAST_MESSAGE"
            
                    LAST_CAM=`echo $LAST_MESSAGE | awk -F 'verbose: motion.' '{print $2}' | awk -F '[' '{print $1}' | cut -d ' ' -f2- | xargs | sed s/" "/_/g |  tr '[:upper:]' '[:lower:]' | sed -r 's/[^a-zA-Z0-9\-]+/_/g'`
                    LAST_EVENT=`echo $LAST_MESSAGE | awk -F 'verbose: motion.' '{print $2}' | awk -F ' ' '{print $1}'`
            
                    if [[ $LAST_EVENT == "start" ]]; then
                        echo " * Motion started on $LAST_CAM"
                        mosquitto_pub -h $MQTT_SERVER -p $MQTT_PORT $MQTT_USER_PASS -r $MQTT_ID_OPT -t $MQTT_TOPIC_BASE/$LAST_CAM -m "$MQTT_ON_PAYLOAD" &
                    else
                        echo " * Motion stopped on $LAST_CAM"
                        mosquitto_pub -h $MQTT_SERVER -p $MQTT_PORT $MQTT_USER_PASS -r $MQTT_ID_OPT -t $MQTT_TOPIC_BASE/$LAST_CAM -m "$MQTT_OFF_PAYLOAD" &
                    fi
                fi
            done
            HomeSeer Version: HS3 Standard Edition 3.0.0.548
            Linux version: Linux auto 4.15.0-72-generic #81-Ubuntu SMP Tue Nov 26 12:20:02 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
            Number of Devices: 484 | Number of Events: 776

            Enabled Plug-Ins: 3.0.0.13: AirplaySpeak | 2.0.61.0: BLBackup
            3.0.0.70: EasyTrigger | 1.3.7006.42100: LiftMaster MyQ
            4.2.3.0: mcsMQTT | 3.0.0.53: PHLocation2 | 0.0.0.47: Pushover 3P
            3.0.0.16: RaspberryIO | 3.0.1.262: Z-Wave

            Z-Net version: 1.0.23 for Inclusion Nodes
            SmartStick+: 6.04 (ZDK 6.81.3) on Server

            Comment


            • #7
              Sorry I missed all this. I don't load anythign on the CK. I connect to the same apis the UI uses. This is why the plugin is more of a "polling" plugin than a real time one.
              Thanks,
              Frank

              Comment


              • #8
                I now totally understand.

                It’s my understanding it’s possible to use SSH to access files using the tail command. I’m still trying to figure it out. If I do then one could access the camera event file remotely instead of loading a program on the CK. If I figure it out I’ll post it.
                HomeSeer Version: HS3 Standard Edition 3.0.0.548
                Linux version: Linux auto 4.15.0-72-generic #81-Ubuntu SMP Tue Nov 26 12:20:02 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
                Number of Devices: 484 | Number of Events: 776

                Enabled Plug-Ins: 3.0.0.13: AirplaySpeak | 2.0.61.0: BLBackup
                3.0.0.70: EasyTrigger | 1.3.7006.42100: LiftMaster MyQ
                4.2.3.0: mcsMQTT | 3.0.0.53: PHLocation2 | 0.0.0.47: Pushover 3P
                3.0.0.16: RaspberryIO | 3.0.1.262: Z-Wave

                Z-Net version: 1.0.23 for Inclusion Nodes
                SmartStick+: 6.04 (ZDK 6.81.3) on Server

                Comment

                Working...
                X