Creating a beautiful front-end for Domoticz

In a previous post (here), we created a simple front-end for Domoticz running on a Raspberry. In this post, we’ll take it a step further and style it a bit to look beautiful. It’s good if you go through the previous post first since we are going to build on that code base.

Areas you need to be familiar with to follow this post

  • Code base used in previous post (here)
  • Bootstrap (you can download it here)
  • CSS

Starting with the design

At the end of this post we want to have a design of our user interface looking like this:

front-end_design

Adding the Bootstrap structure to the HTML file

We have downloaded Bootstrap and extracted the folders in the folder where we have our HTML file from previous post. We add the following two lines of code to include the Bootstrap CSS and JS libraries.

[code language=”html”]
<!DOCTYPE html>
<html>
<head>
<title>Home Security Frontend</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="http://code.jquery.com/jquery.js"></script>
<!– Bootstrap –>
<script src="js/bootstrap.min.js"></script>
<link href="css/bootstrap.min.css" rel="stylesheet" media="screen">
</head>
<body>
[/code]

Adding rows for the Bootstrap structure

In the body of our HTML file, we add three Bootstrap rows. The first one is for adding a nice logo and a ticking clock, the second one is for adding the house icon and the status of the security panel, and the final one is for listing all the sensor information. All three rows must be wrapped in a <div> with a class labeled ”container”.

[code language=”html”]

&lt;div class="container"&gt;

&lt;div class="row"&gt;

&lt;div class="col-sm-12" id="top-bar"&gt;
&lt;img id="yellington_logo" src="https://www.yellington.com/yellington_logo.png"&gt;

&lt;div id="clock"&gt;&lt;/div&gt;

&lt;/div&gt;

&lt;/div&gt;

&lt;div class="row"&gt;

&lt;div class="col-sm-6 v-align" style="text-align: right;"&gt;

&lt;span class="glyphicon glyphicon-home" id="house-image"&gt;&lt;/span&gt;

&lt;/div&gt;
&lt;div class="col-sm-6 v-align" id="house-status-text"&gt;&lt;!– keep the opening a closing div tags together to avoid white space causing line breaks in the user interface–&gt;
&lt;/div&gt;

&lt;/div&gt;

&lt;div class="row" id="sensor-container"&gt;
&lt;/div&gt;

&lt;/div&gt;

[/code]

The glyphicon image is part of the Bootstrap package and we will use it as our house image in the first row. The ”v-align” class will be used in the CSS/style part of our HTML file to align the house image vertically in the middle with the status text of the security panel. Keeping <div></div> on line 14 together is intentional to avoid adding any whitespace when rendering the two columns on the same row in the browser.

Expanding the JavaScript functionality

We will expand the JavaScript part of our HTML file with the following:

  • Wrap the functionality of updating the sensor data in a separate function: updateSensors()
  • Add a function: getSensorImage() to borrow some nice looking sensor images from Domoticz
  • Add the time in the top row. This will be a ticking clock since we update the data automatically with one-second intervals
  • Fetch the status of the security panel
  • Finally set a timer with a one-second interval to update all data automatically.

The script code looks like this:

[code language=”javascript”]
&lt;script&gt;
$(document).ready(function(){

var domoticzURL = "http://192.168.1.99:8080/"; //use this format if you want to place this file on another computer on your home network.
//var domoticzURL = "../"; //use this format if you place this file in a folder in the www folder on your Domoticz server.

$(function() {
updateSensors();
});

function getSensorImage(sensorType){
//This function returns the image of the sensor that is stored in the image folder on your Domoticz server

var sensorURL = "";

//The list of sensor types can be expanded dependant on what type of sensors you have
switch(sensorType) {
case "Light/Switch":
return domoticzURL + "images/Light48_On.png";
break;
case "Temp + Humidity":
return domoticzURL + "images/temp-20-25.png";
break;
default:
return domoticzURL + "images/Light48on.png";
}

}

function updateSensors() {

var sensorInfo = "";

//First we publish the time in our user interface
var d = new Date();
var hours = d.getHours();
if (hours &lt; 10) { hours = ’0’ + hours;}
var minutes = d.getMinutes();
if (minutes &lt; 10) { minutes = ’0’ + minutes;}
var seconds = d.getSeconds();
if (seconds &lt; 10) { seconds = ’0’ + seconds;}
document.getElementById("clock").innerHTML = hours + ":" + minutes + ":" + seconds;

//We request a list of all sensor data and loop through it to publish on our web page
$.getJSON(domoticzURL + "json.htm?type=devices&amp;filter=all&amp;used=true&amp;order=Name", function(result){

for (i = 0; i &lt; result.result.length; i++) {
sensorInfo += ’
&lt;div class="col-sm-6 col-md-4 col-lg-3 sensor-outer-box"&gt;’+

&lt;div class="sensor-box"&gt;’+
’&lt;img class="sensor-icon" src="’ + getSensorImage(result.result[i].Type) +’"&gt;’+

&lt;div class="sensor-data"&gt;’+

&lt;h4&gt;’ + result.result[i].Name + ’&lt;/h4&gt;

’+

&lt;h5&gt;Status: ’ + result.result[i].Data + ’&lt;/h5&gt;

’+

Updated: ’ + result.result[i].LastUpdate +’

’+
’&lt;/div&gt;

’+
’&lt;/div&gt;

’+
’&lt;/div&gt;

’;
}

document.getElementById("sensor-container").innerHTML = sensorInfo;

//We retrieve the status of the security system, baically Disarmed, Armed Away, or Armed Home
$.getJSON(domoticzURL + "json.htm?type=command&amp;param=getsecstatus", function(result){

var homeSecurityStatus="";

if (result.secstatus==0) homeSecurityStatus="Disarmed";
else if (result.secstatus==1) homeSecurityStatus="Armed Home";
else if (result.secstatus==2) homeSecurityStatus="Armed Away";
else homeSecurityStatus="Unknown";

document.getElementById("house-status-text").innerHTML = ’
&lt;h5&gt;Home Security Status&lt;/h5&gt;

’+

&lt;h2&gt;’ + homeSecurityStatus + ’&lt;/h2&gt;

’;

//Finally we set a timer to repeatedly poll the status of the sensors
setTimeout(updateSensors(),1000);

});
});

}

});

&lt;/script&gt;
[/code]

Note that the final getJSON call is nested within the callback function from the first getJSON call to get the sensor data. The domoticzURL variable could either be set to the IP number of your Raspberry + port 8080 or, if you upload the HTML file in the www root of your Domoticz server, you can set it to ”../” or ”./” dependant on if you put it in a subfolder or not. If you open the HTML file in a browser it looks something like this, still not very beautiful.

front-end_without_css

Styling the user interface with CSS

For the simplicity of this post, we will put all the styling in the HTML file. You can just as well put it in a separate CSS file and include it in the <head>. We add the following styling:

[code language=”html”]
&lt;/script&gt;

&lt;style&gt;
body{background-color: grey; padding: 0px 1% 0% 1%;}
.container{padding: 0px 20px 20px 20px;border-radius: 5px; background-color: white; border-style: solid; border-color: grey; border-width: 2px;box-shadow: 0px 0px 10px white;margin-top: 2%}
.row {border-bottom-style: solid; border-color: grey; border-width: 1px;}
#yellington_logo {max-width: 100px; margin: 5px; }
#top-bar{text-align: left; margin-bottom: 1px; padding: 0px;}
#clock{float: right; margin: 5px;}

.v-align {float: none; display: inline-block;vertical-align: middle;} /*class for vertically aligning elements in a row*/

#house-image{font-size: 9em; vertical-align: middle; display: inline-block; margin-top: 10px; margin-bottom: 10px;}
#house-status-text{text-align: left;}

.sensor-outer-box {padding: 0px;}
.sensor-box{padding: 0px;margin: 5px; background-color: white; box-shadow: 5px 5px 10px #888888; border-style: solid; border-color: grey; border-width: 1px; border-radius: 5px;}
.sensor-icon{vertical-align: 80%; margin: 0px;}
.sensor-data {display: inline-block; margin: 0px;}

&lt;/style&gt;

&lt;div class="container"&gt;
[/code]

The result is a user interface with responsive design

Thanks to Bootstrap, we automatically get a user interface with a responsive design that works just as well on a laptop and tablet as it does on a smartphone.

On a laptop:

front-end_domoticz_wide

On a smartphone:

front-end_design

You can play around with the style part of the file and the Bootstrap columns definitions to completely change the look and feel of the user interface to your liking.

Advanced Events handling in Domoticz

In previous post (can be found here) we created a simple event handler in Domoticz to control a power plug based on the events from a motion sensor. In this post we will step it up a notch and build a basic alarm system. If you are not familiar with the Events interface in Domoticz, we recommend you go through our previous post first.

Building a basic alarm system using the Events interface in Domoticz

We want our alarm system to be able to do the following once we have set up the handling of the events:

  • Arm/disarm the alarm system
  • Trigger the alarm (if armed) with a delay of 45 seconds. The reason for this is that we want to be able to enter the door and have time to disarm the alarm without triggering
  • Stop the siren if entering the correct pin code
  • Let the siren time-out after 3 minutes. The alarm system will still be in ”armed” mode. If the burglar has not been scarred after 3 minutes, the siren will most likely only annoy your neighbours.

For this project, we will use the following hardware:

We will be using the Z-Wave Power Plug to switch on a lamp instead of a siren, since it’s more friendly for our ears while developing. Once you’re done, it’s simple to exchange the power plug for a Z-Wave siren.

Starting with a map of states for the alarm system

Since this is a slightly more advance event handling then simply have a motion sensor control a lamp, we need to map out which states our alarm system can have during operation:

  • State 1: Disarmed
  • State 2: Armed
  • State 3: Motion Triggered
  • State 4: Sound siren!

The alarm system will switch state dependant on the events appearing. Here is how the system will jump between different states:

  • Person arming the system on the security panel: the system will jump to ”Armed”.
  • Person disarming the system on the security panel: the system will jump to ”Disarmed” regardless of previous state, the siren will be turned off.
  • If the system is armed and the motion sensor is triggered: the system will jump to ”Motion Triggered”
  • If the system is in ”Motion Triggered” after 45 seconds: the system will jump to ”Sound siren!”
  • The siren has sounded for 3 minutes: the system will jump to ”Armed” and the siren will be turned off. The system is ready to detect new motion.

Sketching this on a piece of paper looks something like this:

states_map

I know it looks a bit cluttered but it will be a big help when building the event handling.

Controlling events using states in Domoticz

First we need to create two things that we need for building our alarm system:

  1. A user variable that we call: ”Alarm State”
  2. A dummy switch that we call: ”Sound Siren”

Creating a user variable in Domoticz

Go to the Domoticz ”Setup” drop-down menu and select ”More Options” -> ”User variables”.

user_variables_menu

Then enter the name ”Alarm State” and the variable value ”1”, and click ”Add”.

alarm_state_variable

Creating a dummy switch in Domoticz

Go to the ”Setup” menu and select ”Hardware”. This view should be familiar to you by now.

dummy_switch

Enter the name ”Sound Siren” and select ”Dummy” in the ”Type” drop-down list. Click ”Add”.

create_virtual_sensors

The dummy hardware appears in the list where your Raspberry is. Next, click on ”Create Virtual Sensors”.

create_virtual_sensor

Enter the name ”Sound Siren Dummy” and select ”Switch” in the ”Sensor Type” drop-down list. Click ”OK”. We will use this dummy switch to decide if we should start the siren. The condition is that we are in alarm state ”Motion Triggered” and that the delay time of 45 seconds has passed. Now, on to building the events handling.

Building the events handling using the Events (Blockly) interface

Select ”More Options” -> ”Events” from the ”Setup” menu. You can check the previous post if you’re not familiar with how the Events interface works (here). To arm our alarm system, we build the following event handler.

arm_alarm

This will move the alarm system from state 1 (Disarmed) to state 2 (Armed). You find the ”Alarm State” variable in the category ”User variables” in the left side menu. The ”Security status” switch is found in the ”Security” category and is directly controlled by the Domoticz security panel that we will use later on. We save the event with the name ”Arm alarm”. Don’t forget to tick the check box ”Event active”. Next we create an event handler for disarming the alarm system.

disarm_alarm

This event handler puts the alarm system in state 1 (Disarmed) regardless of previous state. We also sets our Z-Wave power plug to ”Off” since this represents our siren and we want the siren to be turned off once a valid pin code is entered on the security panel. Most often the siren is not on when someone enters the pin code since the most common use case is that you enter the pin code as soon as you open the door. In this case you have 45 seconds to disarm the alarm system before the siren goes off. We also make sure the dummy switch ”Sound Siren Dummy” is set to ”Off”. We save this event handler with the name ”Disarm alarm”. Next we will create the event handler for state 2 (Armed).

motion_triggered

This event handler captures the event of the motion sensor, puts the alarm system in state 3 (Motion Triggered), and starts a timer to sound the siren after 45 seconds. This time allows the owner of the house to disarm the alarm system without the siren going off. Save it as ”Motion triggered”. We’re almost done, we now make the event handler for jumping from state 3 to state 4 (Sound siren!).

sound_siren_3

If we get the event of ”Sound Siren Dummy” after the 45 seconds time delay in state 3, we should sound the siren. We also sets a 3 minute (180 seconds) time-out on the siren. Save this as ”Sound siren”. Finally we need to create an event handler for state 4 (Sound siren!).

siren_timeout

This will turn off the siren after 3 minutes and put the alarm system in state 2 (Armed) and the system will be ready to detect new movement. We save this event handler as ”Siren time-out” which will give us the following complete list of event handlers:

list_of_events

Next we go on to using the security panel of Domoticz.

Using and configuring the security panel in Domoticz

If you go to the ”Setup” menu and select ”More Options” -> ”Security Panel”, you see this view.

security_panel

With this panel, you can arm and disarm the alarm system with just built. You can select the pin code in the ”Setup” menu under ”Settings”. Here you can also set a delay time in order for you to have time to exit your house before the alarm system is armed. Once you have tested the alarm system to make sure it works ok, you can replace the Z-Wave power plug with a Z-Wave siren.

Using Events in Domoticz

In previous post we learned how to group two devices (post can be found here). In this post we will use the Events interface in Domoticz to tell a switch to act on a certain event that appears. We make sure that we have ”ungrouped” our Multisensor and Z-Wave power plug from previous post, since we are going to use events instead in this post. You ungroup in the same ”Groups & Network” view showed in previous post. In this post we will use Events in Domoticz to control a Z-Wave power switch when an Aeon Labs Multisensor is triggered.

Equipment used in this post to control using Events in Domoticz

We will be using the same equipment as previous post:

Opening the Events interface in Domoticz

You find the Events editor by selecting ”More Options” in the ”Setup” menu and the select ”Events”.

events_menu

In the Events editor. You find a list of categories on the left. You can see this as the toolbox of blocks you will be using to control and manage your events. Click on the first entry in the list ”Control”.

events_editor

You see two blocks that looks like pieces in a puzzle, which they in fact are. We will be combining puzzle pieces in order to control our Z-Wave power plug when our Aeon Labs Multisensor is triggered. We start by dragging the first ”If/Do” block piece to the white work space.

if_do

This ”If/Do” block will allow us to ”do” something ”if” something else happens. If you’re a programmer, this is simple for you. If not, you will understand when you go through the next steps. Since we want to DO turn on our Z-Wave power plug IF our motion sensor is triggered, we go to ”Devices” and select ”Switches”. Here we find our motion sensor.

our_motion_sensor

We click on ”Our motion sensor” in order for it to appear next to the ”If/Do” block that we place on the work space.

our_motion_sensor_workspace

We do the same thing with our Z-Wave power plug.

motion_s_power_p

Now, to control the logic so that the power plugs turns on when the motion sensor is triggered we need some additional logic blocks. Go to ”Logic”.

logic

Select the first green block with the ”=” sign so it appears on the work space.

logic_workspace

Go back to the ”Logic” category and select the green box that says ”On”. We now see the following blocks.

logic_blocks

Since we want to DO something IF our motion sensor turns on, we attach the blocks to one another like this:

combined_blocks

We almost done, but the power plug block is still not connected. We need a ”Set” block and an ”On” block to trigger the power plug.  Select these from the ”Logic” category as you did with the previous blocks.

set_blocks

We connect these blocks to the ”Do” part.

events_connected

The logic won’t just work yet. We need to save it.

save_event

On the right side in Domoticz, enter a name of your choice, tic the ”Event active” box, and click ”Save”. If you now trigger the motion sensor, the Z-Wave power plug turns on. Yes! If you want the power plug to turn off when the motion sensor turns off, you can build the following logic. Don’t forget to save it for it to work.

turn_off

The Aeon Labs motion sensor has a default time out set to 4 minutes.

In upcoming posts we will go through how to make more advanced logic using the Events editor in Domoticz.