Controlling occupancy from your router (Open firmware: Tomato/DD-WRT/OpenWRT)

[quote=“integlikewhoa, post:120, topic:180260”]I have switched out the wireless to Ubiquity Unifi Ap’s inside and outside the house. I think I thrown most if not everything in this thread out.

Any ideas?

How about back to pinging an IP useing this script vs. several PING sensor plugins in vera?

I see 2.0 was posted but is that only for a certain processor/router and I don’t see ping local machines in that script?[/quote]

The script should run on any Linux based router - given you have access to add the script to the router. I don’t know anything about Ubiquity routers.

Ping is not used in this script as is it not needed for this function. I, and others, use Ping Sensor in Vera as a secondary method… there are many threads on Ping Sensor usage for occupancy… I’ve posted several times on it.

[quote=“Aaron, post:121, topic:180260”][quote=“integlikewhoa, post:120, topic:180260”]I have switched out the wireless to Ubiquity Unifi Ap’s inside and outside the house. I think I thrown most if not everything in this thread out.

Any ideas?

How about back to pinging an IP useing this script vs. several PING sensor plugins in vera?

I see 2.0 was posted but is that only for a certain processor/router and I don’t see ping local machines in that script?[/quote]

The script should run on any Linux based router - given you have access to add the script to the router. I don’t know anything about Ubiquity routers.

Ping is not used in this script as is it not needed for this function. I, and others, use Ping Sensor in Vera as a secondary method… there are many threads on Ping Sensor usage for occupancy… I’ve posted several times on it.[/quote]

Ubiquity are not routers. They are commercial POE AP’s using their own server to track and monitor each AP and clients moving across the AP’s for zero hand off. (think Hotels, schools, ect)

Pinging a wan or LAN computer was used in the original script posted on page one and there is a few copies posted on first few pages.

The original scripts varied depending on if it was broadcom or atheros processor if you look on the first few pages of this thread modifications were needed to my script to run on other AP’s because of this.
Version 2.0 doesn’t say what its for and what it will work on.

I’m very familiar with the vera ping sensor plugin and currently modded the original script today (on page one of this thread) to only use the ping function. This will for now remove the need to have several ping sensor plugins installed in VERA and instead unload that on to my router. This also works with the 6 AP’s placed around the property rather then trying to run 7 different scripts.
I haven’t seen a way yet to install custom scripts on the Unifi system yet, but not ruling it out as it is an enterprise system it probley just has to many options that I have no clue how to use.

Can anyone who has this running on a broadcom device please post their script? i’m on a Netgear R7000 w/ Kongs DD-WRT build.
Thank you in advance.

[quote=“big517, post:123, topic:180260”]Can anyone who has this running on a broadcom device please post their script? i’m on a Netgear R7000 w/ Kongs DD-WRT build.
Thank you in advance.[/quote]

Last time I had this running on a broadcom I was using the code from Post #36 on page 3. That worked right for my Linksys e4200 v2 with a broadcom.

I finally got mine to work properly. It’s fairly accurate if you ask me. And of course the whole concept about relying on MAC addresses is great. We are essentially sniffing the area around us and only care about the devices that are relevant to what we are trying to accomplish.

So it wasn’t easy at first because even though I’m a network guy, I don’t do code nor programming. So it was the monkey-see-monkey-do approach for me. I noticed 2 issues after several hours of frustrations with the Update section dated 03/26/2015 on page 1. I use UI7 on my Vera so those instructions were super important for me:

Issue 1 - typo - not a big deal, I know but wanted to point that out for newbies like me:
Double quotation mark there: VERAURL="“http://XX.XX.XX.XX:3480/data_request?id=variableset&DeviceNum=XXserviceId=urn:dcineco-com:serviceId:MSwitch1&Variable=
(2 things: the URL above is for UI7; the DeviceNum value is the ID for the MultiSwitch listed in your Vera admin console)

Issue 2 - be aware - thanks AskewPiste for the UI7 URL:
I needed to adjust the following entries for each device: wget -qs “${VERAURL}SetStatus1&newStatus1=1” instead of wget -qs “${VERAURL}Status1&Value=1”
(the values in red above should match your environment)

Other things mentioned by others:
Yes, make sure you use Notepad++ type of program to make sure you are using the right encoding and formatting as UTF-8. You might get lucky and get away with it, but going with the regular Notepad will create chaos and frustration.

Another thing, and I feel stupid especially being a network guy: lock down that IP on your Vera controller and don’t use DHCP. Because I worked on this over several months, I never paid attention to the IP I used in the URL and guess what? My router reassigned a different IP to the Vera. Wasted some time on this so use a static IP to be safe and avoid frustration.

And of course, THANK YOU everyone else as well!!

Now that I have this running, I’m trying to get some logic work done. I have 5 MAC addresses monitored by MultiSwitch on my Vera controller. I want my motion sensors to be activated when we are all gone. I created a Scene based on my presence (my Galaxy Note 3). It works fine. When my MultiSwitch is on for my Note 3, it turns off the motion sensors. When I leave the house (or turn off wifi for testing), the motion sensors come back on. I edited that scene to add a second trigger (my Wife’s Galaxy S4) but it looks like it’s an “or” versus being an “and”. I apologize in advance if I’m not using the proper terminology. I’m sure I’m real close but my brain is stuck with this.

Without resorting to eg PLEG, you’d have to add some coding in the LUUP tab of the scenes.
The luup code should check the status of all used MultiSwitch buttons and if all are ‘off’ (No devices connected) then return TRUE and your scene will execute. If not all 5 are ‘off’ the code should return FALSE which will prevent the scene from running.

