Presence detection using UniFi Controller (Ubiquiti Networks)

All kinds of 'OS' scripts

Moderator: leecollings

mcmikev
Posts: 146
Joined: Tuesday 26 May 2015 8:11
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: right here
Contact:

Re: Presence detection using UniFi Controller (Ubiquiti Networks)

Post 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
mcmikev
Posts: 146
Joined: Tuesday 26 May 2015 8:11
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: right here
Contact:

Re: Presence detection using UniFi Controller (Ubiquiti Networks)

Post 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!
prekz
Posts: 3
Joined: Sunday 13 August 2017 22:20
Target OS: Linux
Domoticz version:
Contact:

Re: Presence detection using UniFi Controller (Ubiquiti Networks)

Post 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?
rasper
Posts: 21
Joined: Sunday 10 January 2016 22:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.2
Location: Netherlands
Contact:

Re: Presence detection using UniFi Controller (Ubiquiti Networks)

Post 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)
Falken
Posts: 6
Joined: Thursday 22 February 2018 21:20
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8934
Contact:

Re: Presence detection using UniFi Controller (Ubiquiti Networks)

Post 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?
timop
Posts: 57
Joined: Sunday 03 January 2016 17:10
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Presence detection using UniFi Controller (Ubiquiti Networks)

Post 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
timop
Posts: 57
Joined: Sunday 03 January 2016 17:10
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Presence detection using UniFi Controller (Ubiquiti Networks)

Post 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"

Maes
Posts: 34
Joined: Tuesday 05 April 2016 20:45
Target OS: -
Domoticz version:
Contact:

Re: Presence detection using UniFi Controller (Ubiquiti Networks)

Post 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?
Luuc_a
Posts: 24
Joined: Tuesday 16 July 2013 10:12
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Contact:

Re: Presence detection using UniFi Controller (Ubiquiti Networks)

Post 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
denver
Posts: 9
Joined: Sunday 17 December 2017 7:19
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Presence detection using UniFi Controller (Ubiquiti Networks)

Post 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..
CronoS
Posts: 135
Joined: Wednesday 15 July 2015 23:40
Target OS: -
Domoticz version:
Contact:

Re: Presence detection using UniFi Controller (Ubiquiti Networks)

Post 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 ?
credenty
Posts: 16
Joined: Saturday 12 November 2016 11:07
Target OS: Windows
Domoticz version:
Contact:

Re: Presence detection using UniFi Controller (Ubiquiti Networks)

Post 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?
Delfuego
Posts: 32
Joined: Sunday 29 July 2018 16:18
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: The Netherlands
Contact:

Re: Presence detection using UniFi Controller (Ubiquiti Networks)

Post 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!
cmartens75
Posts: 15
Joined: Monday 23 April 2018 10:43
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Netherlands
Contact:

Re: Presence detection using UniFi Controller (Ubiquiti Networks)

Post 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.
RapTile
Posts: 53
Joined: Saturday 10 March 2018 2:01
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Presence detection using UniFi Controller (Ubiquiti Networks)

Post 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`
Last edited by RapTile on Wednesday 17 October 2018 23:06, edited 1 time in total.
RapTile
Posts: 53
Joined: Saturday 10 March 2018 2:01
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Presence detection using UniFi Controller (Ubiquiti Networks)

Post 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.
cmartens75
Posts: 15
Joined: Monday 23 April 2018 10:43
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Netherlands
Contact:

Re: Presence detection using UniFi Controller (Ubiquiti Networks)

Post 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!
RapTile
Posts: 53
Joined: Saturday 10 March 2018 2:01
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Presence detection using UniFi Controller (Ubiquiti Networks)

Post by RapTile »

cmartens75 wrote: Sunday 28 October 2018 21:44
Thanks for the updates! Works like a charm!
U’r welcome :-)
Maes
Posts: 34
Joined: Tuesday 05 April 2016 20:45
Target OS: -
Domoticz version:
Contact:

Re: Presence detection using UniFi Controller (Ubiquiti Networks)

Post 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 ?
Maes
Posts: 34
Joined: Tuesday 05 April 2016 20:45
Target OS: -
Domoticz version:
Contact:

Re: Presence detection using UniFi Controller (Ubiquiti Networks)

Post by Maes »

Okay, it works. I don't know how this jq works but it seems to utilize black magic.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 1 guest