Want to use Life360 to track and use home/away status

My wife and I both run Life360 on our phones, and I currently use it to arm & disarm my iSmartAlarm system (e.g. when the last person leaves the house it arms the alarm, and when the first person arrives at home it disarms the alarm).
I’d like to integrate our Life360 tracking into my HA setup (0.61.1 on Lubuntu), by having a state which I can refer to in HA and which can trigger scripts etc - e.g. when both are at home turn off set the Blue Iris camera profile to “at home”, or when both are out turn off the heating.
I’m a newcomer to HA. So far I have added the Broadlink and Google Assistant platforms, and have set up a couple of simple automations to turn a couple of power sockets on/off at specified times of day.
I’m vaguely aware of MQTT, but have never explored it. Is there a good dummies guide anywhere for linking Life360 with HA and setting up some sort of state variable which I can use to trigger HA actions?

I’ve made a start with MQTT…I managed to get MQTT running and defined a sensor, which is shown at the top of my HA console:

mqtt:
  embedded:
    listeners:
      default:
        max-connections: 5000
        bind: 192.168.1.36:1883
        type: tcp
      ws-1:
          bind: 192.168.1.36:8087
          type: ws
    auth:
      plugins: ['auth.anonymous']
      allow-anonymous: true
  broker: 192.168.1.36
  port: 1883
  
sensor:
  - platform: mqtt
    name: "Ian Location"
    state_topic: "home-assistant/presence/ian"

I then used the HA console to publish this to MQTT:

{
   "topic":"home-assistant/presence/ian",
   "retain":"true",
   "payload":"Home"
}

But the sensor value on my HA console remains set to “Unknown”. Why is my MQTT message not changing my sensor value?

You might want to take a look at this custom component that’s been created for Life360 already: https://github.com/skalavala/smarthome/blob/master/custom_components/sensor/life360.py

It’s used in conjunction with the Owntracks device tracker.

Essentially it takes the location from your Life360 app, and converts it to the Owntracks MQTT format which HASS can then read.

Thanks for that - it sounds promising. Looking at the instructions for installing it, I have a dumnb question - when they say “Copy corresponding Life360 Package from my repo” - I have no idea how to follow that instruction (!):
(a) where/what is the repo
(b) how do I copy a package from it
© where do I copy it to?
Sorry to ask such basic questions but I’m a newcomer to HA (and not a Python developer) and I’m struggling to piece together a basic “how to” guide for alot of this stuff!

…sorry…more dumb questions…

  • do I need to replace the access token stuff in the py file with my own variant? If so, how do I get my own details?
  • what do I need to put into my configuration.yaml to load this component?

Hey, no problem. Wasn’t sure of your experience so I didn’t go into detail the first time.

Essentially the steps are as follows:

  1. Create a /custom_components/sensor directory
  2. Paste life360.py (which I linked) into that directory, exactly as it is on GitHub
  3. In your configuration.yaml, put in the following:
sensor:
  - platform: life360
    name: life360_ian
    username: <your life360 username>
    password: <your life360 password>
    mqtt_topic: "life360/ian"
  1. Also in your configuration.yaml, add a section for the OwnTracks device tracker
device_tracker:
  - platform: owntracks

Additional options for the OwnTracks device tracker can be found here: OwnTracks - Home Assistant

Thanks for that. I now see the life360 sensor, but I don’t see the owntracks device tracker on my States page. My HA console also shows this message:

Unable to load /home/homeassistant/.homeassistant/known_devices.yaml: Config file not found: /home/homeassistant/.homeassistant/known_devices.yaml

Is there something else that I need to do to set up the owntracks device_tracker or the life360 component

Strange… No, I don’t believe there’s anything else required. It should auto-create.

Can you see if known_devices.yaml actually exists in your config directory?

If not perhaps try touch /home/homeassistant/.homeassistant/known_devices.yaml as the user account you’re running homeassistant with, then restart.

I’ve run the touch command and restarted. I now no longer see the known_devices.yaml error message, but I also don’t see the owntracks device tracker anywhere on the HA console.

Can you see anything populated in the contents of known_devices.yaml?

This is definitely strange, I’ve given you almost the exact config I use (only difference being that I use the ASUSWRT tracker in conjunction with Life360) and I haven’t had an issue like that at all.

If you can’t see anything populated in known_devices.yaml, can you try and subscribe to the MQTT broker and check if there are any /owntracks/ topics?

No - known_devices.yaml is empty.

