The Elephant in the Room: Presence Detection in Home-Assistant

Presence Detection in Home-Assistant/Node-Red

I thought I’d write up a post detailing how we tackle presence detection and how we can run some simple modular automations on the back of it. I see many people asking online in the forums and on facebook which is the best way to handle presence detection, or trying to fault find their own various methods. Before the android and IOS apps came along, presence detection was largely hit and miss. You could use ping, nmap etc to search for devices on your wifi network but if you’d turned your wifi off, or were outside of the home the system pretty much failed. We tried numerous third party solutions too. I was an early adopter for tile, and for a while that was working ok for letting the rest of the family know where I (or my keys) was! However, again, it was pretty hit and miss. I then moved onto Owntracks. Using Cloudmqtt I was able to set up a free MQTT broker in the cloud and point my girlfriend’s apple phone and my android phone at the server using their app. Our phones would broadcast our positions at various intervals. The cloudmqtt server would effectively take in and broadcast our locations. My local node-red installation would also be configured to subscribe to the Cloudmqtt server and so it was able to receive our location. This worked reasonably well. It meant that we didn’t have to punch any holes in the firewall and we could get a reasonably accurate location on each other. That being said, to use Owntracks at its most accurate, the battery life on our devices was terrible. We needed to therefore compromise on the broadcast frequency which in turn made any location based automations prone to error. We needed an alternative.

If you look on the home-assistant integrations page (at the time of writing) there are 65 integrations under presence detection. If I can generalise, I would say these basically fall into two categories. Tracking you on your network or tracking you whilst you’re about. I wanted a solution for the second variant. Enter Life360. At first glance this was just another GPS broadcasting app like owntracks or tile, however, people were really raving about this in the forums. People were using phrases like “it just works” or “not failed me yet.” I downloaded the app on to my phone, created and account and signed in. Once the account was authenticated, I was off. I installed the life360 integration in home-assistant and checked through the yaml. I then went to the map in HA and there my little icon was. It was working fine. I would vpn back into my network to check when I was in town and it always showed my position correctly. I grabbed my girlfriend’s phone, and uninstalled Owntracks and added Life360 instead. Repeated the process with the integration and then she was working in the map too. So far so good. We now have an accurate GPS position, with no noticeable battery drain on our devices. Now what can we do with it?

The goal of all of this was to be able have our home know if we’re home or not. Are we both home? Is either one of us alone? Is everyone out? With the answers to these questions we can begin to work on various automations. For example:

  • If either one of us or both are home, switch the evening lights on around sunset
  • If we’re both out, make sure all lights in the home are off
  • If we’re both out, but I walk through the door, play my favourite internet radio station on our Sonos at a preset level. (you can vary this to play hers if it’s just her on her own, or a different one again if we’re both present).

In order to accomplish this, we needed the following:

  1. A set of input booleans (Andy home, Anya home, Both home)
  2. Correct coordinates set up to illustrate home (I created “home” both in life360 and I already had this in home-assistant – make sure you’re coordinates are accurate via Google maps.). I wont go into detail about this here, it’s self explanatory in Life360 and one of the first things you do when you set up HA.
  3.  Some simple node-red flows to read from and write to the input booleans in home-assistant setting the status.
  4. The rest of the automation to be triggered.

So let’s get started.

  • Input Booleans – enter this in your configuration.yaml (assuming you don’t have input booleans already).
input_boolean:
  andy_home:
    name: Andy Home
  anya_home:
    name: Anya Home
  home:
    name: Home

Then in your known_devices.yaml file you should see this come up:

life360_YOURUSERNAME:
  hide_if_away: false
  icon:
  mac:
  name: life360_YOURUSERNAME
  picture:
  track: true
Make sure that you have track set to true. Without it, you won’t be able to see the life360 entity under device tracker.
Then go in the Home-Assistant sidebar, go to Configuration>Persons, and then assign the device tracker to the person. In my instance, the top entity is my android phone (running the native Home-Assistant app), the second entity is my Life_360 entity.
Person Entity Tracking
Person Entity Tracking
Do the above for each person you want to track in your household.
Still with me? The final step in home-assitant is to add a card in lovelace just so we can visualise what’s going on with the booleans. You don’t actually need this but I find it useful for debugging.
Lovelace Glances Card
Lovelace Glances Card
  • Correct Home zones – As I mentioned above, i coded this manually in my configuration yaml i.e. long/lat/height. In life360 you just add a “place.” Be accurate in your placing of the pin here as it will be used to calculate if you’re home or not.
  • Simple node-red flows to detect who’s actually home or not.

Now it’s time to dig into Node-red to see how we can put these input booleans to good use. There’s probably many different ways of doing this, but I prefer the KISS principle (Keep It Simple Stupid). It makes debugging a whole lot easier.

Let’s start with a simple flow. We will poll the life360 entity to see if I am home or not. If I am home then turn on the input boolean, if away, turn it off.

Simple home or away flow
Simple home or away flow
event state node
event state node

 

input boolean code
input boolean code

In order to turn it off if i’m not home, you just need to change the service from “turn_on” to “turn_off”. Next, I need to do this for Anya. So I repeat the process, only changing the life360 id to her entity and changing the input_boolean to anya_home. You should now have a flow that resembles the following:

Input booleans for both
Input booleans for both

Next step is to now introduce our third input boolean which tells us is the home occupied or are both of us away? In order to do that, we need either of our individual “home” states to be plumbed in as inputs to this third input boolean. That’s simple enough, but slightly more complicated is that if either one of us is away, we need to check that the other one is absent also to be able to declare that the nobody is home. It’s kind of tough to write out so I’ll just show you the flow:

The full flow
The full flow

Now don’t be put off by this. It’s simple to follow. If we look at “Either of us home?” – if either myself or Anya are home (according to our life360 entities) then either one will “turn_on” the input_boolean.home that we created earlier. You should be able to recreate this from the coding i’ve already shown above. Now the slightly trickier bit is that if one of us is away, we need to check the state of the other person’s input_boolean to see if they’re away too. You can see that I’ve attached the string to the bottom output of the “Check Anya Home” node. This is the negative, so for condition, I’ve used on. So if Anya is indeed home according to her input boolean, then the top option will flow (which goes nowhere). If her input_boolean is off, then the bottom option will trigger (which will go forward to turning off the “Both Away” node (input_boolean.home). See the coding below:

is anya home?
is anya home?

 

Now all you need to do is reverse the inquiry so that If Anya is away, Node-Red check to see if Andy is home or not via the same logic.

If you’re still with me, you have basically completed presence detection for two people. That flow above, the coding in HA is all you need to be able to turn the input_booleans on and off according to if either (or both) of us are home or away. I suggest that you also add some notification points to the flow for testing purposes. I did this by firing off service calls to my phone when I was home or away and if both of us were away. An example of such a service call is below:

service call + flow
service call + flow
Notification
Notification

With any luck over the next day or so as you start to move around the home you should be getting notifications on your chosen device to test it’s working. Now that we have gotten this far, it’s time to look at useful automations. I’ll illustrate a simple one for you with the help of bigtimer. Now the flow below turns on all of our lights I’ve selected, at sunset, if either one (or both) of us are at home. I have purposely left the negative output of the switch node (i.e. “off”) empty, because I didn’t want the lights to be turned off by bigtimer, as we have our own bedtime routines that take care of that for us. You can learn more about that here.

big timer flow
big timer flow
big timer config
big timer config
simple switch node
simple switch node
input boolean home
input boolean home
service call switches
service call switches

Just a quick note. On the final node, I am using just one service call to turn on several entities. It’s quicker to use switch.1, switch.2 etc than to have each individual call service node per entity. You can certainly do it that way if you wish.

There you have it! You should now have decent presence detection up and running, as well as some time automated lights. I hope this will be useful for you. If you have any comments or questions please let me know. Maybe let us know in the comments what cool automations you’ve come up with alongside presence detection, or if you know of a more simpler way, then enlighten us!

If you did find this useful, and would consider supporting the blog, please take a look at my most recent book. If you’re considering a renovation or looking for home-automation ideas, you might find it useful. More details can be found here.

Cheers!

 

 

 

Comments

  1. I amonly seeing a subscription service for Life360, is there a free version that just tracks GPS location?

  2. Excellent summary, not something we are very used to when it comes to NodeRed user guides! Thanks for taking the time 🙂
    A short question about the NodeRed implementation: How big is actually the performance impact when using input booleans to track if a user is home instead of directly polling the location tracking item for status when required?

    1. Thanks for that. I hope it’s useful. To be honest in terms of latency I don’t see much. I’m running Home-Assistant supervised (aka Hassio!) and have node-red installed as an add-on inside that. The installation is sitting in a 6th gen celeron nuc, and i live in a fairly decent 4g/lte area. I don’t notice any real delays. I intially tested the set up by putting my phone in airplane mode whilst returning home and once inside the “perimeter” I turned on the reception. The test lamp i set up switched on literally as soon as i caught signal. As node-red is effectively managing the input booleans as well as controlling the service calls to turn on/off the lights i’d think there’s very little additional overhead. I hope that kind of answered your question, i do tend to go off on a tangent at times 🙂

  3. Just set this up and it works great, for the most part.

    I added a notification after “both are away” call service node and it kept sending me notifications while me and my wife were driving. It would send a notification when we stopped at a stop light and when we started moving again. Life360 was updating the state from driving to away back to driving. It was a fun 45 minute drive since I used critical notifications. This really makes no difference since the input booleans had the correct value. I just wonder if this constant state change will clog things up in any way.

    1. Strange. I never experienced any kind of false alerts or anything stemming from life360 driving/not driving status. Are there any settings in life360 which could be causing that? I haven’t even checked the app for probably 4-5mths. This for me (touch wood) was set and forget!

  4. Hi. Great post. Thank you for this.
    What I didn’t understand is, how is this different from the built-in GPS tracking of the companion app? Do we really need an extra service to do this?
    Also, can we combine this with a WiFi tracking? If connected, home. If not, check GPS.

    Thank you again

    1. Hi there sorry about not replying, just saw your comment. You can indeed rely on the companion app alone, but our experience with the app has been somewhat hit and miss. My android phone works without issue, but my girlfriend’s iphone doesnt always update correctly. We also have companion apps on our ipads and that too can cause issues if we forget to disable tracking there. With life360 it just worked straight out of the box (at least for me). You know what they say, if it ain’t broke…. !

Leave a Reply to OlivierCancel reply