SmartThings Community

Wi-Fi Presence App?


(MacTechGenius) #1

Hi,

I have a Mac that runs 24/7 is there any Wi-Fi presence solution that I can run?? I am also using the UniFi Controller for my AP’s…any integration with SmartThing’s with that?


#2

Several people have done Wi-Fi presence solutions, but it’s going to depend on the exact router that you have. See the discussions in the presence FAQ :sunglasses:


(MacTechGenius) #3

I don’t have an Asus router…still using an AirPort Extreme. Anything I can run on my Mac?

I am looking into this but need help setting it up: https://github.com/idolpx/WhoIsHome


(Mark) #4

I think airport routers tend to be pretty locked down in terms of scripts and stuff that you can run on them.


(MacTechGenius) #5

True! But I plan to switch to ubiquity. Already have some AP’s and use there controller

Also I have a Mac that stays in 24/7 so I can run something on it


(Andreas A.) #6

I am running

`/usr/sbin/arp -a | grep -c -E '00:11:22:33:44:55|66:77:88:99:AA:BB'`

on my Mac (once a minute using cron), obviously with the correct MAC IDs I care about. This gives the number of devices present. Based on the result I then call curl to toggle a virtual presence sensor.

This is what I use to detect if any of my known “Guest” devices are present.


(MacTechGenius) #7

It would be much much appreciated if you can provide some instructions on how to do this!!! Should I just paste this script on terminal???

Thanks!!!


(Andreas A.) #8

I’m using this SmartApp (make sure to enable OAuth and publish to yourself):

definition(
    name: "Wifi Presence",
    namespace: "aamann",
    author: "Andreas Amann",
    description: "Triggers Wifi Presence Status when HTTP GET Request is received",
    category: "My Apps",
    iconUrl: "https://www.dropbox.com/s/9vrx701fhs8h9zh/WiFi_Presence.png?raw=1",
    iconX2Url: "https://www.dropbox.com/s/d7ql8b06shddfeh/WiFi_Presence%402x.png?raw=1",
    iconX3Url: "https://www.dropbox.com/s/zxkiyx14drakfbu/WiFi_Presence%403x.png?raw=1")


preferences {
    section(title: "Select Presence Device") {
        input "virtualPresence", "capability.presenceSensor", title: "Select Virtual Presence Sensor", required: true, multiple:false
    }
}

def installed() {
    createAccessToken()
    getToken()
    log.debug("Installed WiFi presence with REST API: $app.id")
    log.debug("Installed WiFi presence with token: $state.accessToken")
    log.debug("Installed WiFi presence for virtual sensor: $virtualPresence.name")
}
def updated() {
    log.debug("Updated WiFi presence with REST API: $app.id")
    log.debug("Updated WiFi presence with token: $state.accessToken")
}

mappings {
  path("/home") {
    action: [
      GET: "updatePresent"
    ]
  }
  path("/away") {
    action: [
      GET:"updateNotPresent"
    ]
  }
}

// Callback functions
def getSwitch() {
    // This returns the current state of the Presence sensor in JSON
    return virtualPresence.currentState("presence")
}

def updatePresent() {
    log.debug("A guest has arrived")
    virtualPresence.arrived();
}

def updateNotPresent() {
    log.debug("Last guest has departed")
    virtualPresence.departed();
}

def getToken(){
    if (!state.accessToken) {
        try {
	        getAccessToken()
	        log.debug("Creating new Access Token: $state.accessToken")
	    } catch (ex) {
	        log.debug("Did you forget to enable OAuth in SmartApp IDE settings?")
            log.debug(ex)
	    }
    }
}

and the following script (make sure to properly fill in the redacted parts):

#!/bin/bash
appID="[READACTED]"
accessToken="[REDACTED]"
lastCount=0
if [ -e "/Users/[REDACTED]/.ST_WiFi_Presence" ];then
  lastCount=$(< "/Users/[REDACTED]/.ST_WiFi_Presence")
fi
currentCount=`/usr/sbin/arp -a | grep -c -E '[REDACTED]|[REDACTED]'`
if [ $currentCount -eq $lastCount ];then
  : # WiFi presence count is unchanged
else
  if [ $currentCount -gt 0 ] && [ $lastCount -gt 0 ];then
    : # no need to change since guest occupancy did not change
  elif [ $currentCount -gt 0 ];then
    curl "https://graph.api.smartthings.com/api/smartapps/installations/$appID/home?access_token=$accessToken" -k
  else
    curl "https://graph.api.smartthings.com/api/smartapps/installations/$appID/away?access_token=$accessToken" -k
  fi
  echo "$currentCount" > "/Users/[REDACTED]/.ST_WiFi_Presence"
  touch "/Users/[REDACTED]/.ST_WiFi_Presence"
fi

which I saved as ST_WiFi_Presence into ~ on my 24/7 machine.

Now run crontab -e and add the following line:

*/1  *  *  *  *  /Users/[REDACTED]/ST_WiFi_Presence > /dev/null 2>&1

then save and exit. This will run the script once every minute.

You can install multiple versions of the Smart App and link to multiple virtual presence sensors if you need more than one.


(MacTechGenius) #9

Much Appreciated @ahndee!!

I published the SmartApp and created a Simulated Presence Sensor according to these settings:

And I created a file called “K.ST_WiFi_Presence” and inputted the OAuth Client ID into "appID"and the OAuth Client Secret into “accessToken”.

I also updated my paths accordingly and Mac Address according to the screenshot below, I only had one MAC address so I just inputed one…is that fine?

I wasn’t exactly sure how to approach the contab -e…here is a screenshot of what I did.

It’s not working right now…so it seems that I missed something.

Thanks again (:


(Andreas A.) #10

I’m starting to wonder about that subscript on your account… :wink:

You type crontab -e and then – in the tile that opens in your default editor in the Terminal – add that line. Upon saving it should kick off the script once every minute.

You probably should run the ST_WiFi_Presence script manually from the Terminal first to see whether it works (as observed by IDE logging).


(MacTechGenius) #11

So I tried just pasting:

*/1 * * * * /Users/MyUser/Documents/WIFI/K.ST_WiFi_Presence > /dev/null 2>&1

and got back

server:~ MyUser $

in Terminal.

But I didn’t really see anything in the IDE live log or the presence status of my virtual sensor update.

And how do I enter something in crontab -e? It doesn’t let me type anything…and I don’t know where to put it or how to save…sorry for all these questions…I am just totally new to all this.

Thanks

03 PM


(MacTechGenius) #12

If I just type /Users/MyUser/Documents/WIFI/K.ST_WiFi_Presence in Terminal

I get this error and it deletes everything in the file and just puts a 4 instead


(MacTechGenius) #13

Hey @ahndee, just checking in to see if you have any tips on resolving this.

Thanks so much (:


(MacTechGenius) #14

Happy Thankskiving @ahndee. If you have any free time…would love to have some help on setting this up.
Thanks!!


(Andreas A.) #15

You are using the same path for the script and the file that keeps track of the count – those should be different (the count has a leading period, the script itself doesn’t).


(MacTechGenius) #16

Do you have the code for the file that keeps track of the count?

Should I change the path to that at “lastCount” and “$currentCount” in the file?

What does this mean? “(the count has a leading period, the script itself doesn’t)”

Thanks again :slight_smile:


(Andreas A.) #17

That file is generated by the script.

The file is referenced in lines 5, 6, 19, and 20 and in all those places includes a leading period (whereas the script itself doesn’t).


(MacTechGenius) #18

Appreciate it.

I get this error when I paste the script path into terminal to run it:

It says “Illegal characters found in URL”

This is my script:

And here is the location, it did generate the file.

Thanks


(Andreas A.) #19

Sorry - copy/paste error. The two curl lines should end with

?access_token=$accessToken" -k

rather than

?access_token=$accessToken$

(Fixed the script in the original posting above)


(MacTechGenius) #20

Thanks! Made the changes!!

When I paste the path of the script into Terminal:
/Users/Siddiqi/Documents/WIFI/K.ST_WiFi_Presence
It runs without any errors. And creates the “a.ST_WiFi_Presence” that contains the value of 1.

However, my simulated presence sensor tied to the smartapp doesn’t update and report presence.

Is my MacAddress field formatted correctly? I do have only one Mac address (instead of 2 like your sample code).
currentCount=/usr/sbin/arp -a | grep -c -E 'a0:3b:e3:a3:13:22'

Here are some screenshots from the IDE: