Is there a way to use a binary_sensor as an device_tracker?

Hi! I use ESPHome for device tracking some BLE devices and it works very well. The problem is, that ESPHome uses the binary_sensor entity (with on and off as status) instead of device_tracker. The person component of Home Assistant doesn’t allow binary_sensors for device tracking. Is there a way to convert a binary_sensor into a device_tracker entity?

1 Like

You need the device_tracker.see service.
It will create a known devices file with trackers and update those entities. The created trackers can be added to a person.

Redacted example:

# additional device tracker dynamically created
# based on binary_sensor presence template sensors to defeat Unifi wrapper logic
- id: presence_plus_refresh
  alias: "Update presence plus device tracker"
  mode: queued
  trigger:
    - platform: state
      entity_id: binary_sensor.presence
      from: "off"
      to: "on"
    - platform: state
      entity_id: binary_sensor.presence
      from: "on"
      to: "off"
  condition: []
  action:
    - service: device_tracker.see
      data:
        dev_id: "tracker1"
        location_name: "{{ 'home' if is_state(trigger.entity_id, 'on') else 'not_home' }}"
        source_type: "router"

2 Likes

You can replace this:

  trigger:
    - platform: state
      entity_id: binary_sensor.presence
      from: "off"
      to: "on"
    - platform: state
      entity_id: binary_sensor.presence
      from: "on"
      to: "off"

With:

  trigger:
    - platform: state
      entity_id: binary_sensor.presence

You can also catch the unknown state with this method:

    - service: device_tracker.see
      data:
        dev_id: "tracker1"
        location_name: >
          {% if trigger.to_state.state == 'on') }}"
            home
          {% elif trigger.to_state.state == 'off') }}"
            not_home
          {% else %}
            unknown
          {% endif %}
        source_type: "router"
4 Likes

Thanks for your quick solution. If i get this right, I need an existing device_tracker.tracker1 entity id and the above automation just changes its state, right?

I ran the device_tracker.see service call manually through developer tools > Services to create each device tracker entity - this should create and populate the known_devices file entry.
I then added the tracker(s) to each person as required and deployed the automation.

1 Like

perfekt, thank you! This is my final solution:

- id: presence_plus_refresh_tile_autoschlussel
  alias: "Update presence plus device tracker Tile Autoschlüssel"
  mode: queued
  trigger:
    - platform: state
      entity_id: binary_sensor.tile_autoschlussel
  condition: []
  action:
    - service: device_tracker.see
      data:
        dev_id: "tile_autoschlussel"
        location_name: >
          {% if trigger.to_state.state == 'on' %}
            home
          {% elif trigger.to_state.state == 'off' %}
            not_home
          {% else %}
             unknown
          {% endif %}
        source_type: "router"
2 Likes

Thanks for this! Used the same format to set up a presence bridge from HomeKit, using an input boolean and automation within HomeKit to toggle it. This method works great for that.

There are some syntax errors in your last part. It should look something like this


    - service: device_tracker.see
      data:
        dev_id: "tracker1"
        location_name: >
          {% if trigger.to_state.state == 'on' %}
            home
          {% elif trigger.to_state.state == 'off' %}
            not_home
          {% else %}
             unknown
          {% endif %}
        source_type: "router"
1 Like

Well spotted.

Very cool solutions, thanks a lot!
For people like me who struggle with the syntax once in a while, this is how it looks for the HA Version 2021.3.4 when you go to Automation and then yaml mode.

alias: Presence Giga Red
description: ''
trigger:
  - platform: state
    entity_id: binary_sensor.gigaset_red
condition: []
action:
  - service: device_tracker.see
    data:
      dev_id: gigagregor
      location_name: |
        {% if trigger.to_state.state == 'on' %}
          home
        {% elif trigger.to_state.state == 'off' %}
          not_home
        {% else %}
           unknown
        {% endif %}
      source_type: router
mode: queued
max: 10

I noticed that after the restart of Homeassistant the status of the person changes to away eventhough the binary sensor is still on. Any suggestions?

If the binary_sensor hasn’t changed since the restart then I assume the device_tracker won’t be set after a restart.
it’s a long time since I setup our presence automations, but I included a set of automations that run on

trigger:
    - platform: homeassistant
      event: start

to determine the startup state of the custom presence descriptions we use.

When I added the device_tracker.see trackers I also added a call to the service for each device_tracker to set the initial state from the relevant binary sensor.

3 Likes

thanks for the reply. I the current version I am getting an integration not found error.

For what integration? Can you provide code, logs, and what version you are running?

What I am trying to do is creating a second automation, that if homeassistant is restartet the automation is triggered. Just writing it down the independent from the syntax also the logic is not really working is it? So what is the suggest how to adjust this code:

alias: Presence Giga Red
description: ''
trigger:
  - platform: state
    entity_id: binary_sensor.gigaset_red
condition: []
action:
  - service: device_tracker.see
    data:
      dev_id: gigagregor
      location_name: |
        {% if trigger.to_state.state == 'on' %}
          home
        {% elif trigger.to_state.state == 'off' %}
          not_home
        {% else %}
           unknown
        {% endif %}
      source_type: router
mode: single
max: 10

Use the start event for HomeAssistant per my post above, setting the device tracker state based on the state of the binary sensor.

As part of the Automation you mean ? I am not able to include the trigger of homeassistant start. As I get the platform error as mentioned.

Perhaps you should post some code, because the start event is a completely standard automation trigger listed in the docs and I’ve been using it for a number of years.

Now that I opened my eyes I was easily able to find the right trigger.


As I was not able to get it working with a template my solution looks like this:

alias: Presence Giga Red Restart
description: ''
trigger:
  - platform: homeassistant
    event: start
condition:
  - condition: state
    entity_id: binary_sensor.gigaset_red
    state: 'on'
action:
  - service: device_tracker.see
    data:
      dev_id: gigagregor
      location_name: home
      source_type: router
mode: single
max: 10

How do I do it more elegant with the template you mentioned before.
I guess I have to somehow replace this part

{% if trigger.to_state.state == 'on' %}

Per the earlier posts in this thread

e.g

    - service: device_tracker.see
      data:
        dev_id: "gigagregor"
        location_name: >
          {% if trigger.to_state.state == 'on' %}
            home
          {% elif trigger.to_state.state == 'off' %}
            not_home
          {% else %}
             unknown
          {% endif %}
        source_type: "router"