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

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.

2 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"

I did try that and as I had to change the trigger to restart I get the following error:

Error: Error rendering data template: UndefinedError: 'dict object' has no attribute 'to_state'
id: '1618388207224'
alias: Presence Giga Red (Duplicate)
description: ''
trigger:
  - platform: homeassistant
    event: start
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

Please look at the differences between what was posted and what you have tried.
trigger.to_state.state is documented as correct.

location_name: |

is not the same as

location_name: >

Homeassistant seems to convert it somehow automatically. And it does work for the other automation and I don’t think this is the problem. I assume the problem is, that the object is missing as the binary sensor is not at all part of this automation.

Ah ok.
I create my automations in YAML so my mistake - that’ll teach me not to try and provide a quick response whilst coding elsewhere. Apologies for the confusion.

You need to use the binary_sensor state (which will persist through restarts) to set the initial device_tracker state on startup.
You can use trigger.to_state.state with the other automation replicating changes from binary_sensor to device_tracker.

I got your point and I guess I am not able to explain myself probably, so I will try again.
In order to replace the device with the sensor data I need to read the sensor data.
Which for the automation that is constantly checking I do with example.

For my second automation the trigger is not the binary sensor but the homeassistant restart. Therefore no value. I tried to replace

trigger.to_state.state with state.binary_sensor.gigaset_red or binary_sensor.gigaset_red.state

both is not working.

It really is very hard to try to help you when you don’t provide the code.

Assuming you mean that you tried replacing

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

with

{% if  state.binary_sensor.gigaset_red == 'on' %}

or

{% if  binary_sensor.gigaset_red.state == 'on' %}

then they don’t work because those aren’t valid tests.

The Developer Tools > Template editor lets you test things like that (except for ‘trigger’ elements) and the documentation explains templating.

For the on state you would want to use:

{% if  is_state('binary_sensor.gigaset_red', 'on')  %}

or possibly

{% if states('binary_sensor.gigaset_red') == 'on'  %}

or (not recommended as explained in the documentation)

{% if states.binary_sensor.gigaset_red.state == 'on'  %}

Will take your advice of sharing code for next time.
Thanks for your help now everything works fine. :pray:t5:
For documentation purpose I am putting everything here in one place.

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

Second one

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

The corresponding esp configuration is

esphome:
  name: bletracker
  platform: ESP32
  board: nodemcu-32s

wifi:
  ssid: "Hotspot"
  password: "Hotspot"

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Emergency"
    password: "Emergency"

captive_portal:

# Enable Home Assistant API
api:

ota:

esp32_ble_tracker:
    
binary_sensor:
  - platform: ble_presence
    mac_address: 7C:2F:80:EA:FC:0D
    name: "Gigaset Red"

I am using Gigaset Beacon as devices

2 Likes

Sorry for bringing this topic back, but I was redirected here by a great help from someone else : MQTT room to device tracker for person - #2 by BlackCatPeanut - Configuration - Home Assistant Community (home-assistant.io)

What does source_type do?

Check out the desription here on the hierarchy of different types of trackers: Person - Home Assistant

I used the MQTT Device Tracker configuration which allows you to define the type: MQTT Device Tracker - Home Assistant (using the Source Type)