Presence detection for Fritzbox script v2.0: using TR064 protocol communication to your router

All kinds of 'OS' scripts

Moderator: leecollings

User avatar
capman
Posts: 153
Joined: Friday 12 July 2013 20:48
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Belgium
Contact:

Re: Presence detection for Fritzbox script v1.0: using TR064 protocol communication to your router

Post by capman »

Okee , thanks 4 looking in2.
Spoiler: show
#!/bin/bash

#presence_detection.sh

#script Presence detection for domoticz & Fritzbox v1.04
#created by g.j funcke - 4 feb 2019
#making use of fritzconnection url: https://pypi.org/project/fritzconnection/
#dependencies: lxml, jq and requests
#tested with: Fritzbox 7581 and Fritzbox Repeater 1750E should work with all routers who supporting TR-064 protocol.
#place fritzconnection.py and fritzhosts.py in same directory as this script.
#dont forget to change parameters router inside fritzconnection.py and activate TR-064 on router.
#standard debug information is on just set debug to false to disable it.
# if you have a repeater please specify ip below.


#setup device(s) change values here
mac_device1=94:65:2D:7D:DD:10
name_device1="OnePlus5-Kris"
mac_device2=64:A2:F9:B5:C0:02
name_device2="OnePlus-6T"
idxdevice1=672
idxdevice2=673

#setup router (http://fritz.box), repeater (Fritzbox repeater http://fritz.repeater) and domoticz change values
password_fritzbox=harddisk
host_fritzbox=192.168.178.1
#setup repeater, if you dont have any leave blank
host_repeater=192.168.178.77
#setup host domoticz
host_domoticz=192.168.178.99:8080

#set debug to true or false to see logging information, leave on when using for the first time.. you see some checks. Leave of after it works
enable_debug=true

#some styling ;-)
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color

#current directory script
cwd=$(cd -P -- "$(dirname -- "$0")" && pwd -P)

#get current status of device(s) in domoticz

if [ "$enable_debug" == "true" ]; then
clear;
echo "------------------------------------------------------------"
echo "Presence detection for Domoticz using Fritzbox version 1.0.4"
echo "------------------------------------------------------------"
echo -e "\n check availability of fritzbox"
sleep 1;
if ping -c 1 $host_fritzbox &> /dev/null
then
echo -e "${GREEN} Fritzbox is available${NC} gives answer on ip $host_fritzbox \n"
else
echo -e "${RED} Error!!!! fritzbox is not available, check ip adress of variable host_fritzbox"
exit 1
fi

if [ -n "$host_repeater" ]; then
echo "check availability of repeater"

if ping -c 1 $host_repeater &> /dev/null
then
echo -e "${GREEN} Repeater is available${NC} gives answer on ip $host_repeater \n"
else
echo -e "${RED} Error!!!! Repeater is not available, check ip adress of host_repeater or leave empty is you dont have one${NC} "
exit 1
fi
fi

#Get status domoticz
status_domoticz_device1=$(curl 'http://'$host_domoticz'/json.htm?type=devices&rid='$idxdevice1 | jq -r [.result][][].Data)
status_domoticz_device2=$(curl 'http://'$host_domoticz'/json.htm?type=devices&rid='$idxdevice2 | jq -r [.result][][].Data)
else
status_domoticz_device1=$(curl -s 'http://'$host_domoticz'/json.htm?type=devices&rid='$idxdevice1 | jq -r [.result][][].Data)
status_domoticz_device2=$(curl -s 'http://'$host_domoticz'/json.htm?type=devices&rid='$idxdevice2 | jq -r [.result][][].Data)
fi

#get device status of devices in Router
echo "getting output router.. plz wait... " ;
output_device1=$(python $cwd/fritzhosts.py -i $host_fritzbox -p $password_fritzbox -d $mac_device1)
sleep 2;
output_device2=$(python $cwd/fritzhosts.py -i $host_fritzbox -p $password_fritzbox -d $mac_device2);
sleep 2;
status_router_device1=$(grep "NewActive : 1" <<<"$output_device1")
status_router_device2=$(grep "NewActive : 1" <<<"$output_device2")


