Help using Hass.io on Pi to do bluetooth device tracking over MQTT

I’m running HA on an Intel NUC in Docker. I’m trying to use one or more Raspberry Pi’s as “remote device trackers”, sending status to my main HA instance over MQTT. I figure this should be pretty straightforward (and probably a popular use case) but I just can’t figure it out.

So the “remote” HA instances will run Hass.io on a RPi3. I want to place one or more of these in the house to detect Bluetooth (and maybe BLE) devices with Device Tracker. I have all this set up and running now. My main HA instance is up and running fine too.

I guess I’m just not getting my head around MQTT. How do I tell the Hass.io instance to send device tracker messages to the main HA instance? I can run Mosquitto or the embedded MQTT broker. I don’t care. But I don’t know how to get it to work.

What do I add to the config to “subscribe” to messages? What do I add to “publish”?

Thanks so much! I feel like such an idiot for not being able to figure this out!

I’ve gotten a little further here but I’m still not sure if I’m on the right track… Here’s my progress.

First, the Hive MQ MQTT Essentials series is a great primer on the protocol.

Next, I had some success replicating @Vasiley’s persistence.yaml work - this really helped me figure out how to do some cool stuff with MQTT. His persistence_publish code inspired me to put together a functional mqtt.publish for device tracking:

 - alias: 'Publish Device Tracker States to MQTT'
trigger:
  - platform: event
    event_type: state_changed
condition:
  - condition: template
    value_template: "{{ trigger.event.data.entity_id.split('.')[0] == 'device_tracker' }}"
action:
  - service: mqtt.publish
    data_template:
      topic: "fsp1/btdt/{{ trigger.event.data.entity_id|replace('.', '/') }}"
      payload: "{{ trigger.event.data.new_state.state }}"

Using state_changed as a trigger is a great idea! I’m checking to make sure it’s a device_tracker event then sending it to my MQTT server using a custom topic prefix (fsp1/btdt/). So a device entry appears like “fsp1/btdt/device_tracker/stephen_macbook” in MQTT.

This is going to a Mosquitto instance running in a container on my main HA server.

Then I have the main server importing these using the device_tracker.mqtt platform. This is really not ideal since it’s hard coded per device. I would love to do something like Vasiley’s import stuff but I just couldn’t get it working. I would appreciate suggestions!

In the mean time, here’s my simple code from configuration.yaml:

  - platform: mqtt
    devices:
      fsp1_stephen_macbook: 'fsp1/btdt/device_tracker/stephen_macbook'

This works. I don’t love it. I would appreciate assistance in getting a more general solution up and running. And ideas about any issues with my code!