Can I clarify what changes, if any, I should have made to life360.py? Do I need to replace all instances of USERNAME360 and PASSWORD360 with my own Life360 uid/password? I also didn’t know what to do about this instruction in the life360.py comments - “Copy corresponding Life360 Package from my repo”. Should I have done something here??

I believe that I am already subscribed to the MQTT broker - I have this in my configuration.yaml:

mqtt:
  embedded:
    listeners:
      default:
        max-connections: 5000
        bind: 192.168.1.36:1883
        type: tcp
      ws-1:
          bind: 192.168.1.36:8087
          type: ws
    auth:
      plugins: ['auth.anonymous']
      allow-anonymous: true
  broker: 192.168.1.36
  port: 1883

…but I don’t see a reference to any owntracks topics anywhere.

Hey, apologies for late reply. I’m away from home for a few days with limited internet access so may not be much help.

No changes required to life360.py - USERNAME360 and PASSWORD360 are defined by the username and password values you put in the sensor component.

The config you’ve listed above is setting up the MQTT broker which all looks good as far as I can tell (I use mosquitto, not the embedded broker so can’t compare to my own config).

By subscribing, I mean grab an MQTT client and connect to the broker so you can see if it’s creating the owntracks topics.

I’m not 100% sure this will work but you could try installing the mosquitto client - apt-get install mosquitto-client or possibly apt-get install mosquitto-clients

Once that’s installed, then you can try mosquitto_sub -v -h 192.168.1.36 -p 1883 -t '#'

-v = verbose
-h = host
-p = port
-t = topic and ‘#’ means all topics.

If life360.py is working correctly you should see something like owntracks/ian and life360/ian

Thanks. I didn’t have any luck installing Mosquitto on my Lubuntu 16.10 platform - it kept giving me this error:

Err:3 http://security.ubuntu.com/ubuntu yakkety-security/universe amd64 mosquitto amd64 1.4.8-1ubuntu0.16.10.1
  404  Not Found [IP: 91.189.88.149 80]

…but I managed to install MQTT.fx on a Windows box, and have managed to subscribe to the owntracks topics. It looks like they’re updating - I can see valid longitude and latitude values (as well as battery level etc) - but there’s still no sign of my owntracks device_tracker on my HA console, and no known_devices.yaml file being created/updated.
???

FYI I can see 3 topics being updated in my MQTT subscriptions:
life360/ian
owntracks/my name/my name
owntracks/my wife’s name/my wife’s name
(my wife is in my Life360 family circle).
But still no known_devices.yaml file being created
???

Hmmm…I’ve just noticed that this error is also appearing when I restart HA:

Path to configuration.yaml: /config
Error fetching description at http://192.168.1.249:50001/
13:12 ssdp.py (WARNING)
Error doing job: Task exception was never retrieved
13:12 /usr/src/app/homeassistant/core.py (ERROR)
Unable to load /config/known_devices.yaml: Config file not found: /config/known_devices.yaml
13:12 components/device_tracker/__init__.py (ERROR)
Press the button to load the full Home Assistant log.

Log Details (ERROR)
Sun Mar 11 2018 13:12:45 GMT+0000 (GMT Standard Time)

Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/asyncio/tasks.py", line 180, in _step
    result = coro.send(None)
  File "/usr/local/lib/python3.6/site-packages/hbmqtt/broker.py", line 342, in stream_connected
    yield from self.client_connected(listener_name, StreamReaderAdapter(reader), StreamWriterAdapter(writer))
  File "/usr/local/lib/python3.6/site-packages/hbmqtt/broker.py", line 476, in client_connected
    yield from self.publish_retained_messages_for_subscription(subscription, client_session)
  File "/usr/local/lib/python3.6/site-packages/hbmqtt/broker.py", line 723, in publish_retained_messages_for_subscription
    handler.mqtt_publish(
AttributeError: 'NoneType' object has no attribute 'mqtt_publish'

[Edit: adding “client_id: 12345” to my sensor config makes the above error go away]

Just pondering on how this is all (not) hanging together…how is the sensor definition linked to my owntracks device tracker? Is it the code in life360.py that sends owntracks device tracker info to the life360 sensor?

Is it the code in life360.py that sends owntracks device tracker info to the life360 sensor?

The opposite way around, but yes. The workflow is essentially this:

  • Life360.py polls Life360 for status information on members of your circle
  • Life360.py dumps all that info into MQTT under the life360/ian topic
  • Life360.py also pulls the relevant fields for the Owntracks device tracker, and creates an MQTT topic that matches the format the OwnTracks device_tracker expects to see
  • The OwnTracks device_tracker component should pick up the owntracks/ian/ian topic, and populate known_devices.yaml

You’re running homeassistant in docker right? What user is the docker container running as? Given that HA seems to not be able to create, or populate known_devices.yaml perhaps it could be a permissions issue?

For the sake of testing, what if you do:

  • touch /home/homeassistant/.homeassistant/known_devices.yaml
  • chmod 777 /home/homeassistant/.homeassistant/known_devices.yaml

If it turns out to be permissions then we can revert the 777 and fix it properly.

Edit: You’ll probably need to restart HA after running the above 2 commands.

Hmmm…interesting. I wonder if the topic name could be a clue to my problem? My HA sensor has a defined mqtt_topic of life360/ian but the owntracks MQTT topics that are being updated are:

owntracks/ian lastname/ian lastname
owntracks/gill lastname/gill lastname

i.e. the owntracks MQTT topic names include both my first and last names - presumably picking my full name up from the Life360 web service? I have tried changing the mqtt_topic of my life360 sensor definition to life360/ian lastname and life360\ian lastname\ian lastname respectively, but neither change seemed to make any difference - i.e. neither made the owntracks device tracker visible in my HA console.
Does the mqtt_topic in my sensor definition need to correspond to anything in my Life360 account details?
I did think about permissions being the issue, but think I’ve ruled that out as a cause: as an experiment I added a device tracker using the ASUSWRT platform (my Asus router supports this), and as soon as I added that device tracker the known_devices.yaml file was created by HA and was populated with a list of all of the devices connected to my router. I also saw each device shown as a device at the top of my HA console. That presumably means that HA has whatever permissions that it needs to create and update known_devices.yaml.
[Edit: I tried the touch and chmod commands anyway - followed by a restart - and nothing changed]

Hi I got it working today. One issue I had was that I had put my first name and last name in the name box of life360. I split it cortectly I.e. name and last name separately and then I got the topic owntracks/juan_juan.
I then got device_tracker.juan_juan.

The sensor sample sensor just tells you if the "service " is running.

Hmmm…so you entered your last name in Life360 but got an owntracks topic which consisted only of your first name??
I just noticed this in my log - I’m not sure if that gives any clues to why my device_tracker isn’t being created:

2018-03-15 15:48:11 ERROR (MainThread) [homeassistant.core] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/asyncio/tasks.py", line 180, in _step
    result = coro.send(None)
  File "/usr/local/lib/python3.6/site-packages/hbmqtt/broker.py", line 342, in stream_connected
    yield from self.client_connected(listener_name, StreamReaderAdapter(reader), StreamWriterAdapter(writer))
  File "/usr/local/lib/python3.6/site-packages/hbmqtt/broker.py", line 476, in client_connected
    yield from self.publish_retained_messages_for_subscription(subscription, client_session)
  File "/usr/local/lib/python3.6/site-packages/hbmqtt/broker.py", line 723, in publish_retained_messages_for_subscription
    handler.mqtt_publish(
AttributeError: 'NoneType' object has no attribute 'mqtt_publish'
2018-03-15 15:48:11 DEBUG (MainThread) [hbmqtt.broker.plugins.packet_logger_plugin]  -out-> SubackPacket(ts=2018-03-15 15:48:11.140669, fixed=MQTTFixedHeader(length=3, flags=0x0), variable=PacketIdVariableHeader(packet_id=4), payload=SubackPayload(return_codes=[1]))
2018-03-15 15:48:11 INFO (MainThread) [homeassistant.core] Bus:Handling <Event system_log_event[L]: timestamp=1521128891.142239, level=ERROR, message=Error doing job: Task exception was never retrieved, exception=Traceback (most recent call last):
  File "/usr/local/lib/python3.6/asyncio/tasks.py", line 180, in _step
    result = coro.send(None)
  File "/usr/local/lib/python3.6/site-packages/hbmqtt/broker.py", line 342, in stream_connected
    yield from self.client_connected(listener_name, StreamReaderAdapter(reader), StreamWriterAdapter(writer))
  File "/usr/local/lib/python3.6/site-packages/hbmqtt/broker.py", line 476, in client_connected
    yield from self.publish_retained_messages_for_subscription(subscription, client_session)
  File "/usr/local/lib/python3.6/site-packages/hbmqtt/broker.py", line 723, in publish_retained_messages_for_subscription
    handler.mqtt_publish(
AttributeError: 'NoneType' object has no attribute 'mqtt_publish'
, source=/usr/src/app/homeassistant/core.py>