#get device status of repeater
if [ -n "$host_repeater" ]; then
echo "getting output repeater..plz wait..." ;

output_extender_device1=$(python $cwd/fritzhosts.py -i $host_repeater -p $password_fritzbox -d $mac_device1)
sleep 2;
output_extender_device2=$(python $cwd/fritzhosts.py -i $host_repeater -p $password_fritzbox -d $mac_device2)
sleep 2;
status_extender_device1=$(grep "NewActive : 1" <<<"$output_extender_device1")
status_extender_device2=$(grep "NewActive : 1" <<<"$output_extender_device2")


if [ -z "$status_router_device1" ] && [ -z "$status_extender_device1" ]; then
# device is not active in router and not active in repeater so set it to off
status_router_device1="Off"
else
#device is active so we set variable to on

status_router_device1="On"
fi

if [ -z "$status_router_device2" ] && [ -z "$status_extender_device2" ]; then
# device is not active in router and not active in repeater so set it to off
status_router_device2="Off"
else
#device is active so we set variable to on

status_router_device2="On"
fi

#else if there is no repeater
else
if [ -z "$status_router_device1" ];then
# device is not active in router and not active in repeater so set it to off
status_router_device1="Off"
else
#device is active so we set variable to on

status_router_device1="On"
fi

if [ -z "$status_router_device2" ];then
# device is not active in router and not active in repeater so set it to off
status_router_device2="Off"
else
#device is active so we set variable to on

status_router_device2="On"
fi
fi



if [ "$enable_debug" == "true" ]; then
echo "output router $name_device1 = $output_device1"
echo "output router $name_device2 = $output_device2"
echo "-----------------------------------------------------"
if [ -n "$host_repeater" ]; then
echo "output repeater $name_device1 = $output_extender_device1"
echo "output repeater $name_device2 = $output_extender_device2"
echo "-----------------------------------------------------"
echo "status repeater $name_device1 = $status_extender_device1"
echo "status repeater $name_device2 = $status_extender_device2"
fi
echo "status router $name_device1 = $status_router_device1"
echo "status router $name_device2 = $status_router_device2"
echo "status in domoticz $name_device1 = $status_domoticz_device1"
echo "status in domoticz $name_device2 = $status_domoticz_device2"
fi




#now we compare the status of the router vs the status of domoticz

if [ "$status_router_device1" == "$status_domoticz_device1" ]; then
# both are simular so there is nothing to change.
echo $(date -u) "status router and domoticz for $name_device1 are simular, we do nothing"
else
#router status vs domoticz status are not equal we set domoticz status to router status.
#we change the value in domoticz
echo -e $(date -u)"status router is not simular to status domoticz. ${GREEN} We change status domoticz for $name_device1 to $status_router_device1 ${NC}"
wget -q --delete-after "http://$host_domoticz/json.htm?type=command&param=switchlight&idx=$idxdevice1&switchcmd=$status_router_device1" >/dev/null 2>&1

#we send logging information to domoticz
wget -q --delete-after "http://$host_domoticz/json.htm?type=command&param=addlogmessage&message=presence-detection-logging $name_device1 = $status_router_device1" >/dev/null 2>&1

fi


if [ "$status_router_device2" == "$status_domoticz_device2" ]; then
# both are simular so there is nothing to change.
echo $(date -u) "status router and domoticz for $name_device2 are simular, we do nothing"
else
#router status vs domoticz status are not equal we set domoticz status to router status.
echo -e $(date -u) "status router is not simular to status domoticz. ${GREEN} We change status domoticz for $name_device2 to $status_router_device2 ${NC}"
#we change the value in domoticz
wget -q --delete-after "http://$host_domoticz/json.htm?type=command&param=switchlight&idx=$idxdevice2&switchcmd=$status_router_device2" >/dev/null 2>&1

#we send logging information to domoticz
wget -q --delete-after "http://$host_domoticz/json.htm?type=command&param=addlogmessage&message=presence-detection-logging $name_device2 = $status_router_device2" >/dev/null 2>&1

fi
Kubra
Posts: 22
Joined: Wednesday 01 April 2015 22:06
Target OS: NAS (Synology & others)
Domoticz version: latest
Location: Hollanda
Contact:

