Using the my WiFi SSID for presence detection

I’m fairly new to HA, I just finished setting up a docker image and I’ve integrated most of my automation hardware. So far so good.

I’m using our phones GPS for presence detection, with the phone GPS tracking device assigned to each “People” object. However it’s not 100% reliable, so I thought I’d add a second “Track Device” for precense detection. I came up with this (in my configuration.yaml) in order to track if our phones are connected to our home WIFi network or not:

sensor:
  - platform: template
    sensors:
      my_phone_wifi_tracker:
        friendly_name: My Phone WiFi Tracker (SSID = MySSID)'
        icon_template: >
          mdi:wifi
        value_template: >
          {% if (states('sensor.my_phone_ssid') == "MySSID") or ((states('sensor.my_phone_ssid') == "MySSID-5G")) %}
            home
          {% else %}
            not_home
          {% endif %}

The resulting sensor, my_phone_wifi_tracker, changes state to and from “home” and “not_home” as expected. But, HA won’t let me assign that sensor to a “People” object to be used as a “Track Device”. I assume because HA does not recognize the sensor as a “Track Device”.

I’ve looked high and low for an answer before posting here, but without finding any solution.

How can I define this sensor in a way such that it will be recognized by HA as a “Track Device” that I can assign to a “People” object?

4 Likes

There’s a custom component that can work with binary sensors: GitHub - pnbruckner/ha-composite-tracker: Home Assistant Composite Device Tracker

You can also create an automation that will update an existing device tracker based on the new state: Is there a way to use a binary_sensor as an device_tracker? - #2 by TazUk

You can use the logic you have in an automation and create a device tracker manually and use device_tracker.see service to set the device tracker to home or not home.

Was just going to say the same. See here for examples

Thanks all for your input, much appreciated!

I decided to go the “Composite Device Tracker” route to combine both the phone’s GPS tracker and the WiFi SSID tracker into one. I plan on eventually adding BLE presence detection as well and then I could add that to the tracker_device as well.

Anyhow, this is what I added to my configuration.yaml:

device_tracker:
  - platform: composite
    name: person1_home_or_away
    time_as: device_or_local
    require_movement: true
    entity_id:
      - sensor.person1_iphone_wifi_tracker
      - device_tracker.person1_phone

    name: person2_home_or_away
    time_as: device_or_local
    require_movement: true
    entity_id:
      - sensor.person2_wifi_tracker
      - device_tracker.peson2_phone

Now this creates the expected Composite Device Trackers device_tracker.person1_home_or_away and device_tracker.person2_home_or_away

The only problem is that only the second (device_tracker.person2_home_or_away) seems to work, it will correctly change state based on the values of sensor.person2_wifi_tracker and device_tracker.peson2_phone.

The first Composite Device Tracker (device_tracker.person1_home_or_away) always shows as “not_home” with source_type:null

Now if I change the order of the definition of the two Composite Device Trackers in my configuration.yaml like this:

device_tracker:
  - platform: composite
    name: person2_home_or_away
    time_as: device_or_local
    require_movement: true
    entity_id:
      - sensor.person2_wifi_tracker
      - device_tracker.peson2_phone
    
    name: person1_home_or_away
    time_as: device_or_local
    require_movement: true
    entity_id:
      - sensor.person1_iphone_wifi_tracker
      - device_tracker.person1_phone

then the other Composite Device Trackers is working instead (but still the last one to be defined), and device_tracker.person2_home_or_away always shows as “not_home” with source_type:null.

So it seems like only the second instance of a “Composite Device Tracker” I define is actually working. And I had to add a in after the second instance in configuration.yaml for the second one to even show up in HA. This is the last entry in my configuration.yaml by the way. It would pass “Configuration Validation” without the , but the last entry didn’t show up. So it seems it’s a bit picky of the file formatting by requiring an at the very end of the file, at least if Composite Device Trackers are defined at the end.

I tried to define a third one as well, then the first one I defined didn’t show up at all, the second showed up but didn’t work and the third and last worked as expected.

Now I’m completely new to HA, but this seems strange to me. Any idea why only the second (last) instance of “Composite Device Tracker” I define actually works? Can I only define one at a time? If that’s the case then that does not make any sense either.

Please bear with me if I made a trivial error, I only installed HA this week and I’m still learning. :slight_smile:

1 Like

I finally figured it out, had to add “- platform:” for each instance, eg:

device_tracker:
  - platform: composite
    name: person1_home_or_away
    time_as: device_or_local
    require_movement: true
    entity_id:
      - sensor.person1_iphone_wifi_tracker
      - device_tracker.person1_phone

  - platform: composite
    name: person2_home_or_away
    time_as: device_or_local
    require_movement: true
    entity_id:
      - sensor.person2_wifi_tracker
      - device_tracker.peson2_phone

It all works great now. Not really used to the syntax yet. :slight_smile:

Edit: Well, it works great NOW, turned out I had to change my sensor that checks my SSID to a binary sensor for for the “composite” component to actually use it.

5 Likes

Hi, this is awesome! but…when you say sensor.my_phone_ssid, sensor must be replaced by my phone entity id? otherwise, how to locate the device? Thanks in advance!

Hi, sensor.my_phone_ssid is my phone, ok, but how? sensor.myphoneid? how can I identify my phone?

I’m not sure if you got he answer elsewhere already but it’s the name you give in the HomeAssistant App on your phone, under settings> companion app > device name.
e.g. mine is sensor.daves_phone_wifi_connection

1 Like

Hi , can you give me please the example of the binary sensor ?

I am not sure if this is the best way,

but this is what I use in my configuration.yaml, which works perfectly for me.

template: 
  - binary_sensor:
      - name: s10_connect_home_wifi_tracker
        state: >
          {% if (states('sensor.my_phone_wifi_connection') == "Your Wifi SSID") %}
            on
          {% else %}
            off
          {% endif %}

Best regards.

2 Likes

Sorry for the late response…

Look under “Developer Tools → States” and you should be able to find your phone’s wifi connection/SSID “tracker”. You might have to enable this sensor in the HW companion app.

Here is an example of my binmary sensor:

binary_sensor:
  - platform: template
    sensors:
      my_phone_wifi_tracker:
        friendly_name: My Phone WiFi Tracker (SSID = MySSID)'
        icon_template: >
          mdi:wifi
        value_template: >
          {% if (states('sensor.my_phone_ssid') == "MySSID") or ((states('sensor.my_phone_ssid') == "MySSID-5G")) %}
            home
          {% else %}
            not_home
          {% endif %}

So just like in my very first post above, except binary_sensor: instead of sensor:

2 Likes

You could also do it like this:

binary_sensor:
  - platform: template
    sensors:
      my_phone_wifi_tracker:
        friendly_name: My Phone WiFi Tracker (SSID = MySSID)'
        icon_template: >
          mdi:wifi
        value_template: >
          {% if "MySSID" in states('sensor.my_phone_ssid') %}
            home
          {% else %}
            not_home
          {% endif %}

Is’t that the same as in my example, except that I’m comparing with two possible SSID’s since I have more than one network I might connect to at home?

Not really.
If the names are the same then you only need one if and no or.

Your example assumes that the SSID are the same, except that one ends with “-5G”. That’s not the case.

And besides, I much prefer the IF & OR approach for clarity. Coding style is a matter of taste I guess.

So does your example:

It’s just an example, I should know what my SSIDs are. And my example does not assume anything, they are examples for those who follow to put in their own SSIDs.

Hi, any idea please why I’m getting OFF from binary sensor while template is showing home? When I turn off wifi, template is not_home, but binary sensor is still off. Thank you.