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:
- A set of input booleans (Andy home, Anya home, Both home)
- 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.
- Some simple node-red flows to read from and write to the input booleans in home-assistant setting the status.
- 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).
Then in your known_devices.yaml file you should see this come up:
- 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.
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:
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:
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:
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:
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.
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.