Presence with HomeAssistant

When I decided to switch from SmartThings to HomeAssistant I needed a way of tracking whether I’m home or not for my automations.

As I said in my previous post, the SmartThings keyfob was ideal for presence detection. The range and responsiveness were sufficient that it picked up that I was home before I got to the front door, making it suitable for alarms – you don’t want an alarm every time you get home just because the presence sensor hasn’t reacted quickly enough and you’ve already opened the door before it realises you’re home.

In order to properly evaluate options, I needed to know exactly when, for a given presence detection method, HomeAssistant thought I was home.

I use Pushover for notifications, so I set up a quick automation to send a notification when I went out and when I got home:

- alias: "Gone out"
    - platform: state
      entity_id: group.all_devices
      from: 'home'
      to: 'not_home'
    - service: notify.pushover
        message: "Gone out"

- alias: "Got home"
    - platform: state
      entity_id: group.all_devices
      from: 'not_home'
      to: 'home'
    - service: notify.pushover
        message: "Got home"

This would tell me if HomeAssistant was correctly picking up on my presence – or to be more precise, the location of my phone as all of the options I looked at were based on detecting its presence or precise location.

HomeAssistant has a number of network methods for detecting presence. For example, I could configure my router to give the phone a fixed IP address and use the ping component to detect it or use Nmap to scan the network for it. There is also a component that can log on to my router and check whether the phone is connected.

The problem with all of them is Android’s “doze” feature which is designed to preserve battery life. When the phone isn’t moving and hasn’t been used for a while it goes into a deeper sleep, which disconnects it from the network making HomeAssistant think I’ve gone out.

The other methods all rely on geolocation – i.e. using an app to report the phone’s location back to HomeAssistant and using the zone feature to determine whether the phone is home.

I was reluctant to go down that route. I had been using Tasker’s location methods to run some automations based on location – turning off wifi when I went out, for example, and switching the phone to silent mode when I got to work. This method was fine for those needs but not much use for home automation. Tasker reports location on an interval, so the automations would often run several minutes after I got home or went out. Reducing the interval would help, but at the expense of greater battery use.

Battery use also led me to switch them off altogether after my phone was upgraded to Android 7. For some reason, after the upgrade I found my battery life was much worse. In particular it was draining much faster overnight when the phone wasn’t being used. Trial and error showed that it was Tasker’s location based tasks. I can only assume that Tasker must have been preventing doze mode from working.

Despite my reservations, I decided to try GPSLogger. Despite the name, it does not need to rely on GPS. It has three options for detecting location – GPS, Network and Passive.

Passive is the interesting option. This ties into an Android API that notifies GPSLogger when another app has requested the location. In other words it waits to be told the location rather than actively asking for it.

This is extremely battery efficient but only works if you have other apps that are already asking for the location.

I already have location history enabled on my phone. Yes, I know, I’m telling Google all of my movements but I figure what the hell – Google already knows enough about me.

It turns out that Google has achieved a reasonable balance with their location tracking – it’s pretty accurate, but not to the extent that it drains the battery, especially if you use the battery saving locating method.

In particular, it doesn’t seem to cause battery drain overnight when the phone isn’t moving, so I’m guessing that it works in conjunction with other sensors in the phone to detect when there has been sufficient motion to warrant checking whether the location has changed.

With location history turned on and getting the location for its own use, GPSLogger gets frequent enough updates and is accurate enough.

Within HomeAssistant, I have a zone configured that is centred around my house, and uses the default radius of 100m:

I don’t actually live here…

That means that if I’m within 100m of my house I’m considered to be “home” by HomeAssistant.

With geolocation based on networks and wifi I usually get an estimated accuracy of between 20m and 40m, and HomeAssistant seems to be reliably getting an update that it will interpret as me being at home before I reach the door.

I might still be over 100m away but that doesn’t matter. I’d rather HomeAssistant thought I was home a little ahead of my arrival than a little after, especially if I’m using it to generate alarms when doors open. Conversely it doesn’t matter if it doesn’t register me as away until I’m over 100m from home

It’s worth mentioning that If you’re using network methods for location, the accuracy will vary according to how many cell towers are within range. If you live in a built up area then network based geolocation is likely to be much more accurate than if you live out in the sticks.

With presence detection working I can now finish converting my SmartThings automations into HomeAssistant. I can even create some that were not possible when I was simply detecting presence using the keyfob, such as setting up a zone for work and turning on the heating at home when I leave the office.

in Home Automation

Add a Comment

Your email address will not be published. All comments will be reviewed.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Posts