so the Goood news is you were right even though i changed the script in the router to not include the {} brackets it needed a reboot to implement the change this in turn showed the sitemap as such
one problem Ive noticed is that my router scripts only update last seen every minute. The event script is only sent should my wifi connect or disconnect. However the 4 min expire is turning the presence off as no event happens and my phone is still here so no script is sent.
Ok, this is different behavior than expected which might require a rule.
It might not.
First of all, is it possible to update the php script to report the event every minute instead of just when the phone goes online or offline?
Is last seen published even when the phone is no longer connected? If it stops when the phone is disconnected we can use those messages to drive the switches instead of the events.
Do you always get a disconnect event when the phone goes away? If so we can use the events and do away with the timers, or change slightly how we use the timer.
Ok so the last seen scipt is run by a cronjob that starts every minute, The event script is placed in the RC Local file with a nohup command so as far as I understand, it runs continuously and sends the mqtt message when a phone disconnects or connects.
The phones becomes an ‘associated station’ on the router which in theory means that as long as the phone doesnt disconnect from wifi it should show as present no matter how long you leave the phone (sleep mode) however I have noticed that the wife’s iphone does disconnect when sleeping but so far my android has not( I will be changing her phone for an android soon so hopefully this wont be a problem).
The last seen script only sends the date,time and MAC addresses for any associated stations hence why i have to specify the MAC addresses i want receive updates for in my items. (namely my wife’s and my phone )
so if the phone disconnects it wont send the message for that MAC address.
The only problem with using this as the trigger is that it sometimes takes several minutes before the device become disassociated but this is still an option however once again it will be a problem with the iphone.
So far with my phone at least I have had accurate disconnect events and as far as ive seen so far i do not disconnect or become disassociated when my phone is idle.
As I only have the date and time and a power socket working on my openhab I havent left my openhab running as I wanted a presence detection working before I left it running continuously. So I havent got any logs to show whether my phone has 100% no disconnects or disasociations using the mqtt messages . The logs on my router are minimal and only really show when a handshake is performed between devices and that the last seen script is run. Its only by viewing the routers admin page that I see my wifes phone disconnect when idle or that my phone appears to never disconnect.
Maybe I should leave openhab running and at least this will show me if my theory is correct.
Good. We can use that. Add the following to your Person_x Items:
<[mosquitto:/presence/wifi/*-*-*-*-*-*:state:ON]
So every time the router sends a LastSeen message it will update the Switch Item to ON. since that happens every minute it will keep the expire binding from changing the Switch to OFF. 4 minutes after the last LastSeen message (if for some reason the disconnected event didn’t get sent) the Switch will go OFF.
This will be a problem no matter what I suspect.
Detecting iPhones on the network is a well known and difficult problem. When it goes to sleep it puts the device into a hibernation mode that makes it mostly unresponsive to network events. However, if you search the forum for “hping” you will see one way a lot of people have got around this problem with iPhones. The Network binding just added and arping feature that essentially does the same as the hping solution.
Personally, I use a combination of presence sensors in my system. I configured my and my wife’s phones with static IPs on the router. Then I use the Network binding to periodically ping them to see if they are present.
But the iPhone disconnects so I also use Bluetooth detection on a number of Raspberry Pis that I already have scattered throughout the house for other purposes using a script I wrote. Finally, I’ve added reelyActive to my Raspberry Pi 3 which also does BT device presence detection but in a smarter way. I’ve since replaced one of my old Pi 1s with a Pi Zero W so plan on adding reelyActive to that one as well and see if I can start to get some better resolution (e.g. tell which floor the device is on). That is a relatively low-level priority though.
BT works well for me because both my wife and I have a smart watch and my wife has a medical device always attached which also uses BT so it works well.
Anyway, I require at least two sensors to report a device as present to mark a person as present (BT can have false positives).
Good luck with that. I tried to do that once…
Tell me about it
I’ll have to do a bit of research on here about this arping solution with the network binding sounds like this will help with the accuracy of my presence detection.
I’ve also thought about Bluetooth as an option especially using reelyactive and I like the idea of using multiple sensors to attack the presence accuracy from different directions. I assume you have mentioned this in another thread. I’ll have to have a look into the things you’ve mentioned and try and implement it into my system.
Out of interest how close to 100% accurate is your setup, do you still get the occasional false presence or false away event ?
Thanks for all your help in getting theses problems with my presence setup sorted I’ll shall add that addition to my items and hopefully incorporate your other ideas.
The best I’ve ever seen is probably around 90%. Presence detection is one of those really really hard problems with more edge cases than you can imagine. The more sensors and the more diverse your sensors the better detection will be, but the more weird edge cases that you have to deal with.
I have fairly good wifi coverage so about 80% of the time a phone will connect to the wifi before we open the garage door. However, 20% of the time this doesn’t happen and I get “The garage door has opened and no one is home!” alerts. The BT detection, based on its nature, takes a few minutes before it will recognize a device is near by. You can make it work faster but that results in it flapping much more frequently which makes you have to deal with false absent events.
About the only full proof presence detection will involve not just sensing presence of devices but also heat sensors and/or cameras in every room.
I would have expected to see an error in the logs from the MQTT binding about that.
While you are at it, make double sure that the new part added to the event Item exactly matches the LastSeen Item except instead of using state:default use state:ON. This should cause the Person_x_Wifi_Event Item to receive an ON update every time any message is received on the LastSeen topic, regardless of the message’s contents.
Thanks Rich I copied and pasted the text from my other item and as you say change default to ON. I didn’t even notice I’d missed out the comma silly mistake !, well that’s the problem with C&P I suppose.
Tim, do you can post yours folows files: itens, presence_event.sh and presence_lastseen.sh?
My itens don’t be updated when router send mqtt messages…
Thanks
Ok so your router scripts are fine and you are receiving messages on your openHabian mqtt broker.
I didn’t use the items written in the link. as they require rules to update the items and as Rich and I discussed above, the rules in the example were using Setstate instead of Sendcommand which isnt good.
Thanks to Rich I changed my items and now they use a timer Binding instead of Rules to change my items status to off and Rules to only show a log and to send messages to my OpenHab mobile app when someone leaves or Arrives
I Have both the event script and lastseen script updating the item with a timer this updates my item every minute and after 3 minutes if the router doesn’t see my phone it shows as not present.
This works pretty well but not for Iphones
I have 2 Map transforms for my items
present.map
ON=is present
OFF=is not present
-=is unknown to be
and presence_event.map
new=ON
del=OFF
If you want to use the items format from the example I would recommend contacting the author through the Github link
Thanks guys, it’s working, follow my files, to other noobs like me:
Itens:
Switch Person_1_Wifi_Event "Gustavo Wifi" (Person_1)
{ mqtt="<[broker:presence/wifi/*-*-*-*-*-*/event:state:MAP(presence_event.map)],
<[broker:presence/wifi/*-*-*-*-*-*/event:state:ON]",
expire="4m,state=OFF" } // turns OFF 4 minutes after last time it is set ON
Switch Person_2_Wifi_Event "Juliana Wifi" (Person_2)
{ mqtt="<[broker:presence/wifi/*-*-*-*-*-*/event:state:MAP(presence_event.map)],
<[broker:presence/wifi/*-*-*-*-*-*/event:state:ON]",
expire="4m,state=OFF" } // turns OFF 4 minutes after last time it is set ON
DateTime Person_1_Wifi_LastSeen "Gustavo Wifi last seen [%1$td.%1$tm.%1$tY %1$tH:%1$tM]" <clock> { mqtt="<[broker:presence/wifi/*-*-*-*-*-*:state:default]" }
DateTime Person_2_Wifi_LastSeen "Juliana Wifi last seen [%1$td.%1$tm.%1$tY %1$tH:%1$tM]" <clock> { mqtt="<[broker:presence/wifi/*-*-*-*-*-*:state:default]" }
SItemap
Frame label="Presence" {
Text item=Presence
Switch item=Person_1
Text item=Person_1_Wifi_LastSeen
Switch item=Person_2
Text item=Person_2_Wifi_LastSeen
}
presence_event.sh
#!/bin/sh
MQTT_SERVER="192.168.1.254"
MQTT_ID="OpenWRT-Presence-LastSeen"
MQTT_TOPIC="presence/wifi/"
iw event | \
while read LINE; do
if echo $LINE | grep -q -E "(new|del) station"; then
EVENT=`echo $LINE | awk '/(new|del) station/ {print $2}'`
MAC=`echo $LINE | awk '/(new|del) station/ {print $4}'`
#echo "Mac: $MAC did $EVENT"
mosquitto_pub -h $MQTT_SERVER -i $MQTT_ID -t "$MQTT_TOPIC${MAC//:/-}/event" -m $EVENT
fi
done
presence_lastseen.sh
#!/bin/sh
MQTT_SERVER="192.168.1.254"
MQTT_ID="OpenWRT-Presence-LastSeen"
MQTT_TOPIC="presence/wifi/"
for interface in `iw dev | grep Interface | cut -f 2 -s -d" "`
do
# for each interface, get mac addresses of connected stations/clients
maclist=`iw dev $interface station dump | grep Station | cut -f 2 -s -d" "`
# for each mac address in that list...
for mac in $maclist
do
mosquitto_pub -h $MQTT_SERVER -i $MQTT_ID -t "$MQTT_TOPIC${mac//:/-}" -m $(date +%Y-%m-%dT%H:%M:%S%z)
done
done