Announcement

Collapse
No announcement yet.

How-to: UniFi video - trigger switch if motion

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

    How-to: UniFi video - trigger switch if motion

    Note: I have no idea how to integrate feed from the cameras, but made virtual devices that are turned on whenever a camera detects motion.
    This in turn can for example be used for light-control "turn on light in livingroom when motion detected in stairs" as a control when you're on vacation for example.

    Click image for larger version  Name:	motiondetection.jpg Views:	1 Size:	70.8 KB ID:	1277063

    To do this, you need to install swatch and cURL on the NVR, monitoring the motion-log and triggers a cURL whenever detected. sending JSON to homeseer.

    First off (after installing curl and swatch / swatchdog): create a service /etc/init.d/swatchd that you can run at boot; containing the following code:
    Code:
    start()
    {
            /usr/bin/swatchdog [COLOR=#FF0000]--config-file=/etc/swatch/motion.conf[/COLOR] --tail-file=/var/log/unifi-video/motion.log --pid-file=/var/run/swatch/swatch-motion.pid --daemon > /dev/null >&1
    }
    stop()
    {
    [COLOR=#FF00FF]PID[/COLOR]=`cat /var/run/swatch/swatch-motion.pid`
            kill [COLOR=#FF00FF]$PID[/COLOR]
    }
    case [COLOR=#FF00FF]$1[/COLOR] in
    
    start)
            start
            exit 0 ;;
    
    stop)
            stop
            exit 0 ;;
    
    restart)
            stop
            start
            exit 0 ;;
    
    *)
            echo "Usage: [COLOR=#FF00FF]$0[/COLOR] { start | stop | restart }"
            exit 1 ;;
    esac
    then create a configfile in /etc/swatch that checks the log file for names/aliases of the cameras, then executes a command called "motion" if triggered

    example of /etc/swatch/motion.conf:
    Code:
    [COLOR=#339966]#Note: [/COLOR][COLOR=#FF0000]nameofCameraHere[/COLOR][COLOR=#339966] must match your camera alias in unifi NVR![/COLOR]
    perlcode my [COLOR=#FF00FF]$timer [/COLOR]= '[COLOR=#FF0000]240[COLOR=#000000]';
    
    wa[/COLOR][/COLOR]tchfor /[COLOR=#FF0000]nameofCameraHere[/COLOR]/
        exec [COLOR=#FF0000]/etc/swatch/motion <cameraname>[/COLOR]
        threshold type=limit, seconds=[COLOR=#FF00FF]$timer[/COLOR]
    
    watchfor /[COLOR=#FF0000]Cinema[/COLOR]/
    exec /etc/swatch/[COLOR=#FF0000]motion cinema[/COLOR]
     threshold type=limit, seconds=[COLOR=#FF00FF]$timer[/COLOR]
    
    watchfor /[COLOR=#FF0000]Tesla[/COLOR]/
    exec /etc/swatch/[COLOR=#FF0000]motion garage[/COLOR]
     threshold type=limit, seconds=[COLOR=#FF00FF]$timer[/COLOR]
    
    watchfor /[COLOR=#FF0000]Twizy[/COLOR]/
    exec /etc/swatch/[COLOR=#FF0000]motion garage[/COLOR]
     threshold type=limit, seconds=[COLOR=#FF00FF]$timer[/COLOR]
    
    watchfor /[COLOR=#FF0000]Driveway[/COLOR]/
    exec /etc/swatch/[COLOR=#FF0000]motion driveway[/COLOR]
     threshold type=limit, seconds=[COLOR=#FF00FF]$timer[/COLOR]
    
    watchfor /[COLOR=#FF0000]Garden[/COLOR]/
    exec /etc/swatch/[COLOR=#FF0000]motion garden[/COLOR]
     threshold type=limit, seconds=[COLOR=#FF00FF]$timer[/COLOR]
    
    watchfor /[COLOR=#FF0000]Stairs[/COLOR]/
    exec /etc/swatch/[COLOR=#FF0000]motion stairs[/COLOR]
     threshold type=limit, seconds=[COLOR=#FF00FF]$timer[/COLOR]
    [COLOR=#339966]#...etc etc..[/COLOR]
    finally make the excecutable /etc/swatch/motion that sends JSON commands to homeseer based on above triggers.
    (remember to "chmod +x /etc/swatch/motion" to make it excecutable)
    Code:
    [COLOR=#339966]#!/bin/bash[/COLOR]
    [COLOR=#339966]#useage: /etc/swatch/motion <cameraname>[/COLOR]
    [COLOR=#339966]#ref=[/COLOR][COLOR=#FF0000]xxx[/COLOR][COLOR=#339966] replace with the ID of your virtual trigger in homeseer[/COLOR]
    
    case "[COLOR=#FF00FF]$1[/COLOR]" in
       "[COLOR=#FF0000]cinema[/COLOR]")
    [COLOR=#FF00FF]ref[/COLOR]="[COLOR=#ff0000]111[/COLOR]"
           ;;
       "[COLOR=#FF0000]garden[/COLOR]")
    [COLOR=#FF00FF]ref[/COLOR]="[COLOR=#ff0000]222[/COLOR]"
           ;;
       "[COLOR=#FF0000]driveway[/COLOR]")
    [COLOR=#FF00FF]ref[/COLOR]="[COLOR=#FF0000]333[/COLOR]"
           ;;
       "[COLOR=#FF0000]stairs[/COLOR]")
    [COLOR=#FF00FF]ref[/COLOR]="[COLOR=#FF0000]444[/COLOR]"
           ;;
       "[COLOR=#FF0000]garage[/COLOR]")
    [COLOR=#FF00FF]ref[/COLOR]="[COLOR=#FF0000]555[/COLOR]"
           ;;
    esac
    
    curl "http://[COLOR=#FF0000]YourHomeseerIPAddress[/COLOR]/JSON?request=controldevicebyvalue&ref=[COLOR=#FF00FF]$ref[/COLOR]&value=100"
    In homeseer, this sets the virutal device matching your referenceID to value 100 (ON), whenever a motion is detected.

    I finally have an event that states for example: "if virtual device "motion_in_garden" changes an becomes ON, wait 1 minute then turn it off"


    Personally I combine this with pushover, so whenever there is motion in the house when I'm gone, I get a push alert on my phone, allerting me "Motion in livingroom". then I can simply use the UniFi app to check the camera recording (and see that it's the just darn cat again :-)

    extra: How to track phones in a building with UniFi Access Points
    (linking as it is UniFi related, and contains same type of approach)
    Last edited by RazAquato; January 18, 2019, 07:16 AM. Reason: changed heading

    #2
    This is cool, thank you for posting. I have the CloudKey2 with Protect and have been thinking about a way to send motion to Homeseer. I finally figured out how to get snapshots from the Unifi cameras to Homeseer through Protect and it work well with Homeseer Mobile as well. Thank you for posting!

    Comment


      #3
      Originally posted by btinindy View Post
      This is cool, thank you for posting. I have the CloudKey2 with Protect and have been thinking about a way to send motion to Homeseer. I finally figured out how to get snapshots from the Unifi cameras to Homeseer through Protect and it work well with Homeseer Mobile as well. Thank you for posting!
      Wondering how you are getting snaps and if it's better than the script I'm using that is calling ffmpeg to create them.
      HS3 SEL running Pro Edition 3.0.0.531 on Ubuntu 18.04, mono 5.20, 656 devices, 209 events.
      Plug-Ins: Chromecast, Device History, EasyTrigger, Ecobee, JowiHue, LutronCaseta, MeiUnifi, PHLocation2, Pushover 3P, SDJ-Health, Sonos, WeatherFlow, weatherXML
      Scripts: SparkMan's Lock Event, 5 of Jon00 scripts.

      Comment


        #4
        Thank you. I previously tested UniFi in conjunction with Blue Iris so it could be integrated with Homeseer.

        Comment


          #5
          Originally posted by drparker151 View Post

          Wondering how you are getting snaps and if it's better than the script I'm using that is calling ffmpeg to create them.
          Not sure it would be any better, but here is what I did to get it working:

          I use the Camera tab in the Homeseer setup page.

          I enter http://cameraUN:ProtectDevicePasswor...raIP/snap.jpeg

          The camera user name is still default for me, ubnt.

          The Protect Device Password can be found by launching protect, enter in settings under the general tab. You can edit and see the device password that Protect generates there.

          So far it works well and comes through to Homeseer Mobile.

          Comment


            #6
            Originally posted by btinindy View Post

            Not sure it would be any better, but here is what I did to get it working:

            I use the Camera tab in the Homeseer setup page.

            I enter http://cameraUN:ProtectDevicePasswor...raIP/snap.jpeg

            The camera user name is still default for me, ubnt.

            The Protect Device Password can be found by launching protect, enter in settings under the general tab. You can edit and see the device password that Protect generates there.

            So far it works well and comes through to Homeseer Mobile.
            thanks, I'll give it a try.
            HS3 SEL running Pro Edition 3.0.0.531 on Ubuntu 18.04, mono 5.20, 656 devices, 209 events.
            Plug-Ins: Chromecast, Device History, EasyTrigger, Ecobee, JowiHue, LutronCaseta, MeiUnifi, PHLocation2, Pushover 3P, SDJ-Health, Sonos, WeatherFlow, weatherXML
            Scripts: SparkMan's Lock Event, 5 of Jon00 scripts.

            Comment


              #7
              Originally posted by btinindy View Post

              Not sure it would be any better, but here is what I did to get it working:

              I use the Camera tab in the Homeseer setup page.

              I enter http://cameraUN:ProtectDevicePasswor...raIP/snap.jpeg

              The camera user name is still default for me, ubnt.

              The Protect Device Password can be found by launching protect, enter in settings under the general tab. You can edit and see the device password that Protect generates there.

              So far it works well and comes through to Homeseer Mobile.
              Thanks for this. I found at first that it pulled a red X. when I tried directly in a browser I got a 401 error.

              I had to login directly to the camera and turn on enable anonymous snapshot. I logged in to the camera's IP address using user=ubnt pass=<DevicePassword>. I picked skip on the first page displayed. I put a checkmark in "enable anonymous snapshot" and clicked save.

              Then the url syntax you posted started working.
              HS3 SEL running Pro Edition 3.0.0.531 on Ubuntu 18.04, mono 5.20, 656 devices, 209 events.
              Plug-Ins: Chromecast, Device History, EasyTrigger, Ecobee, JowiHue, LutronCaseta, MeiUnifi, PHLocation2, Pushover 3P, SDJ-Health, Sonos, WeatherFlow, weatherXML
              Scripts: SparkMan's Lock Event, 5 of Jon00 scripts.

              Comment


                #8
                Haha, yeah I forgot to mention that part! Glad it is working. I was worried I would not be able to use the unifi cameras that I have without running them standalone.

                Comment


                  #9
                  Can you post the code line and how to install the swatch, curl and how to create the service, configfile and executable. I know how to log to SSH But lost with the rest.

                  Comment

                  Working...
                  X