28-01-2017: Added code for protected switches. Thanks user timop!
Hey there!
I know there are a few other presence detection methods in the 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)
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 h. 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:
- 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.
- Flash Asus Merlin firmware (it is really easy but please take care of the information provided at their website)
- visit the Asus Merlin website, download the latest firmware and extract it
- 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
- 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
- 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.
- Also click the "Enable JFFS custom scripts and configs" badge
- Click "Reboot" at the top of the page and wait until the reboot finished
- 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: 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.
Code: Select all
ssh -p 2222 router-username@router-ip-address
- 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
- Windows: download putty
- You should now see a blinking cursor. Type the following (Enter after each line):
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"):
Code: Select all
cd /jffs/scripts/ nano init-start
Press CTRL+O then Enter to save the file. Then CTRL+X to exit nano.Code: Select all
#! /bin/sh cru a PRESENCE "*/1 * * * * /jffs/scripts/presence_detection MAC-ADDRESS IDX"
- Now:
Paste this into the file and replace domoticzUser:domoticzPassword@domoticzIP:domoticzPort with the right settings (e.g. domoticz:[email protected]:8080)
Code: Select all
nano presence_detection
Again press CTRL+O then Enter to save the file. Then CTRL+X to exit nano.Code: Select all
#!/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¶m=switchlight&idx=$idx&switchcmd=On" > /dev/null else curl -s "http://domoticzUser:domoticzPassword@domoticzIP:domoticzPort/json.htm?\ type=command¶m=switchlight&idx=$idx&switchcmd=Off" > /dev/null fi fi
If you have a protected switch (thanks user timop!) you have to replace the following three lines:Where "switchPassword" stands you have to enter the password that you have set in Domoticz (Setup->Settings->Light/Switch Protection).Code: Select all
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¶m=switchlight&idx=$idx&switchcmd=On" > /dev/null with curl -s "http://domoticzUser:domoticzPassword@domoticzIP:domoticzPort/json.htm?\ type=command¶m=switchlight&idx=$idx&switchcmd=On&passcode=switchPassword" > /dev/null replace curl -s "http://domoticzUser:domoticzPassword@domoticzIP:domoticzPort/json.htm?\ type=command¶m=switchlight&idx=$idx&switchcmd=Off" > /dev/null with curl -s "http://domoticzUser:domoticzPassword@domoticzIP:domoticzPort/json.htm?\ type=command¶m=switchlight&idx=$idx&switchcmd=Off&passcode=switchPassword" > /dev/null
Now we have to set the correct permissions. Enter: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):Code: Select all
chmod 755 init-start chmod 755 presence_detection
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.Code: Select all
./presence_detection MAC-ADDRESS IDX
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).
Best regards,
Surroot