Creating A Home Security System with Home Automation

One great thing about having a home automation system installed is providing you with the ability to give any of your home devices multiple purposes. With a touch of creativity, thorough planning and a bit of imagination, you can transform any device to the more useful home appliance. An excellent example of this is by making use of lights to simulate occupancy when you’re away from home. You can also use a small speaker or radio to make it appear someone’s at home. All those things are achievable when you have a smart home system installed.

Most of us want to secure our possessions and assets as well as our family. Having a standard alarm is proven to be useful, but it won’t be enough. Sometimes people tend to ignore small alarm noises, and for smarter burglars or trespassers it can’t be a that of a problem with them. Given that fact, a personalised security system may be your best bet. A much better security system that will inform you whatever out of the ordinary happens.

Creating a personalised security system using home automation like Domoticz will allow you to have smart home products that can be armed when you select the “Leaving” feature on. This feature waits for five minutes checking if you come back in, then after another five minutes, it will look for your phone if it is within WiFi range, then disarms the system.

 Requirements to Create the Security System

  • RFXCOM RFXtrx433
  • Raspberry Pi running domoticz
  • Python that runs Raspberry Pi
  • Door sensors, vibration sensors or PIRs (any combination)
  • Network IP camera (for taking photos when a sensor is triggered)

Set Up Duration

 Setting up this security system will take fifteen minutes to several hours, depending on the number of sensors and cameras you want to use. Installing sensors or cameras on all of your doors will take time, so it will depend on how much thorough your security system you want to be.

Step by Step Installation Process

Step #1 – Installing Necessary Devices

 Make sure to cover all the entry points to your home, locate the doors and windows that people can use to gain access to your home. Apparently the main door, back door and other places where someone can gain entry. Those are the crucially vulnerable parts of your home that you need to secure.

On doors, install the sensors towards the top and make sure that you have easy access to the battery compartment. This will allow you to change the batteries without any difficulty. It’s also a good thing to plan how would you like to install the door sensor. It will make it more convenient for you, and it will allow you to have a seamless installation.

For vibration sensors, they are usually installed directly on a door or window using suction cups. For permanent placement, make sure to test a variety of scenarios to achieve the preferred placement that will be comfortable for you and your family.

PIRs are wireless devices that you can install anywhere; you can place them on shelves, wall cavities and even roof spaces. Hallways are ideal for these devices since it is impossible not to cross the sensors when moving from one room to another. Raise the position of PIRs if you have pets to make sure that they are activated only by humans.

Note: It is best to learn and name each sensor in Domoticz, label them by type. It will keep you organised, and it’s a great way to pinpoint what’s the triggered sensor and where it is located.

Step #2 – Creating Dummy Switches

Once the devices are installed, the next step to take is to create dummy switches. Dummy switches determine the function of the sensors; they are the commands that the sensors will carry out once activated.

“Leaving” is one of the dummy switches you can create. With this dummy switch sensors will be ON when you leave home and OFF once you return.

The “Security Alarm” dummy switch is also an essential switch to create. It’s a switch that commands the scripts to send you an alert every time a sensor is triggered. It’s an ideal switch if you are not at home most of the time.

“Waiting for Phones” is another dummy switch that you need to create. It’s a switch that turns the ON the security system while waiting for you to get home.

Then create a dummy switch called “Arm Security”.

After all those switches are created, you have to create a switch for the phones you want to automatically disarm the system with. It’s recommended to create at least two.

Step #3 – Writing the Scripts

 Now is the time to write the scripts, the basic commands for each switch. It sets the condition for each switch, and it determines the behaviour of the devices installed. These scripts you have to create will be considered as the core of the security system you are setting up. In writing these scripts, you can configure the function of the switches you created.

The first code you need to write should be saved as “device_SECURITY_Leave.lua” and this file should be saved in this location, domoticz/scripts/lua/folder. This is a script for the dummy switch you created as “Leaving”.

commandArray = {}

if devicechanged[‘Leaving’] == ‘On’ then
commandArray[‘Environment Automation’] = ‘Off’
commandArray[‘Living Room Camera’] = ‘On’
commandArray[‘Security Alarm’] = ‘Off’
commandArray[‘Arm Security’] = ‘On’
commandArray[‘TEMP Set to 15’] = ‘On’
return commandArray


This script runs the system when the dummy switch named “leaving” is turned on. It is the main script that operates the “Leaving” switch to function. It turns the cameras and other installed devices.

Now you need to write the script for the “Security Alarm” switch. On the first code, the ‘Security Alarm’ is turned off. If that is turned on, you will be receiving plenty of notifications or alerts as you walk around your home. Here you want to add a delay to the arming of the system. This is a timed script, so you will have to start with this text, “script_time” not the “script_device”. Then save it as “script_time_SECURITY_Leaving.lua” and save it to the same location as the first one.

t1 = os.time()
s = otherdevices_lastupdate[‘Arm Security’]

year = string.sub(s, 1, 4)
month = string.sub(s, 6, 7)
day = string.sub(s, 9, 10)
hour = string.sub(s, 12, 13)
minutes = string.sub(s, 15, 16)
seconds = string.sub(s, 18, 19)

commandArray = {}

t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
difference = (os.difftime (t1, t2))
print (‘Leaving difference ‘ .. difference)

if (difference > 300 and otherdevices[‘Security Alarm’] == ‘Off’ and otherdevices[‘Arm Security’] == ‘On’) then
commandArray[‘Security Alarm’] = ‘On’
commandArray[‘Arm Security’] = ‘Off’
print (‘Security Alarm is now armed.’)
commandArray[‘SendNotification’]=’Security Armed#Security alarm is now armed.’

if (difference > 600 and otherdevices[‘Waiting for Phone’] == ‘Off’ and otherdevices[‘Leaving’] == ‘On’) then
commandArray[‘Waiting for Phone’] = ‘On’
commandArray[‘Harry Phone’] = ‘Off’
commandArray[‘Chester Phone’] = ‘Off’
print (‘Waiting for phones to return.’)

return commandArray

On the script, you can remove few potential devices to set the alarm, depending on your preference. On that code, there is another switch present, the “SECURITY Living Room”. It’s a switch linked to cameras in the living room. With the inbuilt scripting on Domoticz, the system will send you photos from the cameras via email; it can also send to multiple recipients. A feature that allows you to monitor every event in your home.

All the scripts above checks how long the “Arm Security” switch was activated. If it has been five minutes, then the “Security Alarm” switch is turned on. If it has been ten minutes, the security system will start looking for phones. It can be difficult to pick the best interval for this setting if you are living on a block of flats. However, due to a series of testing, it turned into more than a ten-minute delay, since the phone is still within the WiFi range. Where it resulted in the system to shut down the “Security Alarm” switch.

With this result, you have to write a script that will watch out for your phones that will deactivate the system. Here’s a three-part script that allows you to fix this issue. The first part is a script that controls the timing, while the other two remaining parts will try to find the phones using a quick Python script. Save the first section as “script_time_SECURITY_Phones.lua” on this location, domoticz/scripts/lua folder.
commandArray = {}
if otherdevices[‘Waiting for Phone’] == ‘On’ then
os.execute(‘python3 ./ &’)
os.execute(‘python3 ./ &’)
return commandArray

As you can see, at this point you need to use Python programs. Technically they are the same. However, the phones have their respective IP address, and both of them has their switch created in Domoticz, stored in the Domoticz folder itself. The root folder of Domoticz.

This script can be labeled as

