Home Assistant MQTT Device Tracker

Hello everbody, this is my first post here.
I just added my T-Beam to Home Assistant via MQTT to get battery and messages information.

 - name: "Node 2ed0 Battery Percent"
    unique_id: "node_2ed0_battery_percent"
    state_topic: "homeassistant/2/json/LongFast/!f71e2ed0"
    state_class: measurement
    value_template: >-
      {% if value_json.from == 4145950416 and value_json.payload.battery_level is defined %}
        {{ (value_json.payload.battery_level | float) | round(2) }}
      {% else %}
        {{ states('sensor.node_2ed0_battery_percent') }}
      {% endif %}
    unit_of_measurement: "%"

Now I want to create a device_tracker to see the location of one of my T-Beams via Lora in Home Assistant. How do I write the needed template?

Device tracker my off-road car.
automations.yaml:

- id:"2"
alias: mesh_vitara 
description: ""
trigger:
  - platform: mqtt
    topic: msh/2/json/msws20/!1c636d3c
condition:
  - condition: state
    entity_id: sensor.mesh_vitara
    attribute: type
    state: position
  - condition: state
    entity_id: sensor.mesh_vitara
    attribute: from
    state: 84757700
action:
  - service: device_tracker.see
    data_template:
      dev_id: czerwony
      gps: >-
        {{
        (((state_attr('sensor.mesh_vitara','payload')['latitude_i'])|float(0))/10000000)
        }},{{
        (((state_attr('sensor.mesh_vitara','payload')['longitude_i'])|float(0))/10000000) 
        }}
    enabled: true
mode: single

configuration.yaml:

mqtt:
  sensor:
    - name: "mesh_vitara"
      unique_id: "sensor.mesh_vitara"
      state_topic: "msh/2/json/msws20/!1c636d3c"
      json_attributes_topic: "msh/2/json/msws20/!1c636d3c"
      json_attributes_template: '{{ value_json | tojson }}'

and on the map:

- type: map
        entities:
          - entity: device_tracker.vitara
        title: Vitara meshtastic
        dark_mode: true
        hours_to_show: 24
        default_zoom: 14

it works very well with all my 4 nodes.

3 Likes

I’ve been struggling a bit getting the devices showing in the map. I already have the battery metrics coming in, so I know the MQTT path is working fine.

Here are my code blocks if anyone has ideas:

automations.yaml:

 - id: location_lcp
  alias: "Las Canchas Park"
  description: ""
  trigger:
    - platform: mqtt
      topic: msh/2/json/LongFast/!934cf8a8
  condition:
    - condition: state
      entity_id: sensor.lcp_location
      attribute: type
      state: position
    - condition: state
      entity_id: sensor.lcp_location
      attribute: from
      state: 3750913147
  action:
    - service: device_tracker.see
      data_template:
        dev_id: mt_lcp
        gps: >-
          {{
          (((state_attr('sensor.lcp_location','payload')['latitude_i'])|float(0))/10000000)
          }},{{
          (((state_attr('sensor.lcp_location','payload')['longitude_i'])|float(0))/10000000) 
          }}
      enabled: true
  mode: single

mqtt.yaml:

   - name: "Las Canchas Park Node"
    unique_id: "sensor.lcp_location"
    state_topic: "msh/2/json/LongFast/!934cf8a8"
    json_attributes_topic: "msh/2/json/LongFast/!934cf8a8"
    json_attributes_template: "{{ value_json | tojson }}"

I don’t get any “device_tracker” entities that pop up.

Confirm the from field value is correct at 3750913147?

You’ve got 2 types of quotes in there - simple and fancy. Like versus " and versus . Suggest try changing those to simple unicode chars since it might be causing issues with config parsing.

Thank you for the troubleshooting help! I re-did my code entries and I think I’m getting closer.

I can see I have good data coming in from the Logbook:

So pretty sure my automations.yaml and mqtt.yaml are set up correctly, but still not seeing anything in the map.

I tried a few entity options in the Map Editor.

Your tracker should be called device_tracker.mt_lcp

Open Developer Tools then click States. Start typing in the text box below Entities column label to search. You can also do this by going to Settings → Devices → Entities and filter there too. - ted

Thanks all for your patience while I worked through this, I got it working! I had to do some data flow tracing before I understood it.

Here are my code blocks for those interested:

First start with mqtt.yaml

  - name: "mt_messages"
    unique_id: "sensor.mt_messages"
    state_topic: "msh/2/json/LongFast/!934cf8a8"
    json_attributes_topic: "msh/2/json/LongFast/!934cf8a8"
    json_attributes_template: "{{ value_json | tojson }}"

automations.yaml:

- id: "LCP"
  alias: LCP
  description: ""
  trigger:
    - platform: mqtt
      topic: msh/2/json/LongFast/!934cf8a8
  condition:
    - condition: state
      entity_id: sensor.mt_messages
      attribute: type
      state: position
    - condition: state
      entity_id: sensor.mt_messages
      attribute: from
      state: 3750913147
  action:
    - service: device_tracker.see
      data_template:
        dev_id: lcp
        gps: >-
          {{
          (((state_attr('sensor.mt_messages','payload')['latitude_i'])|float(0))/10000000)
          }},{{
          (((state_attr('sensor.mt_messages','payload')['longitude_i'])|float(0))/10000000) 
          }}
      enabled: true
  mode: single

map card on the dashboard:

type: map
entities:
  - entity: zone.home
  - entity: device_tracker.lcp
title: Meshtastic Nodes
dark_mode: true
hours_to_show: 24