Re: Presence detection for Fritzbox script v1.0: using TR064 protocol communication to your router

Post by Kubra »

Tested and working fine on a Synology DSM 6.2. You need Python3 to run this.
Have used the presence detection from Chopper_Rob quite a long time but this works way better. Thanks.

PS. I'm running this script for 4 devices now.
Last edited by Kubra on Saturday 23 February 2019 17:20, edited 1 time in total.
funky
Posts: 74
Joined: Saturday 06 May 2017 22:38
Target OS: Raspberry Pi / ODroid
Domoticz version: newest
Location: Zeist, Netherlands
Contact:

Re: Presence detection for Fritzbox script v1.0: using TR064 protocol communication to your router

Post by funky »

Thnx. Now working on easy install script hope to finish it this weekend.
gerbenvanasselt
Posts: 74
Joined: Friday 07 September 2018 14:46
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta 4.1
Location: Netherlands
Contact:

Re: Presence detection for Fritzbox script v1.0: using TR064 protocol communication to your router

Post by gerbenvanasselt »

@funky, please keep is informed about you're progress. I want to install it on my main pi.

Verstuurd vanaf mijn ONEPLUS A6003 met Tapatalk

gerbenvanasselt
Posts: 74
Joined: Friday 07 September 2018 14:46
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta 4.1
Location: Netherlands
Contact:

Re: Presence detection for Fritzbox script v1.0: using TR064 protocol communication to your router

Post by gerbenvanasselt »

I've worked out the problem with the error message i got when bash-ing presence_detection.sh. I changed the domoticz gateway to 127.0.0.1:8080 and after that the error was gone. Now the presence detection works on my second machine as well.

Since week or 2 my Pi 3b+ is stalling (loopt vast). Are you experiencing the same? It would be handy that we can exclude the script as the source for the stalling?

When are you finishing you're next version of the plugin?
funky
Posts: 74
Joined: Saturday 06 May 2017 22:38
Target OS: Raspberry Pi / ODroid
Domoticz version: newest
Location: Zeist, Netherlands
Contact:

Re: Presence detection for Fritzbox script v1.0: using TR064 protocol communication to your router

Post by funky »

I dont have problems with stalling. I hope 2 finish the script next week
funky
Posts: 74
Joined: Saturday 06 May 2017 22:38
Target OS: Raspberry Pi / ODroid
Domoticz version: newest
Location: Zeist, Netherlands
Contact:

Re: Presence detection for Fritzbox script v1.0: using TR064 protocol communication to your router

Post by funky »

New version has been uploaded.

Changed 2.0 - 23 februari 2019:
1. Easy installer. Now there is no need to edit script files
2. Add a lot of checks to make the script more reliable.
3. Now its possible to add as many devices as you want.

Download: git clone https://github.com/hydex80/presence_det ... z_fritzbox

First time installation:
Run: sudo bash presence_detection.sh

If you want to reinstall the script run:
sudo bash presence_detection.sh install

for debugging:
sudo bash presence_detection.sh debug

If you are running a previous version, plz overwrite files and reinstall. Sorry it's not downwards compatible.
VHK17
Posts: 11
Joined: Tuesday 31 January 2017 9:10
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Presence detection for Fritzbox script v2.0: using TR064 protocol communication to your router

Post by VHK17 »

The script is reading data from the Fritzbox but does not read / write to Domoticz ?
Naamloos.jpg
Naamloos.jpg (68.32 KiB) Viewed 3502 times
funky
Posts: 74
Joined: Saturday 06 May 2017 22:38
Target OS: Raspberry Pi / ODroid
Domoticz version: newest
Location: Zeist, Netherlands
Contact:

Re: Presence detection for Fritzbox script v2.0: using TR064 protocol communication to your router

Post by funky »

It is also writing to domoticz
funky
Posts: 74
Joined: Saturday 06 May 2017 22:38
Target OS: Raspberry Pi / ODroid
Domoticz version: newest
Location: Zeist, Netherlands
Contact:

Re: Presence detection for Fritzbox script v2.0: using TR064 protocol communication to your router

Post by funky »

