Setting up a "lighweight" alarm with Shelly Motion, MQTT, Openhab and Telegram

In this post I want to show you how you can setup an easy alarm system using Shelly Motion detectors, MQTT, Openhab and Telegram. This is for sure no replacement for a real alarm, but it will give you a good idea and at least some protection :)

Setting up Shelly Motion, MQTT and Openhab

How to setup your Shelly Motion using MQTT and Openhab can be found here.

Setting up Telegram

I have chosen Telegram over other chat programs as it is secure, private and very easy to set up.

To get started you need to register a bot. An introduction how to do this can be found here.

Once you have the bot set up you will need to add the bot to a conversation and then get the bot token. You get the bot token at the end of the bot registration process.

As a next step you need to get the chat id. This can be done with the following comman:

curl https://api.telegram.org/bot<bot_token>/getUpdates | jq '.result[0].my_chat_member.chat.id'

For this you obviously need curl and jq installed:

sudo apt install curl jq

As a next step you create a send message shell script and save it (I have called it send_message.sh)

#!/bin/sh
curl -X POST -H 'Content-Type: application/json'-d '{"chat_id": "<chat_id>", "text": "There has been motion detected but nobody is home.Go go go, take care of it or call the police!", "disable_notification": true}' https://api.telegram.org/bot<bot_token>/sendMessage

As a next step make the script executable

sudo chmod +x /path/to/script/send_message.sh

Setting up the items in Openhab

In Openhab you now need to set up the things and items for handling the presence detection. Presence detection is done via the cell phones and the Shelly Motion detectors. For the cell phone detection you need to add the network binding in Openhab.

As a next step set up the cell phone as things:

Thing network:pingdevice:cellphone1  [ hostname="192.168.xx.xx" ]
Thing network:pingdevice:cellphone2  [ hostname="192.168.xx.xx" ]

And then the items:

//Groups
Group:Switch:OR(ON,OFF)     gPresence     "gPresence"	    <icon_presence>
Group:Switch:OR(ON,OFF)     gMotionSensor "gMotionSensor"	<motion_sensor>

//Motion Detector
Switch GF_Corridor_MS_Motion "Motion detected"    <switch>  (GF_Corridor, gMotionSensor)   ["MotionDetector"]    {channel="mqtt:topic:MyMQTTBroker:shelly_shellysense_xxxx:Motion"}

//Cell phones
Switch CP_Phone_1 "CP1 is home"  <mobile>    (Home, gPresence)  ["Smartphone", "Switch", "Presence"]  { channel="network:pingdevice:cellphone1:online" }
Switch CP_Phone_2 "CP2 is home"  <mobile>    (Home, gPresence)  ["Smartphone", "Switch", "Presence"]  { channel="network:pingdevice:cellphone2:online" }

The groups are necessary as they will be used in the rules later.

Setting up the rules in Openhab

In order to send a message when there is motion detected but nobody home you have to create a rule which checks if the group gMotionSensor receives and update and then if gPresence is ON or OFF. ON means that at least one person (cell phone) is at home and OFF means that no person (cell phone) is at home. At home here means connected to the WiFi :)

The rule looks like this

rule "Presence detection alarm"
when
    Member of gMotionSensor received command ON or
    Member of gMotionSensor received update ON
then
    logInfo("MotionSensor", "gMotionSensor motion detected => Checking presence")
    if(gPresence.state === ON){
        logInfo("MotionSensor", "People are home, let's move on")
    }else{
        logInfo("MotionSensor", "Something is wrong, let's contact everyone")
        executeCommandLine(Duration.ofSeconds(5), "/openhab/userdata/send_message.sh") 
    }
end

That's it. Now you can try it out. E.g. an easy way to try it is to change the group for gPresence from OR to AND and simply take one cell phone off the WiFi. Now when you walk by a Shelly Motion detector a message is sent to your telegram chat.