About

OpenHAB2 can be used to automate the surveillance of objectives based on voice triggers. Since the OpenHAB2 homekit binding contains tags for Amazon Alexa, Alexa can be told via voice commands to start or stop monitoring an objective. Furthermore, using the motion Linux package, the surveillance can be performed by detecting movement thereby sparing storage space.

Requirements

The tutorial assumes that the following items are available:

  • Camera (the network based type seem more reliable than web cams via USB)
  • Amazon Alexa or compatible
  • motion software, installable under Debian by issuing aptitude install motion.
  • ffmpeg software, installable under Debian by issuing aptitude install ffmpeg.
  • Mosquitto
  • OpenHab2
    • JavaScript transformation,
    • OpenHAB2 Alexa binding,
    • HomeKit binding

Setting up Motion

motion will be used to record video clips when movement is detected. Installing motion can be performed by issuing the command on Debian:

aptitude install motion

and then configured by editing /etc/motion/motion.conf.

The main configuration changes involve:

  • specifying a camera to record from (not covered),
  • enable the web control
  • enabling and tweaking camera detection,
  • disabling picture capture,
  • enabling and tweaking video recording,
  • tweaking filenames to be chronologically sortable by name

Enabling Web Control

motion can be controlled (set to start detection and recording or to pause detection) by sending HTTP requests to a specify port. Edit /etc/motion/motion.conf and enable web control by specifying a port:

webcontrol_port 8080

Enabling and Tweaking Camera Detection

One important change is to disable the event gap:

event_gap 0

Once movement is detected, motion starts recording and just slows down the framerate once movement slows down. By disabling the event gap motion will just create gapless video clips where each clip is recorded using the full framerate.

motion monitors the amount of pixels changed and once a certain threshold is reached, video recording starts. By default, motion is quite sensitive to what it considers to be movement and the amount of pixels necessary to change for an event to be recorded can be tweaked by modifying the threshold value (by default 1500).

Disabling Picture Capturing

Edit /etc/motion/motion.conf and set output_pictures to off.

Enabling and Tweaking Video Capture

To enable video capture, install ffmpeg and edit /etc/motion/motion.conf to set ffmpeg_output_movies to on.

Using the MP4 video codec can be enabled in order to make the captured video clips more compatible. Changing the video codec can be performed by setting ffmpeg_video_codec to mp4.

Tweaking Filenames to be Chronologically Sortable by Name

One nice trick is to change the filenames created by motion to use a specific naming convention:

YEAR MONTH DAY HOUR MINUTE SECONDS EVENT_NUMBER

without the spaces in between. The effect of naming video clips this way is that when a file manager can be used to sort the clips by name, which will result in the video clips being sorted in chronological order and by event number. The naming convention is superior to using filesystem metadata because the latter may not be compatible across operating systems.

To change the naming convention of files, edit /etc/motion/motion.conf and set:

  • snapshot_filename to %Y%m%d%H%M%S-%v-snapshot,
  • picture_filename to %Y%m%d%H%M%S-%v-%q,
  • movie_filename to %Y%m%d%H%M%S-%v and,
  • timelapse_filename to %Y%m%d-timelapse.

Setting up OpenHAB2 and Alexa

First install OpenHAB2 and browse to the Paper UI in order to enable the:

  • Alexa binding,
  • HomeKit binding,
  • JavaScript transformation

The Alexa binding has to be setup by visiting the OpenHAB2 Cloud and creating an account. Furthermore, Alexa needs to have the OpenHAB2 skill installed - more instructions on how to do that is listed in the official OpenHAB2 Alexa skill documentation.

With the Alexa skill, bindings and transformations installed, create a new file in /etc/openhab2/items named security.items with the following contents:

Group Security (All)

Switch Camera_Monitoring "Monitoring [%s]" (Security) ["Switchable"] {
    http=">[ON:GET:http://localhost:8080/0/detection/start] >[OFF:GET:http://localhost: 8080/0/detection/pause] <[http://localhost:8080/0/detection/status:1000:JS(motion.js)]"
}

where 8080 corresponds to the webcontrol_port configuration parameter configured previously in /etc/motion/motion.conf.

This configuration has the following effects:

  • A new switch is created with the label Camera_Monitoring as part of the Security OpenHAB2 group.
  • The switch uses a homekit tag (Switchable) in order to declare itself controllable by Alexa.
  • The switch will send a request to http://localhost:8080/0/detection/start to start detection via motion and http://localhost: 8080/0/detection/pause to pause detection.
  • In order to obtain the current status of the switch, OpenHAB2 will send a HTTP request to http://localhost:8080/0/detection/status every second (1000) and then map the contents of the documents using a javascript file named motion.js to ON and OFF which OpenHAB2, respectively Alexa can understand.

Next, create a file at /etc/openhab2/transform/motion.js with the following contents:

(function(i){
  if (i.indexOf("ACTIVE") !== -1) 
      return "ON";
  if (i.indexOf("PAUSE") !== -1) 
      return "OFF";
  return "UNDEF";
})(input)

The motion.js script will check the document retrieved from http://localhost:8080/0/detection/status and if the document contains the keyword ACTIVE then OpenHAB2 will consider motion to be in detection mode. Similarly if the document contains PAUSE it will assume that motion is not detecting movement.

The last step is to add the item label to a frame in an OpenHAB2 sitemap. Edit your sitemap and add a frame:

    Frame label="Security" icon="siren" {
        Default item=Camera_Monitoring
    }

Testing with Alexa

First talk to Alexa and say Alexa, detect devices and Alexa should detect one new device.

Then, say Alexa, set monitoring on and Alexa should start motion's movement detection and motion should start recording video clips.

Finally, say Alexa, set monitoring off and Alexa should stop motion's movement detection.

When switching monitoring on or off, a command-line HTTP tool can be used to send a GET request to http://localhost:8080/0/detection/status in order to check whether the status has changed.


iot/openhab2/voice_triggered_surveilance.txt ยท Last modified: 2022/04/19 08:28 by 127.0.0.1

Access website using Tor Access website using i2p Wizardry and Steamworks PGP Key


For the contact, copyright, license, warranty and privacy terms for the usage of this website please see the contact, license, privacy, copyright.