Are you sure the idx and the ip /host of domoticz is correct in the settings?
VHK17
Posts: 11
Joined: Tuesday 31 January 2017 9:10
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Presence detection for Fritzbox script v2.0: using TR064 protocol communication to your router

Post by VHK17 »

Hello,

I have the settings ;
1.jpg
1.jpg (37.75 KiB) Viewed 3487 times
2.jpg
2.jpg (13.2 KiB) Viewed 3487 times
3.jpg
3.jpg (5.48 KiB) Viewed 3487 times
gerbenvanasselt
Posts: 74
Joined: Friday 07 September 2018 14:46
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta 4.1
Location: Netherlands
Contact:

Re: Presence detection for Fritzbox script v2.0: using TR064 protocol communication to your router

Post by gerbenvanasselt »

VHK17 wrote:Hello,

I have the settings ;
1.jpg
2.jpg
3.jpg
I had the same problem last week. I used 127.0.0.1:8080 instead of domoticz ip. And after that it worked. The domoticz widgets received an update.

Verstuurd vanaf mijn ONEPLUS A6003 met Tapatalk

User avatar
felix63
Posts: 244
Joined: Monday 07 December 2015 9:30
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.1
Location: Gouda
Contact:

Re: Presence detection for Fritzbox script v2.0: using TR064 protocol communication to your router

Post by felix63 »

After downloading when trying to install I get:

Code: Select all

parse error: Invalid numeric literal at line 1, column 60
there is a problem adding the virtual hardware to domoticz. 
Also I have the impression the checks for dependencies don't work properly.
Prutsium
Posts: 92
Joined: Monday 18 June 2018 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Germany / Netherlands
Contact:

Re: Presence detection for Fritzbox script v2.0: using TR064 protocol communication to your router

Post by Prutsium »

Hmmmm get an error ... when i install the script devices are made and when i turn them manual on they will be turned off so far so good.
But one of the devices is actually there ....... but wont go offline.

Fritzbox has been setup for TRS so that seems to work.

Code: Select all


Traceback (most recent call last):
  File "/home/media/presence_detection_domoticz_fritzbox/fritzhosts.py", line 25                , in <module>
    import fritzconnection
  File "/home/media/presence_detection_domoticz_fritzbox/fritzconnection.py", li                ne 36, in <module>
    import requests
ImportError: No module named requests
Do 14. Mär 16:46:51 UTC 2019 status router and domoticz for IPAD_JG are simular,                 we do nothing
debug information:
status domoticz:
Off
config:

Code: Select all

ip_fritzbox=192.168.98.1
pass_fritzbox=****
ip_repeater=
ip_domoticz=127.0.0.1:8080
device_names=(OPO6T_JG IPAD_JG)
device_macs=(64:A2:F9:F2:57:E5 88:E9:FE:45:1E:F6)
device_idx=(2 3)
Any idea?
funky
Posts: 74
Joined: Saturday 06 May 2017 22:38
Target OS: Raspberry Pi / ODroid
Domoticz version: newest
Location: Zeist, Netherlands
Contact:

Re: Presence detection for Fritzbox script v2.0: using TR064 protocol communication to your router

Post by funky »

@felix63 and @prutsium. You are missing dependencies.

Indeed the script check of dependencies is not working, im now working on it to fix this.

Can you run the following command and report if something is installed? If it asked to install confirm with yes and reboot and re-run the script again

Code: Select all

 sudo apt-get install python jq python-lxml python-requests 
funky
Posts: 74
Joined: Saturday 06 May 2017 22:38
Target OS: Raspberry Pi / ODroid
Domoticz version: newest
Location: Zeist, Netherlands
Contact:

Re: Presence detection for Fritzbox script v2.0: using TR064 protocol communication to your router

Post by funky »

I updated the script to 2.2 and fixed the check of dependencies

can you help me to test the script?:

1. if you didn't first delete old dummy device: go to domoticz > settings > hardware > delete dummy device presence_detection
2. go to ssh
make a new directory for example test. move over to this directory
and run: sudo git clone https://github.com/hydex80/presence_det ... z_fritzbox
then reinstall the script with: sudo bash presence_detection.sh

