Advanced PIR sensors features in OpenHAB

Advanced PIR sensors features in OpenHAB

The HC-SR501 PIR sensor contains mechanical adjustement for range detection and time. But in an intelligent house, we want to be able change time how long the light will be active and change whether the light will be controlled by PIR or not. You can find a tutorial how to config your PIR sensor to enable this features in this article.


PIR sensor HC-SR501 or any other PIR sensor (Zigbee, Z-Wave)

Pi-Home - you can find tutorial how to install it in section Basic - OpenHAB on Raspberry Pi


OpenHAB 3


Create Items with content below in OpenHAB 3. First two rows are PIR sensors and last two rows are triggers, which active or deactive the PIR sensor for light.

/* PIR sensors */
Switch SP111 "PIR Vestibule"      <motion> (SensorPIRThing,motions,GS111) ["Status","Presence"]  { channel="mqtt:topic:pihome:pirthing:SP111" }
Switch SP121 "PIR Toilet"         <motion> (SensorPIRThing,motions,GS121) ["Status","Presence"]  { channel="mqtt:topic:pihome:pirthing:SP121" }

/*Triggers */   
Switch SP111_trigger "PIR Trigger Vestibule"      <motion> (SensorPIRThing,motions,GS111)       ["Point","Noise"]  
Switch SP121_trigger "PIR Trigger Toilet"         <motion> (SensorPIRThing,motions,GS121)       ["Point","Noise"] 

Then go to Rules and create new rule for lights. For example PIR SP111 could looks as below:

if you click to "execute a given script" (DSL rule) there is a code below:

In final, we will add Metadata -> Expiration timer for item SP111. Set"update state", state: OFF for 1 minute. See below:

How it works?

PIR sensor detect motion and change state to "ON". If there is a astronomical night (sun phase !=DAYLIGHT) and if SP111_triger is "on", then run the action in rule. Rule script contains code, which turn on L111 if it turned off. SP111 has expiration set to 1 minute. After 1 minute, SP111 goes "OFF". If the light L111 is "ON" then turn it "OFF". If the PIR sensor get update in meantime, it restarts the expiration timer.

Tip: In case of alarm state, PIR sensors act as motion detection for panic state and set notification to email or telegram.

Set HC-SR501 range of detection to minimum (right orange knob).

/* PIR sensors */
/*Triggers */    
Switch pir1  "PIR 1 TRIGGER"    (PIR)          {mqtt="<[mosquitto:pir:command:ON:pir1on]",expire="20s,command=OFF"}
Switch pir2  "PIR 2 TRIGGER"    (PIR)          {mqtt="<[mosquitto:pir:command:ON:pir2on]",expire="20s,command=OFF"}

/*Toggle Settings */
Switch pir1_toggle   "Svícení objektu 1 na čidlo"       (PIR)
Switch pir2_toggle   "Svícení objektu 2 na čidlo"       (PIR)
Number pir1_timer    "Délka sepnutí [%.0f s] pro objekt 1 "   (PIR)
Number pir2_timer    "Délka sepnutí [%.0f s] pro objekt 2 "   (PIR)

Next, create a pir.rules file with the following content in the OpenHAB rules folder. Here, we directly check if light  (e.g. Light_Gar_1) is triggered via PIR (pir1_toggle.state == ON). If yes, turn the light ON until the time (pir1_timer) and if we are still under the sensor this set time is extended.

var Timer occTimepir1 = null
var Timer occTimepir2 = null

/*PIR 1 */
rule "PIR_1"
    Item pir1 received update ON
    if(pir1_toggle.state == ON){
       sendCommand(Light_Gar_1, ON)
    if(occTimepir1 === null || occTimepir1.hasTerminated()) {        
        occTimepir1 = createTimer(now.plusSeconds((pir1_timer.state as Number).intValue), [|
            sendCommand(Light_Gar_1, OFF)
        occTimepir1 = null
    else {
        occTimepir1.reschedule(now.plusSeconds((pir1_timer.state as Number).intValue))

/*PIR 2 */
rule "PIR_2"
    Item pir2 received update ON
    if(pir2_toggle.state == ON){
       sendCommand(Light_Gar_2, ON)
    if(occTimepir2 === null || occTimepir2.hasTerminated()) {        
        occTimepir2 = createTimer(now.plusSeconds((pir2_timer.state as Number).intValue), [|
            sendCommand(Light_Gar_2, OFF)
        occTimepir2 = null
    else {
        occTimepir2.reschedule(now.plusSeconds((pir2_timer.state as Number).intValue))

The connection with the Astro binding at OpenHAB can be a very useful to use at home as the HC-SR501 does not contain luminescence sensor. Astro binding enable lights up only when it's astronomically dark, after sunset, or other day time. You can also use PIR sensors as alarm when nobody is in the house. Such a nice functionality for 2 €. Have a fun!

Rate the article:

Average: 4 (1 vote)

Support Us:

Add comment:

Add comment

Newest articles in blog

Nilan VP18 vs OpenHABRating: 

We provide tutorial for the connection of the active Nilan recuperation unit, in our case the VP18 K WT type with OpenHAB 3. By communicating via Modbus it is possible to read the whole range of values, but mainly also to control the unit through OpenHAB. This allows us to remotely or programmatically change the performance and ventilation settings depending on the mode (night, cooking, etc.)

PiHome - Chytry dum mereni spotreby elektriny
Energy meter in smart homeRating: 

We are providing a simple step-by-step tutorial how to install home energy meter. We did not find any MQTT device, so we buy Aeotec Home Energy Meter as we are already have a Z-Wave controlller in our smart home.

Centrální tablet pro chytrý dům
Wall tablet to controll your smart homeRating: 

Wall tablet or older smartphone is perfectly suited as a central indicator of the state of the home, as well as controlling it. It usually replaces the video doorbell and as example, you can play your favorite music or read recipes on it during cooking in your kitchen. In the article, we describe a functional example of such a "wall" tablet and its settings in so-called kiosk mode so, display doesn't run at night and only reacts when someone is nearby.

Jablotron and smarthome synchronization
Connecting Jablotron with OpenHABRating: 

In this article we will show you how to install Jablotron binding in OpenHAB. This binding helps us to synchronize states from Jablotron alarm to OpenHAB. Typically, you can force to switch off lights, close water pipe and circulation, switch off sockets or whatever you want to do when you leave home and set alarm.

Advanced disk cloningRating: 

The classic disk cloning tool in linux is the dd command. This command creates a image of disk or extracts the contents from the image to disk. The dd tool needs to be handled carefully and you need to understand its use. We'll show a more advanced technique of creating a disc image that contains unallocated space. A classic example is when I have a Raspbian operating system installed on a 250 GB disc, but there's only a tiny boot partition (e.g. sda1) and my own operating system partition (sda2) which together have 15 GB. Instead of saving a 250 GB image, I would like to save only two partitions with 15 GB, so that the image is smaller and can be expanded to 16 or 32 GB flash.

iOS OpenVPN Connection SettingsRating: 

In this article you will find detailed instructions on how to connect to OpenVPN from iOS devices.

Android OpenVPN Connection SettingsRating: 

In this article you will find detailed instructions on how to connect to OpenVPN on Android.

OpenVPN on MikroTik routersRating: 

If you have a smart-home, NAS, etc., it's a good idea to think about safe access to your network when you are not at home. Currently I recommend commercial router like Turris or a cheaper option of MikroTik routers. In the other hand, Raspberry Pi can be used as OpenVPN server too. In this article, we will show how to set up secure access to your home network from the internet using OpenVPN on MikroTik routers.

Video intercom Doorbird in OpenHABRating: 

Native support for OpenHAB is provided by video intercom by Doorbird company. The solution is powered and connected by a UTP cable and has its own IP address to make it easier for you to install at the gate. OpenHAB is then able to import images from the intercom, visit history, manage gate opening on the button and by the phone, do not disturb feature during night, alarm feature etc. In the instructions we guide you how to use OpenHAB with Doorbird D101.