Domoticz Presence detection

Veröffentlicht von philipp am

I know there are a few other presence detection methods in the Domoticz-Forum, but I want to show just another approach:
I wanted to track if my girlfriend and I am at home or not, to enable or disable a camera motion sensor in Domoticz. So I needed a reliable way of presence detection.

I’ve tried both versions of presence detection from the Domoticz-Wiki (Ping- and SNMP-Method), but soon I realised that both had its own problems with detecting devices in the network. For example:

  • switching on and off the virtual switch although the device was still in the network
  • device not detected when its in sleep mode
  • snmp device status did not update for about 20mins (when I disconnected the phone from the network)

Therefore I took another approach which I found here and works just perfectly: Let my Asus router do the detection work instead of the raspberry pi.
My home router is an Asus RT-AC68U which I have flashed with the Asus Merlin custom firmware, but every router for example with OpenWrt, DD-WRT, etc. should work as long as you can SSH into it and read out the connected wifi devices. Note: My router has a Broadcom Wifi-chipset and the script provided in step VII. should work on all Broadcom devices. This post is written for routers with Asus Merlin firmware. On OpenWrt, DD-WRT etc. some details have to be made differently!

So here we go:

  1. Create a Dummy Hardware and add a virtual switch for it in Domoticz. Please have a look at the Wiki for how to do this.
  2. Flash Asus Merlin firmware (it is really easy but please take care of the information provided at their [url=https://github.com/RMerl/asuswrt-merlin/wiki/Installation]website[/url])
    1. visit the [url=https://asuswrt.lostrealm.ca]Asus Merlin website[/url], download the latest firmware and extract it
    2. log into your router by going to its local address (for example https://192.168.1.1:8443), accept the certificate and enter your username and password
    3. click on the „Administration“ panel on the left –> „Firmware Upgrade“ –> „Choose File“ –> select extracted file and hit Upload. It can take a few minutes for your router to upgrade and reboot. You have to manually power-cycle you router after the update has finished
  3. start the SSH daemon on your router by logging into it and then going to „Administration“ –> „System“ –> adapt the settings according to the image:
    You can also choose another port, it doesn’t matter, you just have to remember it for the next steps.
  4. Also click the „Enable JFFS custom scripts and configs“ badge
  5. Click „Reboot“ at the top of the page and wait until the reboot finished
  6. SSH into your router
    • Windows: download putty
      • IP: your router ip
      • Port: port which you have entered above
      • Connection Type: SSH
      • Hit Open and enter your username and password which you also use to login into the router website
    • Mac or Linux-Systems: open a terminal and type:
      ssh -p 2222 router-username@router-ip-address

      Hit the Enter key on your keyboard. Type the router password (you wont see what you are writing, just type the password) and hit Enter again.
    • You have to type „yes“ (without quotes) when there is the question if you want to add this device to the known-hosts file and hit the Enter key
  1. You should now see a blinking cursor. Type the following (Enter after each line):
cd /jffs/scripts/
nano init-start

Paste the following into this file and replace MAC-ADDRESS by the mac address you want to detect and IDX with the virtual switch ID which we have created in step a. (e.g. cru a PRESENCE „*/1 * * * * /jffs/scripts/presence_detection A1:B2:3C:D4:5F:67 27“):

#! /bin/sh
cru a PRESENCE "*/1 * * * * /jffs/scripts/presence_detection MAC-ADDRESS IDX"

Press CTRL+O then Enter to save the file. Then CTRL+X to exit nano.

  1. Now:
nano presence_detection

Paste this into the file and replace domoticzUser:domoticzPassword@domoticzIP:domoticzPort with the right settings (e.g. domoticz:myPASS123@192.168.1.100:8080)

#!/bin/sh

mac=$1
idx=$2
device_present=Off
domoticz_status=`curl -s "http://domoticzUser:domoticzPassword@domoticzIP:domoticzPort/json.htm?\
type=devices&rid=$idx" | grep Status | awk '{print $3}' | sed 's/[",]//g'`

# look in 2.4GHz (eth1) network for device
for x in `wl -i eth1 assoclist | awk '{print $2}'`; do
 if [ $x = $mac ]; then
        device_present=On
 fi
done

# look in 5GHz (eth2) network for device
for x in `wl -i eth2 assoclist | awk '{print $2}'`; do
 if [ $x = $mac ]; then
        device_present=On
 fi
done

# tell domoticz the new device status
if [ $domoticz_status != $device_present ]; then
 if [ $device_present = "On" ]; then
        curl -s "http://domoticzUser:domoticzPassword@domoticzIP:domoticzPort/json.htm?\
        type=command&param=switchlight&idx=$idx&switchcmd=On" > /dev/null
 else
        curl -s "http://domoticzUser:domoticzPassword@domoticzIP:domoticzPort/json.htm?\
        type=command&param=switchlight&idx=$idx&switchcmd=Off" > /dev/null
 fi
fi

Again press CTRL+O then Enter to save the file. Then CTRL+X to exit nano.

If you have a protected switch (thanks user timop!) you have to replace the following three lines:

replace
domoticz_status=`curl -s "http://domoticzUser:domoticzPassword@domoticzIP:domoticzPort/json.htm?\
type=devices&rid=$idx" | grep Status | awk '{print $3}' | sed 's/[",]//g'`
with
domoticz_status=`curl -s "http://domoticzUser:domoticzPassword@domoticzIP:domoticzPort/json.htm?\
type=devices&rid=$idx&passcode=switchPassword" | grep Status | awk '{print $3}' | sed 's/[",]//g'`

replace
curl -s "http://domoticzUser:domoticzPassword@domoticzIP:domoticzPort/json.htm?\
type=command&param=switchlight&idx=$idx&switchcmd=On" > /dev/null
with
curl -s "http://domoticzUser:domoticzPassword@domoticzIP:domoticzPort/json.htm?\
type=command&param=switchlight&idx=$idx&switchcmd=On&passcode=switchPassword" > /dev/null

replace
curl -s "http://domoticzUser:domoticzPassword@domoticzIP:domoticzPort/json.htm?\
type=command&param=switchlight&idx=$idx&switchcmd=Off" > /dev/null
with
curl -s "http://domoticzUser:domoticzPassword@domoticzIP:domoticzPort/json.htm?\
type=command&param=switchlight&idx=$idx&switchcmd=Off&passcode=switchPassword" > /dev/null

Where „switchPassword“ stands you have to enter the password that you have set in Domoticz (Setup->Settings->Light/Switch Protection).

Now we have to set the correct permissions. Enter:

chmod 755 init-start
chmod 755 presence_detection

Then we test the script we have just created by typing (replace MAC-ADDRESS by the mac address you want to detect and IDX with the virtual switch ID):

./presence_detection MAC-ADDRESS IDX

If there is no error after executing the script and I have not forgotten to describe a step and the device you want to detect is in your network, the virtual switch in Domoticz should change its state.

After these steps you have to reboot your router (you can do this again from the routers website). After the reboot the router calls the /jffs/scripts/init-start script which adds a cronjob which calls the presence_detection script every minute.

I hope someone can use it. Its the solution I like best at the moment as the switches are updated nearly instantly (the only delay comes from the cron timeout).


0 Kommentare

Schreibe einen Kommentar

Avatar-Platzhalter

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert