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.
The tutorial assumes that the following items are available:
motion
software, installable under Debian by issuing aptitude install motion
.ffmpeg
software, installable under Debian by issuing aptitude install ffmpeg
.
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:
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
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
).
Edit /etc/motion/motion.conf
and set output_pictures
to off
.
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
.
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
.First install OpenHAB2 and browse to the Paper UI in order to enable the:
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:
Camera_Monitoring
as part of the Security
OpenHAB2 group.Switchable
) in order to declare itself controllable by Alexa.http://localhost:8080/0/detection/start
to start detection via motion
and http://localhost: 8080/0/detection/pause
to pause detection.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 }
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.