Hello, i am trying to connect Life360 presence detection to my openhab. Just copied this script from a forum:
#!/bin/bash
#Set variables
username360="username"
password360="PASS"
mosquitto_pub="/path/to/mosquitto_pub"
mqtt_host="127.0.0.1"
mqtt_port="1883"
mqtt_user="USER"
mqtt_pass="PASS"
timeout=300
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"
}
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[@]}"
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)
$mosquitto_pub -h $mqtt_host -p $mqtt_port -u $mqtt_user -P $mqtt_pass -t "owntracks/${firstName//\"/}/${i//\"/}" -m "{\"t\":\"p\",\"tst\":$(date +%s),\"acc\":${accuracy//\"/},\"_type\":\"location\",\"alt\":0,\"lon\":${longitude//\"/},\"lat\":${latitude//\"/},\"batt\": ${battery//\"/}}"
count=$(($count+1))
done
done
sleep $timeout
done
and put it into conf/scripts folder. When i run the script it gives : "Error: Unknown option 'owntracks/âŠ"
I really donât know if i have to use owntracks or is this mosquitto_pub is defining something that i really need. What i really need is that i try to publish presence and battery status of the iphones that my family members have, in my openhab sitemap. But stuck at the MQTT part that i simply know nothing about. What will be the steps from this on? Any help appreciatedâŠ
OK Made it work. Leaving the mqtt_pass empty makes the script thought -t is the password and topic is an option. Silly mistake.
You can get any status, lat, long, battery etc with an item including {mqtt="<[mosquitto:owntracks/Erkan:state:JSONPATH($.batt):.*]"}. However this script has to be run all the time in the background with nohup. Also, you can not get push messages from life360 like you do on owntracks. Time to decide whether i should go with life360 or owntracksâŠ
So, i put this into use. For people who might want to use it instead of owntracks, since my whole family is currently using Life360 and it is not easy for me to convince them to use owntracks instead of Life360;
life360.sh:
#!/bin/bash
#Set variables
username360="Life360Username"
password360="Life360Password"
mosquitto_pub="/volume1/@appstore/mosquitto/bin/mosquitto_pub"
mqtt_host="127.0.0.1"
mqtt_port="1883"
mqtt_user="mosquitto"
mqtt_pass="mqtt_pass"
timeout=300
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"
}
bearer
circles $bearer_id
#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]}" # @ is made 0 to include Family group only
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
$mosquitto_pub -h $mqtt_host -p $mqtt_port -u $mqtt_user -P $mqtt_pass -t "owntracks/${firstName//\"/}" -m "{\"t\":\"p\",\"tst\":$(date +%s),\"acc\":${accuracy//\"/},\"_type\":\"location\",\"alt\":0,\"lon\":${longitude//\"/},\"lat\":${latitude//\"/},\"batt\": ${battery//\"/},\"locname\": ${locationname//\"/}}"
#Forgot to put next line previously, will solve de ID problem
count=$(($count+1))
done
done
Life360.items:
Switch PresenceF_i360Home "F @ Home [%s]" {mqtt="<[mosquitto:owntracks/F:state:JSONPATH($.locname):.*]"}
Switch PresenceD_i360Home "D @ Home [%s]" {mqtt="<[mosquitto:owntracks/D:state:JSONPATH($.locname):.*]"}
Switch PresenceH_i360Home "H @ Home [%s]" {mqtt="<[mosquitto:owntracks/H:state:JSONPATH($.locname):.*]"}
myhome.sitemap:
Text item=FamilyPresence label="Presence" icon="athome" {
Frame {
Text item=PresenceF_i360Home icon="athome"
Text item=PresenceD_i360Home icon="athome"
Text item=PresenceH_i360Home icon="athome"
}
}
myhome.rules:
rule "Check Life360 Presence"
when
Time cron "0 */5 * * * ? *" // every 5 minutes
then
executeCommandLine("/etc/openhab2/scripts/life360.sh@@start", 5*1000)
if (PresenceF_i360Home.state == ON || PresenceD_i360Home.state == ON || PresenceH_i360Home.state == ON) {
postUpdate(FamilyPresence,ON)
logInfo ("Life360", "Somebody Home")
} else
{
postUpdate(FamilyPresence,OFF)
logInfo ("Life360", "Nobody is Home")
}
end
I really am not sure if this is the best or the most optimum way to do it, however i am a beginner and this just works for me.
Super solution which is just up my alley, I seem to just miss a point or two !
I got jq and mosquitto installed, confâd and tested mosquitto successfully with this detailed guide: https://www.digitalocean.com/community/tutorials/how-to-install-and-secure-the-mosquitto-mqtt-messaging-broker-on-debian-8 , placed the above sh script in /etc/openhab2/Life360 and chmod +x on the script. Created the Circle named âHomeâ with members on https://www.life360.com. Also saw the MQTT receive correct messages on 5 min interval after adding a bash in the rules line to : executeCommandLine(âbash /path/to/script/life360.shâ, 3*1000) on debian Turnkeylinux VM - otherwise the error: âfunction: not foundâ and âSyntax error: â}â unexpectedâ.
But I seem to lack some connection to the items or sitemaps in Openhab2, I donât get the statusâ shown in basicUI.
Items file:
Switch PresenceL_i360Home "L @ Home [%s]" {mqtt="<[mosquitto:owntracks/L:state:JSONPATH($.locname):.*]"}
Switch PresenceC_i360Home "C @ Home [%s]" {mqtt="<[mosquitto:owntracks/C:state:JSONPATH($.locname):.*]"}
Switch PresenceT_i360Home "T @ Home [%s]" {mqtt="<[mosquitto:owntracks/T:state:JSONPATH($.locname):.*]"}
rules file:
rule "Check Life360 locations"
when
Time cron "0 */5 * * * ? *" // every 5 minutes
then
executeCommandLine('bash /etc/openhab2/Life360/Life360.sh', 3*1000)
end
sitemap file:
Text item=PresenceL_i360Home icon="present" valuecolor=[ON="green",OFF="blue"]
Text item=PresenceC_i360Home icon="present" valuecolor=[ON="green",OFF="blue"]
Text item=PresenceT_i360Home icon="present" valuecolor=[ON="green",OFF="blue"]
Well, nothing is unsaid on the net, found an alternative working example here: https://pastebin.com/xrYTvtCR and mixed it with the above and got this running without the duplicate ID bug, successfully tested against MQTT for all IDs, which previous script did not count for. Removed the timeouts as the rule should control that.
Still got the status unwilling to report to Openhab2 though.
#!/bin/bash
#Set variables
username360="xxxxx"
password360="xxxx"
mosquitto_pub="/usr/bin/mosquitto_pub"
mqtt_host="127.0.0.1"
mqtt_port="1883"
mqtt_user="xxxxx"
mqtt_pass="xxxxx"
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 () {
echo "$(date +%s) INFO: requesting members"
members=$(curl -s -X GET -H "Authorization: Bearer $1" https://api.life360.com/v3/circles/$2)
}
bearer
circles $bearer_id
#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[@]}"
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_count=$(echo $members | jq '.members | (length)')
count=0
while [ $count -lt $members_count ]
do
id=$(echo $members | jq .members[$count].id)
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)
avatar=$(echo $members | jq .members[$count].avatar)
locationname=$(echo $members | jq .members[$count].location.name)
if [ "$locationname" = "\"Home"\" ]; then
locationname="ON"
else
locationname="OFF"
fi
#echo $firstName
#echo "$(date +%s) INFO: owntracks/${firstName//\"/}/${id//\"/}" -m "{\"t\":\"p\",\"tst\":$(date +%s),\"acc\":${accuracy//\"/},\"_type\":\"location\",\"alt\":0,\"lon\":${longitude//\"/},\"lat\":${latitude//\"/},\"batt\": ${battery//\"/}}"
$mosquitto_pub -h $mqtt_host -p $mqtt_port -u $mqtt_user -P $mqtt_pass -t "owntracks/${firstName//\"/}" -m "{\"t\":\"p\",\"tst\":$(date +%s),\"acc\":${accuracy//\"/},\"_type\":\"location\",\"alt\":0,\"lon\":${longitude//\"/},\"lat\":${latitude//\"/},\"batt\": ${battery//\"/},\"locname\": ${locationname//\"/}}"
count=$(($count+1))
done
done
Sorry that i moved to domoticz instead of openhab just because of zwave issues. However as i remember these might light up some solution:
Add âcount=$(($count+1))â to the end of bash script as shown in my previous message, this can solve ID problem, i updated the script. EDIT: Oh you did it
I also updated the myhome.sitemap in that message also.
Also the rules fileâŠ
You should add both mqtt and mqttitude(or owntracks) in openhab2. I remember the script stopped working when i remove mqttitude.
I still use this bash file on domoticz and it is the best presence detector for me; i tried many!..
Got it working on the TKL LAMP image - had to add the paperUI\Add Ons\Transformation\JSONPath Transformation to be able to transform the Items. Reminder to myself - to only have one or no Frames in sitemaps.
Using :
mosquitto_sub -v -t '#'
to monitor mqtt is better than previous example.
Thanks for the support on this.
Now I only need the actions taken, based on presence.
hello, thanks for the info. I did everything indicated but I donât see the presence.
I used the internal Openhab MQTT broker, the script works, in the mqtt client I see the information. I see the items in the configuration, if I go to the CONTROLs I donât see any switch indicating the presence of the members of the circle in the house.
In the HabPanel I created switches that point to the items, but they are always off.
Some idea?
I am very new to IoT in general and I am trying to figure out how MQTT works and how OpenHab handles it in 2.4. I am working on figuring out how to turn what was posted above into a MQTT binding for version 2.4. You mention there you were able to do it, could you post a list of the steps you took to make it. It would help me a lot.
Does someone know what information encoded in base64 part of basic authentication header?
Is it safely to send my login and password using this script? So, maybe iâm paranoid.