[Updated] Home Assistant presence detection with Google WiFi

[Updated] Home Assistant presence detection with Google WiFi

Previously my presence detection in Home Assistant works through Asus router and asuswrt component. And it was bad. Very bad. Recently I switched to Google WiFi and realized that the only way to implement presence detection with this router is IFTTT (Update: No). I thought it would be the slowest presence detection in the world but I was wrong.

While you still can do this through IFTTT you need to know that there is much easier way not depending on your router model and any 3rd party services – Nmap Tracker. You just enable this component and configure your devices IP addresses.
I configured static IP addresses for devices I wanted to track using DHCP IP Reservation on Google WiFi router.

If you still want to use IFTTT, you are welcome to continue reading.

Home Assistant IFTTT webhook

First of all, we need to create a webhook for IFTTT in our Home Assistant instance through Integrations. Go to “Configuration” -> “Integrations” in the Home Assistant web UI. Find “IFTTT” in the “Set up a new integration” section and click “CONFIGURE”. You will be provided with a webhook URL to use in IFTTT applets. It should look similar to this:

https://ha.mydomain.com:8123/api/webhook/e7cd74856399e8934b8f5beffeeeaee4c351cdc8373647585ec040c7b69c2b999

Save this URL somewhere because you will not be able to see it again in your Home Assistant UI.

Home Assistant Entities

For presence detection, I’m using binary_sensors with device_class: presence. Also, the state of those sensors depends on the corresponding input_boolean. It is made to be able to switch someone’s presence on and off manually by adding input_boolean to UI and switching its state. Don’t set the initial state for input_boolean. It will make it possible to save and restore its state on HA reboots. Also, I set these sensors to have different pictures depending on their presence.

input_boolean:
    is_yegor_home:
      name: “Yegor’s presence”

binary_sensor:
  - platform: template
    sensors:
      presence_yegor:
        friendly_name: "Yegor"
        value_template: "{{ is_state('input_boolean.is_yegor_home', 'on') }}"
        device_class: presence
        entity_picture_template: "/local/yegor_picture_bw.jpg"

IFTTT applets

Next. There is an official Google WiFi service in IFTTT and it can create IF’s like “If some device connected/disconnected”. That’s exactly what we need.

Open IFTTT website and log in with your account. Go to “My Applets” and click “New Applet” in the upper right corner.

Then hit “+this”, search for “Google WiFi” and click it. You will be asked to authorize IFTTT in your google account. After that you will be able to select one of the triggers Google WiFi can send to IFTTT:

Let’s start with “Device connects”. Click it and select the device name you want to track in the next step.

Next, you will be asked to create an action by clicking “+that”. For action, you need to find the “Webhooks” service and choose the only action it provides – “Make a web request”.

Now we need to fill all the fields for web request:

URL: [The url of our Home Assistant webhook we created recently]

Method: POST

Content Type: application/json

Body: { "action": "call_service", "service": "input_boolean.turn_on", "entity_id": "input_boolean.is_yegor_home"}

Handling IFTTT requests

The last thing we need is to create a handler in Home Assistant for web requests from IFTTT. It will be new automation:

- id: 'ifttt_webhook'
  alias: IFTTT Webhook
  trigger:
    platform: event
    event_type: ifttt_webhook_received
    event_data:
      action: call_service
  action:
    service_template: '{{ trigger.event.data.service }}'
    data_template:
      entity_id: '{{ trigger.event.data.entity_id }}'

This automation will handle any requests from IFTTT that will have `action: call_service` in their body with `service` and `entity_id` in it.

For disconnecting from Google WiFi we need to create another applet in IFTTT. It will be the same except for two things:

  1. We should select “Device disconnects” from Google WiFi services when creating “+this” for applet.
  2. We need to replace input_boolean.turn_on with input_boolean.turn_off in web request’s “Body” field when creating “+that” to make it looks like this:
{ "action": "call_service", "service": "input_boolean.turn_off", "entity_id": "input_boolean.is_yegor_home"}

Automations based on presence

Assuming we’ve set up presence detection for everyone living in our home, we now need one variable to know if there is nobody home now. It would be helpful when creating automation that should be triggered when there is nobody home or when someone is back home. So we will add all our presence sensors in a group in groups.yaml:

presence:
    entities:
        - binary_sensor.presence_someone
        - binary_sensor.presence_yegor
        - binary_sensor.presence_someone_else

Now we can rely on that group’s state to check if there is nobody home. I’ve added a delay of 10 minutes to make sure this automation will not be triggered when I simply reboot my device or there was a short connection lost to a router.

- id: 'nobody_home'
  alias: 'Nobody home'
  trigger:
  - entity_id: group.presence
    for:
      minutes: 10
    from: 'on'
    platform: state
    to: 'off'
  condition: []
  action:
  - data:
      message: “Looks like there is nobody home now”
    service: notify.push

That is all for now. Thanks for reading.