The code should query the value of StatusX (where X is 1 to 8 depending on the button) with 1 being on and 0 being off.

Thank you! I’ not familiar with anything related to the use of LUUP so let me start reading on this.

LUUP is not that hard but can be confusing. A quick and dirty (untested) script is below. Not 100% it will work as typed but should give you an idea.

Assuming device 1 is on button1 (Status1), device2 on button 2 (Status2) etc on device 25 with service ID MSwitch1.


local phone1 = luup.variable_get(“urn:dcineco-com:serviceId:MSwitch1”,“Status1”,25)
local phone2 = luup.variable_get(“urn:dcineco-com:serviceId:MSwitch1”,“Status2”,25)
local phone3 = luup.variable_get(“urn:dcineco-com:serviceId:MSwitch1”,“Status3”,25)
local phone4 = luup.variable_get(“urn:dcineco-com:serviceId:MSwitch1”,“Status4”,25)
local phone5 = luup.variable_get(“urn:dcineco-com:serviceId:MSwitch1”,“Status5”,25)

– If any phone is showing, we can assume someone is home and scene should not run
if (phone1 ==“1” or phone2 ==“1” or phone3 ==“1” or phone4 ==“1” or phone5 ==“1” ) then
return false
end

Thank you for that info! It’s been a crazy week for me and next is not any better. I will review and test things out. Then I will report back. Again, thank you SO much for all the help!

I have been using this on an Asus AC66U with succes for some time. For this I flashed DD-WRT to the router. However I have noticed that the router itself is less stable for me on DD-WRT.

I would prefer to switch to ASUSWRT which is build on the original ASUS firmware. ASUSWRT also supports execution of scripts ([url=https://github.com/RMerl/asuswrt-merlin/wiki/User-scripts]https://github.com/RMerl/asuswrt-merlin/wiki/User-scripts[/url]). However they don’s have this nice window in the GUI you can copy-paste the code in being the ‘startup script’ and the ‘custom script’.

Is the openwrt procedure mentioned in the starting post also suitable for asusWRT?

You’ll need to place the startup script and the occupancy script in the folders which they need to go, manually using SCP or SFTP. Make sure you change the perms to make them executable.

The code I created (post #1) is actually self documenting as to where they go and what to name them)

Here’s a script I whipped up based on what I read in this thread which seems to work well on my recently updated OpenWRT router with two wireless radios.

[code]#!/bin/sh

MACS=“ <…>”
BASE_URL=“http://192.168.3.100:3480/data_request
QUERY=“id=lu_action&DeviceNum=26&serviceId=urn:dcineco-com:serviceId:MSwitch1”
TIMEOUT_SECONDS=60

n=0
for mac in $MACS; do
n=$((n+1))
eval count_$n=$TIMEOUT_SECONDS
done

reset=0
while true; do
reset=$((reset-1))
if [ $reset -le 0 ]; then
n=0
for mac in $MACS; do
n=$((n+1))
eval state_$n=-1
done
reset=$TIMEOUT_SECONDS
fi

n=0
for mac in $MACS; do
    n=$((n+1))
    eval state=\$state_$n
    if iw dev wlan0 station get $mac > /dev/null 2>&1 || iw dev wlan1 station get $mac > /dev/null 2>&1; then
        if [ $state -ne 1 ]; then
            echo "Setting $n ($mac) to 1"
            url="${BASE_URL}?${QUERY}&action=SetStatus${n}&newStatus${n}=1"
            wget -O - $url > /dev/null 2> /dev/null && eval state_$n=1
            eval count_$n=$TIMEOUT_SECONDS
        fi
    else
        eval count=\$count_$n
        if [ $count -le 0 ]; then
            if [ $state -ne 0 ]; then
                echo "Setting $n ($mac) to 0"
                url="${BASE_URL}?${QUERY}&action=SetStatus${n}&newStatus${n}=0"
                wget -O - $url > /dev/null 2> /dev/null && eval state_$n=0
            fi
        else
            eval count_$n=$((count-1))
        fi
    fi
done

sleep 1

done
[/code]

Elegant coding sir. Unfortunately I use the ping server functionality of the original script as well so I can’t easily change to this one.

Reardoncode, what is different with your code vs what I posted previously?
Thanks, I’m no coding expert :slight_smile:

Sent from my HTC6525LVW using Tapatalk

Aaron: Mostly I didn’t copy/paste the code for each MAC, instead used some shell magic to let me loop over them. This does make it a bit less flexible (the MACs are automatically assigned to switches on the multiswitch based on the order you list them in my script where the previous one you could specify specific switches for specific MACs).

BOFH: I’ll add that and better factor the code and post it to github after work today :slight_smile:

@reardencode: Thank you! I can deal with ensuring I list the MAC’s etc a certain way to get them to certain buttons as your version is more elegant and most likely also uses less resources.

Between this setup and UI7’s geofencing I’m up to about 99% reliability on presence detection.

Since I have 2 vera’s, I have them check on each other using the script and notify me if one can’t ping the other.

Updated version, added comments, etc., posted to github in case anyone has suggestions or comments.

https://raw.githubusercontent.com/reardencode/OpenWrtVeraOccupancy/master/vera_occupancy

The code is good, I do like being able to specify the buttons.
I don’t think this code will be any different from a resource usage stand point than the code I posted. Both code sets are very small and basically the same and should execute at about the same speed. The code I posted might actually be faster since it has fewer loops, thus less conditional checks… In the end no one would ever notice a difference

Sent from my HTC6525LVW using Tapatalk