Page 2 of 3

Re: Presence detection using UniFi Controller (Ubiquiti Networks)

Posted: Sunday 14 January 2018 15:34
by mcmikev
Hi BakSeeDaa,

I am also having problems with the scripts now.
I run it on a seperate RPi2 and used to work but now with (i guess because of that it stopped working) cloudkey firmware version 0.8.9 it does not work anymore. \

Code: Select all

pi@rpi2:~/domoticz/scripts/unifi-detect $ ./unifi_detect.sh
Running unifi_detect.sh 1.0.1
Exiting with a result code of : 401
pi@rpi2:~/domoticz/scripts/unifi-detect $
I have looked at the url, port and credentials and they are fine.

Do you have any info regarding the working status with CK 0.8.9 version and Unifi version 5.7.15.0

Re: Presence detection using UniFi Controller (Ubiquiti Networks)

Posted: Sunday 14 January 2018 15:57
by mcmikev
Update:

It does work!

My unifi password was not right anymore. CloudKey and unifi had different passwords (due to reset I had to do a while back)
I don't have an issue now :-)

Sorry but also thanks!

Re: Presence detection using UniFi Controller (Ubiquiti Networks)

Posted: Monday 05 February 2018 21:52
by prekz
When i run the script (without crontab enabled/configured) i get the following message:

Code: Select all

Running unifi_detect.sh 1.0.1
There is no output so i don't know if the script is running. As Cellphones i used the name that is known in the unifi controller (android-macadress for example). I don't see my virtual switches turned on or off when the script runs (also when i run it under crontab i don't see them changing).

Can anyone help?

EDIT: after an hour i fixed the problem, turns out the unifi second site had a # in front..

Other question, how long does it take to turn the signal "off" i have the idea it takes a couple of minutes when the cellphone leaves the unifi controler (and turns the virtual switch off in domoticz) is there a way to speed up this process?

Re: Presence detection using UniFi Controller (Ubiquiti Networks)

Posted: Wednesday 07 February 2018 13:36
by rasper
prekz wrote: Monday 05 February 2018 21:52 When i run the script (without crontab enabled/configured) i get the following message:

Code: Select all

Running unifi_detect.sh 1.0.1
There is no output so i don't know if the script is running. As Cellphones i used the name that is known in the unifi controller (android-macadress for example). I don't see my virtual switches turned on or off when the script runs (also when i run it under crontab i don't see them changing).

Can anyone help?

EDIT: after an hour i fixed the problem, turns out the unifi second site had a # in front..

Other question, how long does it take to turn the signal "off" i have the idea it takes a couple of minutes when the cellphone leaves the unifi controler (and turns the virtual switch off in domoticz) is there a way to speed up this process?
it takes 10 minutes for the wifi client list in the unifi to get updated if a device disconnects, I also sometimes have random disconnects while the device is connected to wifi, but it then doesnt show up in the list of connected clients. for that i added an additional bluetooth ping as a safety net (but not my whole house has bluetooth reach so sometimes still a false message)

Re: Presence detection using UniFi Controller (Ubiquiti Networks)

Posted: Saturday 24 February 2018 14:30
by Falken
I get: chmod: cannot access '/home/pi/domoticz/scripts/unifi-detect/unifi_detect.sh': No such file or directory


But the file is there, what am I doing wrong?

Re: Presence detection using UniFi Controller (Ubiquiti Networks)

Posted: Tuesday 27 February 2018 15:17
by timop
@Falken: file isnt there ls -l /home/pi/domoticz/scripts/unifi-detect

is it possible to check from arp in/from Unifi controller, like in asus APs, it works immediately when fevice is off from wifi
viewtopic.php?f=61&t=15531

Re: Presence detection using UniFi Controller (Ubiquiti Networks)

Posted: Tuesday 27 February 2018 15:35
by timop
I added little feature, my devices are password protected and I added to script couple things:

Code: Select all

domoticz_ip=192.168.XXX.XXX # Domoticz IP or host name
domoticz_port=8080
dompasscode=YourOwnSwitchPassCode 

domoState=$(curl --silent "http://$domoticz_ip:$domoticz_port/json.htm?type=devices&rid=$1&passcode=$dompasscode")
	
curl --silent --output /dev/null "http://$domoticz_ip:$domoticz_port/json.htm?type=command&param=switchlight&idx=$1&switchcmd=$2&passcode=$dompasscode"


Re: Presence detection using UniFi Controller (Ubiquiti Networks)

Posted: Sunday 11 March 2018 21:27
by Maes
Hi BakSeeDa,

Hoping you can help me out here. I've been using your script without any problems for several months already. However, since a couple of days the script doesn't recognise my phone leaving or entering the house anymore. The stupid thing is that it's still working perfectly for my wife's phone.

I double checked the device ID and the unifi client name several times but nothing has changed. The only things that have changed is that I updated the unifi cloud key and access points and my phone (galaxy s8) has been updated to android 8. I don't know if this matters at all because I still see my phone being connected in the unifi client list under the same client name.

I also noticed that when I run the script manually, it DOES switch off/on for my phone, but not when it's running under Cron...

Any idea what this could be?

Re: Presence detection using UniFi Controller (Ubiquiti Networks)

Posted: Thursday 22 March 2018 11:44
by Luuc_a
Maes wrote: Sunday 11 March 2018 21:27 Hi BakSeeDa,

Hoping you can help me out here. I've been using your script without any problems for several months already. However, since a couple of days the script doesn't recognise my phone leaving or entering the house anymore. The stupid thing is that it's still working perfectly for my wife's phone.

I double checked the device ID and the unifi client name several times but nothing has changed. The only things that have changed is that I updated the unifi cloud key and access points and my phone (galaxy s8) has been updated to android 8. I don't know if this matters at all because I still see my phone being connected in the unifi client list under the same client name.

I also noticed that when I run the script manually, it DOES switch off/on for my phone, but not when it's running under Cron...

Any idea what this could be?
Today I started with this script because I have a Unifi Controller to and all works fine. First I had the same issue like you en with the following adjustment it works also with cron.

Edit the first rule of the script from:

Code: Select all

#!/usr/bin/env bash
to

Code: Select all

#!/bin/bash
Maybe it will works for you to

Re: Presence detection using UniFi Controller (Ubiquiti Networks)

Posted: Sunday 25 March 2018 9:34
by denver
Not sure how to put

Code: Select all

*/5 * * * * /home/pi/domoticz/scripts/unifi-detect/unifi_detect.sh
in crontab, any help appreciated, thanks..

Re: Presence detection using UniFi Controller (Ubiquiti Networks)

Posted: Wednesday 11 April 2018 12:56
by CronoS
I am running this script on a Synology with Domoticz, where my Unifi controller is in Azure. I can successfully get the content; but the script seems to hang, it only sometimes flip the switch and the script doesn't seems to stop. . Anyone knows what to expect on a Synology ?

Re: Presence detection using UniFi Controller (Ubiquiti Networks)

Posted: Wednesday 18 April 2018 7:40
by credenty
Hello, I do not know if there will be more people like me. Would there be any possibility of doing a scrip for windows?

Re: Presence detection using UniFi Controller (Ubiquiti Networks)

Posted: Friday 10 August 2018 12:31
by Delfuego
credenty wrote: Wednesday 18 April 2018 7:40 Hello, I do not know if there will be more people like me. Would there be any possibility of doing a scrip for windows?
Maybe my DzVents script runs on Windows!? Does not depend on Curl.
viewtopic.php?f=59&t=24453
Let me know if it works on Windows!

Re: Presence detection using UniFi Controller (Ubiquiti Networks)

Posted: Wednesday 05 September 2018 21:47
by cmartens75
Hi all, the script is working fine for multiple months already. However, I would like to have the passwords removed from the script. Is this possible? If yes, how can this be done? Thanks in advance for your reaction and ideas.

Re: Presence detection using UniFi Controller (Ubiquiti Networks)

Posted: Wednesday 17 October 2018 22:53
by RapTile
i changed two things in this awesome script for the following reasons.

First: Because it takes a long time (20+ minutes) before the wifi connected devices get removed from the "clients" list due to a known bug (wifi devices that disconnect, will first be moved to the LAN connected devices before they get removed from the list +- 20 minutes later) i made sure an extra check is build in that checks if the device is wired or not.
If it is not wired, the device is on the WiFi
If it's not in the list or marked as a wired correction it means that the device isn't on WiFi

Code: Select all

        for i in "${!cellPhones[@]}"; do
                onWifi=$(echo "$var" | head -n 1 | jq -r '.data[] | select(.hostname=='\"$i\"') and select(.is_wired==false)')
                if [ ! -z "$onWifi" ]; then
                        newState="On";
                else
                        newState="Off";
                fi
                if [ "$newState" != "${lastStates[$i]}" ]; then
                        lastStates[$i]="$newState"
                        switchDevice "${cellPhones[$i]}" "$newState";
                fi
        done
In order to get this to work you need to install "jq"

Code: Select all

sudo apt-get install jq

Second: I don't want my password to be in this script so i created a password file that only root can access.

Code: Select all

chown root:root unifi_passwd.txt
followed by

Code: Select all

 chmod 600 unifi_passwd.txt
In the script i cat the password file because the script started as user root ( sudo crontab -e )
Off course this unifi_passwd.txt file contains my unifi password

Code: Select all

unifi_password=`cat /home/pi/domoticz/scripts/unifi-detect/unifi_passwd.txt`

Re: Presence detection using UniFi Controller (Ubiquiti Networks)

Posted: Wednesday 17 October 2018 23:04
by RapTile
cmartens75 wrote: Wednesday 05 September 2018 21:47 Hi all, the script is working fine for multiple months already. However, I would like to have the passwords removed from the script. Is this possible? If yes, how can this be done? Thanks in advance for your reaction and ideas.
See my resonse above.

Re: Presence detection using UniFi Controller (Ubiquiti Networks)

Posted: Sunday 28 October 2018 21:44
by cmartens75
RapTile wrote: Wednesday 17 October 2018 23:04
cmartens75 wrote: Wednesday 05 September 2018 21:47 Hi all, the script is working fine for multiple months already. However, I would like to have the passwords removed from the script. Is this possible? If yes, how can this be done? Thanks in advance for your reaction and ideas.
See my resonse above.
Thanks for the updates! Works like a charm!

Re: Presence detection using UniFi Controller (Ubiquiti Networks)

Posted: Monday 29 October 2018 8:25
by RapTile
cmartens75 wrote: Sunday 28 October 2018 21:44
Thanks for the updates! Works like a charm!
U’r welcome :-)

Re: Presence detection using UniFi Controller (Ubiquiti Networks)

Posted: Monday 29 October 2018 8:47
by Maes
RapTile wrote: Monday 29 October 2018 8:25
cmartens75 wrote: Sunday 28 October 2018 21:44
Thanks for the updates! Works like a charm!
U’r welcome :-)
Hi RapTile,

Nice update, I'm also using it. However, you seem to be looking at the host names of the devices rather than the normal names (alias from unifi).
These host names are not unique.

Can I simply change this code ?
select(.hostname=='\"$i\"')

to

select(.name=='\"$i\"') ?

Or will this not work because the is_wired parameter is BEFORE the name and AFTER the hostname parameter in the unifi data ?

Re: Presence detection using UniFi Controller (Ubiquiti Networks)

Posted: Monday 29 October 2018 9:13
by Maes
Okay, it works. I don't know how this jq works but it seems to utilize black magic.