WiFi sensor into device tracker

1 minute read

Something that makes for a great home/away tracker is the Android and iOS app’s connected WiFi sensor, or at least it would if it was a device tracker.

Well, you can turn it into one, and I mentioned it before, but it’s lost in the depths of that post so I thought I’d float it out into it’s own article.

You’ll need a working MQTT setup for this, as well as remote access for Home Assistant (or the phone can’t report that it’s not connected to your WiFi).

Android App

I use the connected WiFi sensor. This identifies the name of the WiFi network we’re connected to. If you need more granularity there’s also the bssid sensor. These only exist for the Android app, so iOS users can stop reading here. iOS also has a sensor for this, which is sensor.YOUR_PHONE_ssid.

Automation

You can find the automation here. The example below runs any time the sensor updates.

If we’re connected to our Wifi (name Cogs-n-Gears) then it publishes home to the topic, otherwise it publishes not_home. I retain the topics so that they’re always available, for example if I reload the MQTT integration or restart HA.

automation:
  - id: 'YOU_wifi_status'
    alias: 'YOU WiFi status'
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: sensor.YOUR_PHONE_wifi_connection # or for iOS sensor.YOUR_PHONE_ssid 
        to: ~
    action:
      - choose:
        - conditions:
            - condition: template
              value_template: "{{ 'Cogs-n-Gears' in trigger.to_state.state }}"
          sequence:
            - service: mqtt.publish
              data:
                topic: location/YOU_wifi
                payload: 'home'
                retain: true
        default:
          - service: mqtt.publish
            data:
              topic: location/YOU_wifi
              payload: 'not_home'
              retain: true

I use in in the template for a simple reason, I have both Cogs-n-Gears 2 for 2.4 GHz band and Cogs-n-Gears 5 for the 5 GHz bands.

MQTT Device Tracker

You can find that here too:

mqtt:
  device_tracker:
    - name: "person YOU wifi"
      state_topic: 'location/YOU_wifi'
      source_type: router
      unique_id: "mqtt_person_YOU_wifi"

There’s nothing fancy here, we set it to router as that’s the closest match (the other options are gps, bluetooth, and bluetooth_le). This matters for the person you link it to. We add a unique_id so that you can easily customise it in the UI.

Now you can link the device tracker to your Person, and have a fast reliable home/away detection.