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.

4 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
1 Like

Hello, may i ask to make it clear… noob level questiion?

At HA I should add mqtt.mestastic.org server, right and then pick up topic with my node id?
I want to take environment data from BME280 that is cnnected to TTGO board.

code example: msh/2/json/msws20/![here my node id] right?
[msh/2/json/msws20] remains constant or I have to edit/adjust it as well?

My setup is TTGO on premises where I want to see temp/RH data and at home I have T-Boeam as client-router role (connected to wi-fi) TTGO cant be connected to wifi so I think Environemnt data should go thru mesh network and I should pick it up somehow from mqtt.meshtastic.org server, right?

+how to encrypt messgaes, I see something happening on MQTT Explored, but data is " not readable" and on Home assistant I have " unknown values"

Thanks in advance!

“msws20” is my channel name. You have to edit it.

yes, pardon, I used mine, but output is some random numbers, seems it is encrypted, but I dont know how todecrypt, see this ost where I shared screens-

do you see this topic? #/mesh/2/json

the message tree should look like this
image

if you don’t see the json topic, and are getting messages, then enable JSON output: MQTT Module Configuration | Meshtastic

Hello, Ted!

I see sensor date from my device with role client-router that has connected to wifi.

msh/EU_433/2/json/LongFast/!xxxxx

{"channel":0,"from":4184516416,"id":1598144019,"payload":{"air_util_tx":0.085999995470047,"battery_level":100,"channel_utilization":10.3316669464111,"voltage":4.15700006484985},"sender":"!fxxxxx","timestamp":1708710718,"to":4294967295,"type":"telemetry"}

but client with no wifi, only BT- iOS app it shows nothing today
but with proxy funcion enablet it posts encrypted info…
I cant replicate anymore that it show just =online.
setup is folowing, see pic →

if I kill iOS app, then no new posts, seems it does not brodcast anything trhu mest, but I tested iOS app without “network” just BT enabled and then i can chat with my other clinet that has wifi… strange

What firmware and hardware is your device? There have been issues with MQTT in recent versions. Try latest BETA firmware if you’re not already using it.

Suggest you create a new thread and post link here; I’ll help you there.- ted

Hi, Ted!

Thanks for replay, OK, I made topic, but normally I try to join existing conversation, to not spam forum… I thoght it will be short question, but leaded to longer conversation. here is link - https://meshtastic.discourse.group/t/sensor-data-over-msh-take-out-reading-from-mqtt/10514

Thanks for the write up.

If i understand this, you are filtering on a node with ID 3750913147. What would be the right approach to see all the nodes on the mesh on a map? Should i repeat the automation step to create new entities?

Thanks…

Yeah to see all the other nodes on a HA map, you would have to repeat the automation for each node. That’s a bit painful if there are a lot of nodes in the area.

I use HA just to monitor my placed nodes because they are all solar. I’d recommend just using https://meshmap.net/ if you want to check the other nodes around you that are just on the map.

1 Like

I had issues implementing this, because the “import” with the MQTT sensor failed many times.

My Sensor us now:

    - name: "MeshMQTT Positions"
      unique_id: "meshmqtt_positions"
      state_topic: "msh/CH/2/json/LongFast/!da112233"
      value_template: >-
        {% if value_json.type == "position" and
              value_json.payload.latitude_i is defined %}
          {{ (value_json.timestamp | float) | round(1) }}
        {% else %}
          {{ states('sensor.meshmqtt_positions') }}
        {% endif %}
      json_attributes_topic: "msh/CH/2/json/LongFast/!da112233"

You could also filter the address of your device in there. But I want to track more than one device and and so I need only one sensor, but more than one automation. It probably depends how busy your MQTT topic is.

Also I excluded sensor.meshmqtt_positions from the recorder