import urllib
import requests
from random import randint
import base64,requests,json,time,datetime
import os
Detects Harry’s phone and switches Domoticz if found.
hostname = “”
response = os.system(“ping -c 1 ” + hostname)
#and then check the response…
if response == 0:
print (hostname, ‘is up!’)
req = requests.get(‘¶m=switchlight&idx=176&switchcmd=Onlse:
print (hostname, ‘is down!’)
req = requests.get(‘¶m=switchlight&idx=176&switchcmd=Off

The above script is a command to look for Phone1, where the IP address is fixed to Then it switches a switch in Domoticz if Phone1 is detected or not. The sample above shows that Domoticz has given Phone1 as “Harry’s Phone” switch the number 17. That is the line you have to edit, depending on whether or not Phone1 is detected.

The following script deactivates the security alarm once the phones are detected. Named as “script_device_SECURITY_Phones.lua” and saved in domoticz/scripts/lua:

commandArray = {}
if devicechanged[‘Waiting for Phone’] == ‘On’ then
commandArray[‘Harry Phone’] = ‘Off’
commandArray[‘Chester Phone’] = ‘Off’
if (devicechanged[‘Chester Phone’] == ‘On’ and otherdevices[‘Waiting for Phone’] == ‘On’ and otherdevices[‘Leaving’] == ‘On’) then
commandArray[‘Leaving’] = ‘Off’
print(“Chester’s phone detected.”)
commandArray[‘SendNotification’] = ‘Security Message#Chesters phone detected.  Disarming system and switching on devices.’
if (devicechanged[‘Harry Phone’] == ‘On’ and otherdevices[‘Waiting for Phone’] == ‘On’ and otherdevices[‘Leaving’] == ‘On’) then
commandArray[‘Leaving’] = ‘Off’
print(“Harry’s phone detected.”)
commandArray[‘SendNotification’] = ‘Security Message#Harrys phone detected.  Disarming system and switching on devices.’
if (devicechanged[‘Chester Phone’] == ‘On’ and otherdevices[‘Waiting for Phone’] == ‘On’ and otherdevices[‘Leaving’] == ‘Off’) then
commandArray[‘Waiting for Phone’] = ‘Off’
print(“Chester’s phone detected.  No action taken.”)
if (devicechanged[‘Harry Phone’] == ‘On’ and otherdevices[‘Waiting for Phone’] == ‘On’ and otherdevices[‘Leaving’] == ‘Off’) then
commandArray[‘Waiting for Phone’] = ‘Off’
print(“Harry’s phone detected.  No action taken.”)
return commandArray

Now you’re almost done. This script is called “script_device_SECURITY_Return.lua” and saved in domoticz/scripts/lua. It’s a script that tells Domoticz what should be switched on when one of the phones is detected.

commandArray = {}
if devicechanged[‘Leaving’] == ‘Off’ then
commandArray[‘Power Up’] = ‘On’
commandArray[‘Living Room Camera’] = ‘Off’
commandArray[‘Arm Security’] = ‘Off’
commandArray[‘Waiting for Phone’] = ‘Off’
commandArray[‘Security Alarm’] = ‘Off’
commandArray[‘Environment Automation’] = ‘On’
if otherdevices[‘VAR Dusk’] == ‘On’ then
commandArray[‘DIMMER TV Lamps’] = ‘Set level 100’
commandArray[‘Front Balcony Lights’] = ‘On’
commandArray[‘Air Purifier’] = ‘On’
commandArray[‘Living Room TV’] = ‘On’
commandArray[‘Washing Machine’] = ‘On’
commandArray[‘Cat Sitter’] = ‘Off’
return commandArray

At this point, there’s only one thing left to do. Which is to decide what happens when the alarm is activated. You can turn on a network speaker to produce sounds, turn on your lights switch on an electronic device or alert you through the Domoticz alert function. There plenty of things you can do, you have a vast range of options. Here’s a useful bit of code stored in domoticz/scripts/lua and named as “script_device_SECURITY_Sensors.lua”

commandArray = {}
if (devicechanged[‘DOOR Entrance’] == ‘Open’ and otherdevices[‘Security Alarm’] == ‘On’) then
commandArray[‘SendNotification’] = ‘Security Message#Front door opened.’
commandArray[‘VAR Entrance’] = ‘On’
commandArray[‘SECURITY Entrance’] = ‘On’

elseif (devicechanged[‘DOOR Hallway’] == ‘Open’ and otherdevices[‘Security Alarm’] == ‘On’) then
commandArray[‘SendNotification’] = ‘Security Message#Hallway door opened.’
commandArray[‘SECURITY Living Room’] = ‘On’

elseif (devicechanged[‘DOOR Hallway’] == ‘Closed’ and otherdevices[‘Security Alarm’] == ‘On’) then
commandArray[‘SendNotification’] = ‘Important Security Message#Hallway door closed!’
commandArray[‘SECURITY Living Room’] = ‘On’
commandArray[‘VAR Entrance’] = ‘On’
return commandArray

It’s a small bit fragment of the entire network of sensors that will trigger your security alert if something unusual happens around your home.

A Brief Recap

This particular system is a more difficult one to set up, it can be a very complicated system but it is a proven security system that works flawlessly. It’s an efficient and reliable security system that will give you a peace of mind while you’re away. It’s a very creative way of utilising unconventional devices for security purposes.

Leave a Reply 0 comments

Leave a Reply: