Life360 Presence Detection

All kinds of 'OS' scripts
Post Reply
febalci
Posts: 83
Joined: Monday 03 July 2017 19:58
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Life360 Presence Detection

Post by febalci » Saturday 08 July 2017 14:45

Hello, Beginner in Domoticz here. Using iphones familywise. It has always been a headache using presence for home automation on iphones. I tried almost all, including ddwrt, hping3, homekit etc. DDwrt is in basement, my DSL router is in the entrance and it is the first one that catches wifi and it is not open source, so count that out. hping3 is not working on my synology domoticz server, and cannot wake iphone. Homekit seems promising together with homebridge, however family automations are still not there...

So, as family we are using life360 app on iphone to check and warn which of us enters or leaves the house with notifications. Altough the api of life360 is not open, the following bash script which i gathered parts from this and there on web works for me:

Code: Select all

#!/bin/bash

#Set variables
#Domoticz Server Settings
domoticzserver="192.168.1.90:8080"
domoticzusername="username"
domoticzpassword="password"
username360="life360username"
password360="life360password"

function bearer {
echo "$(date +%s) INFO: requesting access token"
bearer_id=$(curl -s -X POST -H "Authorization: Basic cFJFcXVnYWJSZXRyZTRFc3RldGhlcnVmcmVQdW1hbUV4dWNyRUh1YzptM2ZydXBSZXRSZXN3ZXJFQ2hBUHJFOTZxYWtFZHI0Vg==" -F "grant_type=password" -F "username=$username360" -F "password=$password360" https://api.life360.com/v3/oauth2/token.json | grep -Po '(?<="access_token":")\w*')
}

function circles () {
echo "$(date +%s) INFO: requesting circles."
read -a circles_id <<<$(curl -s -X GET -H "Authorization: Bearer $1" https://api.life360.com/v3/circles.json | grep -Po '(?<="id":")[\w-]*')
}

function members () {
members=$(curl -s -X GET -H "Authorization: Bearer $1" https://api.life360.com/v3/circles/$2)
echo "$(date +%s) INFO: requesting members"
}

function domoticzrequest () {
domoticzmessage=$(curl -s -u domoticzusername:domoticzpassword -X GET $1) 
}

bearer
circles $bearer_id

#Main Loop
#while :
#do

#Check if circle id is valid. If not request new token.
if [ -z "$circles_id" ]; then
bearer
circles $bearer_id
fi

#Loop through circle ids
for i in "${circles_id[0]}" # @ changed to  0 only for the family group
do

#request member list
members $bearer_id $i

#Check if member array is valid. If not request new token
if [ -z "$members" ]; then
bearer
circles $bearer_id
members $bearer_id $i
fi

members_id=$(echo $members | jq '.members[].id')
IFS=$'\n' read -rd '' -a members_array <<<"$members_id"
count=0
for i in "${members_array[@]}"
do
    firstName=$(echo $members | jq .members[$count].firstName)
    lastName=$(echo $members | jq .members[$count].lastName)
    latitude=$(echo $members | jq .members[$count].location.latitude)
    longitude=$(echo $members | jq .members[$count].location.longitude)
    accuracy=$(echo $members | jq .members[$count].location.accuracy)
    battery=$(echo $members | jq .members[$count].location.battery)
    locationname=$(echo $members | jq .members[$count].location.name)
	if [ "$locationname" = "\"Home"\" ]; then 
		locationname="On"
    else
		locationname="Off"
    fi
	echo "$locationname"	
	echo "$firstName"	
# Me	
	if [ "$firstName" = "\"King of the North"\" ]; then 
	    passaddress="http://$domoticzserver/json.htm?type=command&param=switchlight&idx=97&switchcmd=$locationname"
	fi	
#Wife
	if [ "$firstName" = "\"Queen of the North"\" ]; then 
	    passaddress="http://$domoticzserver/json.htm?type=command&param=switchlight&idx=98&switchcmd=$locationname"
	fi	
#Son
	if [ "$firstName" = "\"Prince of the North"\" ]; then 
	    passaddress="http://$domoticzserver/json.htm?type=command&param=switchlight&idx=99&switchcmd=$locationname"
	fi	
	domoticzrequest $passaddress
	count=$(($count+1))
done
done
#sleep $timeout
#done
I created 3 dummy switches for me, wife and son. Created timers in TimerPlans as "Home" and "Away". Using Lua script in events as

Code: Select all

if (devicechanged['FEB_Presence'] or devicechanged['DIB_Presence'] or devicechanged['HTB_Presence']) then
    if (otherdevices['FEB_Presence'] == 'On' or otherdevices['DIB_Presence']== 'On' or otherdevices['HTB_Presence']== 'On') then
        os.execute('curl -s "http://username:[email protected]:8080/json.htm?type=command&param=setactivetimerplan&ActiveTimerPlan=4" &')
        print("Family is Home")
    else
        os.execute('curl -s "http://username:[email protected]:8080/json.htm?type=command&param=setactivetimerplan&ActiveTimerPlan=3" &')
        print("Family is Away")    
    end
end
Now the thing is; since i started Domoticz after openHab, i couldn't set the events usage stable in my head. The Bash script is working with synology cron every 2 minutes. It pushes presence info to domoticz json. Lua script is checking switch updates. It pushes to domoticz json again for setting active timer plan. Now that seems to me that is way excessive usage of cron or scripts. How do i keep everything in one place or make it more effective? Any suggestions?

User avatar
heggink
Posts: 304
Joined: Tuesday 08 September 2015 21:44
Target OS: Raspberry Pi
Domoticz version: V3.7067
Location: NL
Contact:

Re: Life360 Presence Detection

Post by heggink » Friday 21 July 2017 14:57

Hi febalci,
i tried your script but it keeps giving me errors. Would you mind sending me the script as a pm?
I'd love to give it a try as I have both iphones and droid.
btw, I use an icloud python script that works VERY well for locating the devices (uses icloud family).
H
Pi3, latest beta
RFXCOM, z-wave (AEOTEC, switches, temhum, pir, contacts),
Plugwise2py, P1 'smart'meter & solar panel
Alexa, Wifi Cams motion detection
ESP: relays, PIR & Temp/TempHum
Geofence iCloud, Bluetooth & Wifi ping
Harmony hub, Nest

febalci
Posts: 83
Joined: Monday 03 July 2017 19:58
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Life360 Presence Detection

Post by febalci » Monday 31 July 2017 12:54

I've sent you the PM. Icloud and homekit presence sounds promising however they still use geofencing and what i need is to check the presence if i am at the door of the house, geofencing is either unstable when my phone connects to cellphone tower 2 kms away or in best case thinks i am at home when i am actually 100 meters away in my friends house...

User avatar
warp
Posts: 62
Joined: Thursday 07 May 2015 12:51
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Fr
Contact:

Re: Life360 Presence Detection

Post by warp » Wednesday 02 August 2017 15:35

Otherwise it is necessary to use IFTTT with life360 and marker (api json of domoticz), it works well with me.
https://www.domoticz.com/wiki/IFTTT_int ... h_Domoticz
Raspberry 2B:RaZberry Z-Wave+:Edimax EW-7811UN:Huawei E220 3G
4xQubino ZMNHCA2
Everspring SE812 & ST812
Mcohome MH-S411 & MH-S412
2xAEON LABS ZW-100
2xFibaro FGSD-002:Fibaro FGFS-101:Fibaro FGS-221 & KLF-050

User avatar
Derik
Posts: 1841
Joined: Friday 18 October 2013 23:33
Target OS: Raspberry Pi
Domoticz version: BETA
Location: Arnhem/Nijmegen Nederland
Contact:

Re: Life360 Presence Detection

Post by Derik » Wednesday 02 August 2017 19:01

dear...
Like the life360
Only do you have perhaps also a map with the place where the phones are?
Or is the a option to switch a dummy when someone is in a place x and perhaps a place y?
Is it possible to switch on more places?


Or how do i need to see your script?
Xu4: Beta Extreme antenna RFXcomE,WU Fi Ping ip P1 Gen5 PVOutput Harmony HUE SolarmanPv OTG Winddelen Alive ESP Buienradar MySensors WOL Winddelen counting RPi: Beta SMAspot RFlinkTest Domoticz ...Different backups

febalci
Posts: 83
Joined: Monday 03 July 2017 19:58
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Life360 Presence Detection

Post by febalci » Wednesday 02 August 2017 20:52

Dear Derik,

Actually what you mentioned is the real purpose of Life360. When you install the Life360 app on Android or Iphone, you can see all phones on the map, a little history where they have been, and also you can name locations as well as you named "Home". So what you are mentioning i am doing it in Life360 application itself and do not need any scripting. What i needed in Domoticz is only the presence at home, so that i can switch on the lights and blinds, and when i leave home switch off the lights, tv and blinds etc. Thats why i only focused on entering and leaving the house, but you can also add more to the script.

febalci
Posts: 83
Joined: Monday 03 July 2017 19:58
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Life360 Presence Detection

Post by febalci » Wednesday 02 August 2017 20:58

Dear warp, i especially try not to use IFTT just because i want everything to be handled under Domoticz. That's better and easier for backup/restore, upgrade and any other purposes. If there was a way to handle presence all by Domoticz i woud have prefer it that way, however i am stuck with life360 since it is the best presence method i could come up so far.

So in short if i use IFTT, Philips Hue, Apple Homekit etc. it becomes so complicated with rules distributed everywhere, even a simple debug becomes a pain. Instead of Life360->IFTT->Domoticz, i prefer to use Life360->Domoticz. But if yours is working stable, who am i to judge :)

User avatar
warp
Posts: 62
Joined: Thursday 07 May 2015 12:51
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Fr
Contact:

Re: Life360 Presence Detection

Post by warp » Thursday 03 August 2017 10:00

Hi febalci,
i agree with you, it is better to be autonomous and to concentrate all the logic.
Do you think we can ask "Gizmocuz" to integrate your solution into "plugin/hardware" like "Weather Underground" and many others ?

Which would be simpler for most people.
Raspberry 2B:RaZberry Z-Wave+:Edimax EW-7811UN:Huawei E220 3G
4xQubino ZMNHCA2
Everspring SE812 & ST812
Mcohome MH-S411 & MH-S412
2xAEON LABS ZW-100
2xFibaro FGSD-002:Fibaro FGFS-101:Fibaro FGS-221 & KLF-050

User avatar
Derik
Posts: 1841
Joined: Friday 18 October 2013 23:33
Target OS: Raspberry Pi
Domoticz version: BETA
Location: Arnhem/Nijmegen Nederland
Contact:

Re: Life360 Presence Detection

Post by Derik » Thursday 03 August 2017 18:08

mmm what i am looking for is to see in a personal tab in domoticz to see where my family phones are.
The home and away is very simple to make with ping ip...
Xu4: Beta Extreme antenna RFXcomE,WU Fi Ping ip P1 Gen5 PVOutput Harmony HUE SolarmanPv OTG Winddelen Alive ESP Buienradar MySensors WOL Winddelen counting RPi: Beta SMAspot RFlinkTest Domoticz ...Different backups

User avatar
EdwinK
Posts: 795
Joined: Sunday 22 January 2017 22:46
Target OS: NAS (Synology & others)
Domoticz version: 3.8552
Location: Rhoon
Contact:

Re: Life360 Presence Detection

Post by EdwinK » Friday 22 September 2017 19:20

Now I have the app installed on my Android and the first script installed. However, I don't really see what I need to do with the second (LUA?) script. I'm the only one here, so I don't need all those checks for more phones.

My device is called 'Telefoon Edwin' , and atm it's working with the Geofence feature of the Domoticz app, but I see many errors, switching to and from AWAY.

Code: Select all

parse error: Invalid numeric literal at line 1, column 10
Running latest STABLE on a NAS Synology 413j | Toon Thermostat | RFxcom | Dashticz V2 |

febalci
Posts: 83
Joined: Monday 03 July 2017 19:58
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Life360 Presence Detection

Post by febalci » Friday 22 September 2017 19:53

Dear Edwin,
as you are using android phone there might be easier ways for presence detection, since i do not have an android phone, and presence with iphone is always a headache, i used this approach. Using Life360 for one person is in fact really not necessary because the app logic is to see your family and frends whereabouts. But nevermind the scripts for one person would be as follows:
1. Install Life360 app on your phone
2. Create a Circle called "Family" in Life360
3. From "Add Home" add your home in your name on the map and call it "Home".

4. I do not know on which platform you installed domoticz, raspberry or synology or ... In any case any server with linux works. Create the following bash script on vi or any other text processor on linux and save as life360.sh. And give it +x for executable permissions.

Code: Select all

#!/bin/bash

#Set variables
#Domoticz Server Settings
domoticzserver="your_domoticz_server_ip:your_domoticz_server_port"
domoticzusername="your_domoticz_username"
domoticzpassword="your_domoticz_password"
username360="your_life360_username"
password360="your_life360_password"

function bearer {
echo "$(date +%s) INFO: requesting access token"
bearer_id=$(curl -s -X POST -H "Authorization: Basic cFJFcXVnYWJSZXRyZTRFc3RldGhlcnVmcmVQdW1hbUV4dWNyRUh1YzptM2ZydXBSZXRSZXN3ZXJFQ2hBUHJFOTZxYWtFZHI0Vg==" -F "grant_type=password" -F "username=$username360" -F "password=$password360" https://api.life360.com/v3/oauth2/token.json | grep -Po '(?<="access_token":")\w*')
}

function circles () {
echo "$(date +%s) INFO: requesting circles."
read -a circles_id <<<$(curl -s -X GET -H "Authorization: Bearer $1" https://api.life360.com/v3/circles.json | grep -Po '(?<="id":")[\w-]*')
}

function members () {
members=$(curl -s -X GET -H "Authorization: Bearer $1" https://api.life360.com/v3/circles/$2)
echo "$(date +%s) INFO: requesting members"
}

function domoticzrequest () {
domoticzmessage=$(curl -s -u domoticzusername:domoticzpassword -X GET $1)
}

bearer
circles $bearer_id

#Main Loop
#while :
#do

#Check if circle id is valid. If not request new token.
if [ -z "$circles_id" ]; then
bearer
circles $bearer_id
fi

#Loop through circle ids
for i in "${circles_id[0]}" # @ işareti 0 yapıldı sadece Family grubu olsun diye
do

#request member list
members $bearer_id $i

#Check if member array is valid. If not request new token
if [ -z "$members" ]; then
bearer
circles $bearer_id
members $bearer_id $i
fi

members_id=$(echo $members | jq '.members[].id')
IFS=$'\n' read -rd '' -a members_array <<<"$members_id"
count=0
for i in "${members_array[@]}"
do
    firstName=$(echo $members | jq .members[$count].firstName)
    lastName=$(echo $members | jq .members[$count].lastName)
    latitude=$(echo $members | jq .members[$count].location.latitude)
    longitude=$(echo $members | jq .members[$count].location.longitude)
    accuracy=$(echo $members | jq .members[$count].location.accuracy)
    battery=$(echo $members | jq .members[$count].location.battery)
    locationname=$(echo $members | jq .members[$count].location.name)
	if [ "$locationname" = "\"Home"\" ]; then 
		locationname="On"
    else
		locationname="Off"
    fi
#	echo "$locationname"	
#	echo "$firstName"	
# Change your name here to Life360 Full First name
	if [ "$firstName" = "\"Edwin"\" ]; then 
	#Change the following XX to IDX of your Edwin_Presence Switch
	    passaddress="http://$domoticzserver/json.htm?type=command&param=switchlight&idx=XX&switchcmd=$locationname"
	    checkaddress="http://$domoticzserver/json.htm?type=devices&rid=XX"
	fi	

  domoticzrequest $checkaddress
  presence_status=$(jq -r '.result[0].Data' <<<"$domoticzmessage")	
  if [ "$locationname" != "$presence_status" ]; then
      echo $presence_status
      domoticzrequest $passaddress
  fi
	count=$(($count+1))
done
done
#sleep $timeout
#done
5. Add a cronjob to execute this script every lets say 5 min.
6. Create a dummy switch in domoticz (On/Off Switch) called "Edwin_Presence"
7. Create 2 timerplans
8. In Domoticz, Setup-More Options-Events, create a LUA script, with "Device" not "All" and paste the following with changing the required parts:

Code: Select all

commandArray = {}
if (devicechanged['Edwin_Presence']) then
    if (otherdevices['FEB_Presence'] == 'On') then
        os.execute('curl -s "http://username:[email protected]_ip:domoticz_port/json.htm?type=command&param=setactivetimerplan&ActiveTimerPlan=4" &')
        print("Family is Home")
    else
        os.execute('curl -s "http://username:[email protected]_ip:domoticz_port/json.htm?type=command&param=setactivetimerplan&ActiveTimerPlan=3" &')
        print("Family is Away")    
    end
end
I'm sorry, it doesn't get simpler than this on my side. As i told you can always use another presence method like the Pilot Domoticz app presence if you're looking for simpler solution. Second script (LUA) is just for changing Timer events when i am at home and not at home.

User avatar
EdwinK
Posts: 795
Joined: Sunday 22 January 2017 22:46
Target OS: NAS (Synology & others)
Domoticz version: 3.8552
Location: Rhoon
Contact:

Re: Life360 Presence Detection

Post by EdwinK » Friday 22 September 2017 22:36

Going to take a look at this some other time :) Thanks for your help anyway.

For now I think I've got a working presence-detector with another script.
Running latest STABLE on a NAS Synology 413j | Toon Thermostat | RFxcom | Dashticz V2 |

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest