Skip to main content
loading
By Michal Novotný
December 14, 2025
Alarm from PIR sensors in a smart home

In a your smart home, PIR sensors may not only be used to switch lights on and off based on motion, it is possible to utilize these sensors to detect the presence of motion in a particular room. This information can be used to create a relatively reliable uncertified home security system. In this guide, you will find the logic for how this can work in the OpenHAB software in our model smart home.

Hardware

HC-SR501 motion PIR sensors

Pi-Home - if you don't have it, see the tutorial section

Software

OpenHAB 3

 

Application

We will use the existing PIR sensors SP111 - SP241, whose items we set up in the tutorial.

We will also set up two new Items:

Alarm - used to set the alarm and type (Full security, Night (garden + ground floor), only garden, only ground floor).

In Add Metadata -> State Description, set, for example, these states (Options):

0=None
1=Full
2=Night
3=Garden

Panic - triggers the alarm due to foreign movement (specific description of which room triggered the alarm) or alerts for smoke, flooding, etc. according to other sensors

 

Logic:

We need to set rules. Our chosen logic is: If a specific PIR sensor detects movement and the Alarm setting is not 0 (None) but is, for example, Night or Full, activate the Panic item and assign it the status according to the PIR name. Another rule monitors the Panic item. If it changes to a non-zero value and Alarm is not 0 (None), send an email/telegram according to the type of alarm (specific PIR that triggered the event).

Example rule for PIR, in this case SP102, which is a sensor in the hallway on the ground floor and is set to send an alarm if Full or Night is set. Add a new rule and insert the following code in the Code tab:

configuration: {}
triggers:
  - id: "1"
    configuration:
      itemName: SP102
      state: ON
    type: core.ItemStateChangeTrigger
conditions: []
actions:
  - inputs: {}
    id: "3"
    configuration:
      type: application/vnd.openhab.dsl.rule
      script: |-2
         if(Alarm.state == 1 || Alarm.state == 2){
           sendCommand(Panic, "Alarm vestibule")
           }
    type: script.ScriptAction

Example rule for Panic (also in the Code tab):

configuration: {}
triggers:
  - id: "1"
    configuration:
      itemName: Panic
    type: core.ItemStateUpdateTrigger
conditions:
  - inputs: {}
    id: "2"
    configuration:
      itemName: Alarm
      state: "0"
      operator: "!="
    type: core.ItemStateCondition
actions:
  - inputs: {}
    id: "3"
    configuration:
      type: application/vnd.openhab.dsl.rule
      script: "val mailActions = getActions(\"mail\",\"mail:smtp:mailserver1\")

        val PanicState = Panic.state.toString() \ 

        mailActions.sendHtmlMail(\"YOUREMAIL@EMAIL.COM", PanicState,
        \"\")

        \         "
    type: script.ScriptAction
  - inputs: {}
    id: "4"
    configuration:
      type: application/vnd.openhab.dsl.rule
      script: >2
          val telegramAction = getActions("telegram","telegram:telegramBot:YOURTELEGRAMBOT")
          val PanicState = Panic.state.toString()  
          telegramAction.sendTelegram(PanicState)
    type: script.ScriptAction

 

Now you just need to copy and adjust the SP102 rule for all other PIRs and test the functionality. Email setup can be found in the guide.

I also recommend looking into Telegram notifications directly from your OpenHAB here.

 

Tags

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.