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

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)