It should be working now. If not please let me know.
User avatar
felix63
Posts: 244
Joined: Monday 07 December 2015 9:30
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.1
Location: Gouda
Contact:

Re: Presence detection for Fritzbox script v2.0: using TR064 protocol communication to your router

Post by felix63 »

Code: Select all

sudo apt-get install python jq python-lxml python-requests 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
jq is already the newest version (1.5+dfsg-1.3).
python is already the newest version (2.7.13-2).
python-requests is already the newest version (2.12.4-1).
The following additional packages will be installed:
  libxslt1.1 python-bs4 python-html5lib python-webencodings
Suggested packages:
  python-genshi python-lxml-dbg python-lxml-doc
The following NEW packages will be installed:
  libxslt1.1 python-bs4 python-html5lib python-lxml python-webencodings
0 upgraded, 5 newly installed, 0 to remove and 0 not upgraded.
Need to get 1,193 kB of archives.
After this operation, 4,561 kB of additional disk space will be used.
and some further fiddling with permissions to get things running.
funky
Posts: 74
Joined: Saturday 06 May 2017 22:38
Target OS: Raspberry Pi / ODroid
Domoticz version: newest
Location: Zeist, Netherlands
Contact:

Re: Presence detection for Fritzbox script v2.0: using TR064 protocol communication to your router

Post by funky »

felix63 wrote: Sunday 17 March 2019 21:51

Code: Select all

sudo apt-get install python jq python-lxml python-requests 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
jq is already the newest version (1.5+dfsg-1.3).
python is already the newest version (2.7.13-2).
python-requests is already the newest version (2.12.4-1).
The following additional packages will be installed:
  libxslt1.1 python-bs4 python-html5lib python-webencodings
Suggested packages:
  python-genshi python-lxml-dbg python-lxml-doc
The following NEW packages will be installed:
  libxslt1.1 python-bs4 python-html5lib python-lxml python-webencodings
0 upgraded, 5 newly installed, 0 to remove and 0 not upgraded.
Need to get 1,193 kB of archives.
After this operation, 4,561 kB of additional disk space will be used.
and some further fiddling with permissions to get things running.
Hi Felix send you a PM
User avatar
felix63
Posts: 244
Joined: Monday 07 December 2015 9:30
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.1
Location: Gouda
Contact:

Re: Presence detection for Fritzbox script v2.0: using TR064 protocol communication to your router

Post by felix63 »

Got it working.

Tip: when you run configuration it asks for IP of fritz.box displaying a default address. I took this to mean that pressing enter would accept the default value (192.168.178.1) but it actually gave me an empty line and caused the configuration to fail.
Prutsium
Posts: 92
Joined: Monday 18 June 2018 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Germany / Netherlands
Contact:

Re: Presence detection for Fritzbox script v2.0: using TR064 protocol communication to your router

Post by Prutsium »

funky wrote: Saturday 16 March 2019 11:14 @felix63 and @prutsium. You are missing dependencies.

Indeed the script check of dependencies is not working, im now working on it to fix this.

Can you run the following command and report if something is installed? If it asked to install confirm with yes and reboot and re-run the script again

Code: Select all

 sudo apt-get install python jq python-lxml python-requests 
Hi Funky,

Thanks got the script running now and run directly into two issues:
1: Every time i run the script it comes back to the installer:

Code: Select all

media@media-N61Vg:~/presence_detection_domoticz_fritzbox$ bash presence_detection.sh
checking:IPAD_JG
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1895  100  1895    0     0  1850k      0 --:--:-- --:--:-- --:--:-- 1850k
Mo 18. Mär 07:56:25 UTC 2019 status router and domoticz for IPAD_JG are simular, we do nothing
checking:OP6T_JG
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1901  100  1901    0     0  1856k      0 --:--:-- --:--:-- --:--:-- 1856k
Mo 18. Mär 07:56:31 UTC 2019 status router and domoticz for OP6T_JG are simular, we do nothing

Enter ip adres of fritzbox default:192.168.178.1  and press [ENTER]:
Only way to abort: CTRL-C

2: I have multiple extenders in my setup (2 actually) would it be possible to create a option for multiple extenders by just simply adding multiple IP's on the same line ?

Thanks for your help!
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests