Better Presence Detection in Home Assistant

If you have a home automation that closes the blinds and turns on the lights at sunset, do you really want it turning on the lights if you’re not home? The most useful home automations are ones that are aware of you being home or not.

There are two people who live in my house – my partner and I, and we both have mobile phones which we generally take with us when we leave the house. Home Assistant provides device tracker sensors that can be used to infer if a person is home or not by working out if their mobile phone via signals provided through the Home Assistant mobile application and GPS. I previously relied on this device tracker to work out if we were home or not but it wasn’t always accurate. Occasionally the phone would report us being in a different location if it couldn’t get a good GPS signal, which would trigger the automation to turn out the lights when we leave, leaving us sitting in the dark in our own living room.

To stop this from happening I needed to find a more accurate way to detect if we were home or not, and this blog post will show you how you can do it in your home too.

Using the Wifi Network Name (SSID)

The most accurate way I found to determine if we were home was to use a combination of GPS location and to find out if my phone was connected to my Home Wireless network. The Home Assistant mobile application can be configured to report the name of the wireless network, or SSID, that the phone is currently connected to. You can then use that information to create a new device tracker sensor based on that information.

To get started, you will need to enable the Wifi network reporting in the Home Assistant mobile application for each person’s phone that you are tracking. To do this:

  1. Open the Home Assistant application menu
  2. Click on App Configuration
  3. Click Manage Sensors
  4. Click on the Wifi Connection sensor under the Network Sensors area
  5. Enable the sensor
Home Assistant Menu
1. Select App Configuration from the Home Assistant menu

2. Select Manage Sensors

Screenshot of Home Assistant mobile application showing WiFi Connection sensor
3. Select the WiFi Connection sensor
Screenshot of Home Assistant Mobile Application showing the Wireless name sensor

4. Enable the WiFi Connection sensor

Once you enable this sensor you will see a new entity in Home Assistant for each mobile app that you enabled with the wireless connection name.

Screenshot showing wireless connection sensor in Home Assistant developer tools

Creating a device tracker using the Wireless network name

Knowing the name of the wireless network that a phone is connected to is only the first step. To make it work easily with automations you will need to create a new device tracker that can tell you if someone is home or not based on that information.

Home Assistant allows you to create new device tracker sensors that can have a value of either home or not_home like the default ones created by the Home Assistant mobile application which use GPS.

To create a new device tracker you need to edit your Home Assistant configuration.yaml file and add the following sensor to the sensors area.

- platform: template
    sensors:
      alans_pixel_4_home:
        value_template: >-
          {% if states('sensor.alan_s_pixel_4_wifi_connection') == "burnsie" %}
            home
          {% else %}
            not_home
          {% endif %}
        friendly_name: 'Alans Pixel 4 Home (SSID = burnsie)'

This will create a template sensor called alans_pixel_4_home. This sensor will check the WiFi Connection sensor from the mobile application and if the phone is connected to my home Wifi network called Burnsie it will set the sensor value to home. If it’s connected to any other wireless network, or no wireless network, it will set the sensor value to not_home.

If you have multiple WiFi Networks you want to test you can wrap the IF block in brackets and add an OR to check as many network SSIDs as you want like this:

- platform: template
    sensors:
      alans_pixel_4_home:
        value_template: >-
          {% if (states('sensor.alan_s_pixel_4_wifi_connection') == "burnsie") or ((states('sensor.alan_s_pixel_4_wifi_connection') == "burnsie-5G")) %}
            home
          {% else %}
            not_home
          {% endif %}
        friendly_name: 'Alans Pixel 4 Home (SSID = burnsie)'

Combining the GPS tracker with the Wifi tracker

Now that you have the original device tracker which uses GPS, and this new WiFi Network based tracker you will need to find a way to combine them. To achieve this I created a new Home Assistant Group called Residents and added the GPS and Wifi based trackers for both my partner and I into this group.

I added this entry to my Home Assistant groups.yaml file

residents:
  name: Residents
  entities:
    - person.h
    - person.alan_byrne
    - sensor.alans_pixel_4_home
    - sensor.h_pixel_5_home

This now creates a new device tracker group that will tell Home Assistant if any of the residents are home or not. Device tracker groups can have the same home or not_home state as any other device tracker. If any of the entities within the group are reported as home, then the whole group is reported as home.

This is a really useful sensor to use in the triggers and conditions of my automations to make sure they only activate when everyone is either home, or away. For example, in the automation that sends us a notification if any doors or windows have been left open when everyone has left the house.

Home Assistant screenshot showing device tracker group called Residents as an automation trigger

There are many advantages of using a group for this:

  1. You only have to update the device trackers for a person in the group if they get a new mobile phone, rather than in every single automation that it is used
  2. You can easily modify the name of the Home wireless network in a single place rather than in each automation
  3. You can add or remove additional people or devices to the group
  4. You only need to add one trigger or condition for the whole house to an automation

I use this residents group in more than half of my automations – it’s one of the most useful sensors I have in my Home Assistant!