Introduction: Affordable Home Security System

About: I like to play with electronics

Security systems comes in different types.

You can find very expensive systems, that usually are complete and reliable but not really customizable, or you can find cheaper ones, surely less complete and rest assured, less customizable.

Today I’m going to teach you how to build, no skills required, a complete, reliable, cheap, upgradeable and fully customizable security system.

It uses just a Raspberry PI (you can use anyone you already have or buy a new on, model 3 recommended, but you can also use a model 2), a Xiaomi gateway and a bunch of Xiaomi sensors.

Let’s get to it!

Step 1: Gathering the Material

Here’s what you need. Products with “ * “ are optional, depending if you want or not to use their feature

You can click on the links to check the best offers I found for you. The links are temporary until I find better deals.

Raspberry PI You can power it with any Micro USB power adapter 5v 2A

* Case for Raspberry PI

Raspberry PI with case and power adaptor (You don’t have to get this if you get the previous 2 items)

Micro SD 8gb (you can use one you already own)

Xiaomi Gateway

* Doors and Windows sensors

* Motion sensors

* Switch

Step 2: ​Download and Install the OS

To run our system, we’re going to use a software called OpenHAB.

It’s a software running on Raspbian (or can be used with actually any other system like windows or OSX, but in this case we’re going to stick with Raspbian) and it’s used for domotics. I may do another instructable about the other uses of OpenHAB in the future.

To make things easier, we’re going to install “openhabian”, a full featured version of OpenHAB and absolutely hassle-free.

To download it just go to this page and download the latest release.

Once done, you need to put that on the micro SD. Not just by copying the file though.

We can do it with Etcher that comes for both Windows and OSX (linux too) OS.

Installing and flashing is pretty straightforward so I won’t cover that. If you need me to help with that just contact me.

Once done, put the SD into the Raspberry, connect to network with an Ethernet cable, connect power and go for a walk. Installation process may take up to 1 hour.

Step 3: ​Install Xiaomi Gateway and Sensors

While it is installing you can install the Xiaomi Gateway.

First of all download the “MI home” on your smartphone. Once you run it, you have to register (choose “Mainland China” as server) and you can start adding devices. You only need an internet connection when adding new devices. The system can run smoothly even without being online. We start adding the gateway of course, otherwise we won’t be able to add the sensors.

Tap the “ + “ icon in the APP, then choose the gateway from the list. You’ll be asked to push the button on the gateway. It will take seconds and then it will appear in your devices.

Once done so, you can add sensors.

Go to the gateway in the app and tap on “Device”, then “Add child device”. Select the type of device you want to add and follow the instructions. Anyway, I suggest you to add devices one at the time after you added the Gateway in the system in next step so you don’t have to struggle finding out which one is which later.

Step 4: ​Install Bindings and Services and Adding Devices

Now that your OpenHAB is installed and your Raspberry is connected to your local network, access http://openhabianpi:8080 from your browser and select “PaperUI”.

Once it opens, click on “Add-ons” and then click on “BINDINGS”. Scroll down until you see “Xiaomi Mi Home Smart Binding” and click install on the right of it.

Now, in the “ACTIONS” tab, search for “Telegram Actions” and install it as well. We’re going to use Telegram to receive alerts and notifications.

There’s a lot more bindings, mainly for domotic devices, but we won’t cover those and even in the more advanced domotic setting, almost everyone of them is unused, so don’t worry about them.

Now in the Inbox, you should see your gateway. Click on the blue “ticked" circle next to it and go with the flow to add it.

Once done, the system will find the other devices (if you already added them, if not, add them now, one by one).

When you add a new device, a new “Thing” is created.

Go into "Configuration -> Things" to find your new device, click on it, rename it the way you like most (e.g.: Entrance door sensor) and save. This will help you when adding items in the next step.

Step 5: Setting Up Items

Now that we have our devices listed as things, it’s time to create our .items file containing the items that will trigger the alarm and its switch.

Gain access to the config folder on the raspberry. To do so, you can use the smb (Samba) protocol. I’m not going in depth on this, but all you have to do is opening a file explorer instance and insert “ smb://your-raspberry-IP “ if you’re using Windows, or open finder then go -> connect to server and insert your raspberry IP if you’re using OSX.

Once you’re there, open the items folder. You’ll find a file named default.items . That’s the one we’re going to modify.

Delete everything and insert the following lines. As you can see i’ve put XXXXXXXXXXXX in the code. That has to be changed to your own device serial. To find the device serial go to Things in PaperUI and you can find the serial as the last digits in the third row of the item description (as shown in the picture)

// Groups

Group Home "Casa"

Group batterie "Batterie”

Group:Contact:OR(OPEN, CLOSED) porte "Porte aperte [(%d)]" (Home)

Group:Switch:OR(ON, OFF) sensori “Movimenti [(%d)]" (Home)

Switch allarme_status "Status Allarme”

Switch allarme_triggered "Allarme scattato"

// Gateway Switch PrimoGateway_LightSwitch { channel="mihome:gateway:XXXXXXXXXXXX:brightness” }

Dimmer PrimoGateway_Brightness { channel="mihome:gateway:XXXXXXXXXXXX:brightness" }

Color PrimoGateway_Color { channel="mihome:XXXXXXXXXXXX:color" }

Dimmer PrimoGateway_ColorTemperature { channel="mihome:gateway:XXXXXXXXXXXX:colorTemperature" }

Number PrimoGateway_Sound { channel="mihome:gateway:XXXXXXXXXXXX:sound" }

Switch PrimoGateway_SoundSwitch { channel="mihome:gateway:XXXXXXXXXXXX:enableSound" }

Dimmer PrimoGateway_SoundVolume { channel="mihome:gateway:XXXXXXXXXXXX:volume" }

// Doors (repeat for every sensor changing “Portaingresso” with your own sensor name)

Contact Portaingresso_Status (porte) { channel="mihome:sensor_magnet_aq2:XXXXXXXXXXXX:isOpen" }

Switch Portaingresso_Battery (batterie) { channel="mihome:sensor_magnet_aq2:XXXXXXXXXXXX:lowBattery" }

// Motion sensors (repeat for every sensor changing “Motion” with your own sensor name)

Switch Motioningresso { channel="mihome:sensor_motion_aq2:XXXXXXXXXXXX:motion" }

Switch Motioningressobatt { channel="mihome:sensor_motion_aq2:XXXXXXXXXXXX:lowBattery"}

Step 6: Create Rules

Before creating rules, we need to setup telegram notification.

I’m not going to cover it myself, but I’ll give you a link to the official OpenHAB wiki that explains very clearly how to do it, better than i ever could.

Once you’ve done so, remember the items file? Ok, go find the default.rules file in its own folder.

Again, delete it and we start writing. The same about XXXXXXXXXXXX apply here, but it’s only about button(s) to enable/disable the system.

//RULE 1 - alarm triggered by doors/windows sensors

rule "allarme porte"

when Member of porte changed to OPEN then

if (allarme_status.state == ON) {

// repeat for every door/window sensor

if (Portaingresso_Status.state == OPEN) {

sendTelegram("bot1", "ALLARME! Apertura Porta Ingresso") }

// end of repeat

var boolean loop_allarme = true


while (loop_allarme) {

sendCommand(PrimoGateway_Sound, 2)

sendCommand(PrimoGateway_SoundVolume, 100)

if (allarme_triggered.state == OFF) {

loop_allarme = false }




//RULE 2 - alarm triggered by motion sensors

rule "allarme sensori"

when Member of sensori changed to ON then

if (allarme_status.state == ON) {

// repeat for every motion sensor

if (MotionIngresso_Status.state == ON) {

sendTelegram("bot1", "ALLARME! Movimento rilevato Ingresso") }

// end of repeat

var boolean loop_allarme = true


while (loop_allarme) {

sendCommand(PrimoGateway_Sound, 2)

sendCommand(PrimoGateway_SoundVolume, 100)

if (allarme_triggered.state == OFF) {

loop_allarme = false }




//RULE 3 - enable/disable system with button

rule "Xiaomi Switch Ingresso"

when Channel "mihome:sensor_switch_aq2:XXXXXXXX:button" triggered then

var actionName = receivedEvent.getEvent()

switch(actionName) {


if (allarme_status.state == ON) {

allarme_status.sendCommand(OFF) }

else {

allarme_status.sendCommand(ON) }




RULE 4 - Warn if battery low on sensors (and switch) - Repeat for every item

rule "porta1"
when Item Portaingresso_Battery changed to ON then

sendTelegram("bot1", "Batteria Porta Ingresso quasi scarica. Controllare")


Step 7: ​Enjoy Your Secure Home

That’s pretty much it for the security system. But it’s not over. OpenHAB is a VAST system that allows you to do almost everything, very well mantained and with an awesome community.

You can command light (integrates easily with Alexa and Google Home), music, shutters and so on.

If you’d like some other tutorial about OpenHAB, just ask and say what you would like me to cover, and when I’ll have some spare time, I’ll do one.

Thanks for reading so far! As usual if you have any comments, suggestions, questions, if you need help or just want to chat, feel free to contact me.

Safe and Secure Challenge

Participated in the
Safe and Secure Challenge