Combining multiple device trackers into one using MQTT

Took me a while to figure it out and I know some people were looking for this, so sharing my config, hopefully you’ll find it useful.

My setup uses iOS application with few predefined zones and custom ping script that will change location to home as soon as the device connects to your wifi (which is almost instantly).

Using this setup you can combine as many sensors as you want and finally properly track location.

configuration.yaml

mqtt:

device_tracker:
  - platform: mqtt
    devices:
      cezary: '/location/cezary'

known_devices.yaml (you have to add this manually)

cezary:
  hide_if_away: false
  icon:
  mac:
  name: Cezary
  picture: 
  track: true
  vendor:

sensors.yaml

- platform: command_line
  name: ping_cezary
  command: 'dir/presence.sh 192.168.1.20'
  scan_interval: 5

presence.sh (custom ping script)

#!/bin/bash
/sbin/ping -c5 -t5 -W100 -n $1 &>/dev/null
if [ $? -eq 0 ]
then
	echo -n "home"
else
	echo -n "not_home"
fi

And finally automations:

- id: cezary_home
  trigger:
    - platform: zone
      entity_id: device_tracker.ios_iphone_cezex
      zone: zone.home
      event: enter
    - platform: state
      entity_id: sensor.ping_cezary
      to: 'home'
  condition: state
      entity_id: device_tracker.cezary
      state: 'Away'
  action:
    - service: mqtt.publish
      data: { "payload": "Home", "topic": "/location/cezary" }

- id: cezary_away
  trigger:
    - platform: zone
      entity_id: device_tracker.ios_iphone_cezex
      zone: zone.home
      event: leave
    - platform: state
      entity_id: sensor.ping_cezary
      to: 'not_home'
      for:
        minutes: 30
  action:
    - service: mqtt.publish
      data: { "payload": "Away", "topic": "/location/cezary" }
4 Likes