Home Assistant via MQTT

Still working on getting a video out but i figured i would post some sensors used in HA for MQTT usage. I use a sensor file to keep the sensors from taking up too much space in the configuration.yaml. Add this line mqtt: “!include mqtt.yaml” under “sensor:” in your configuration.yaml. Make a file in the root folder named mqtt.yaml. Here is a direct example of how i create the sensors in that file. Also make sure to connect the MQTT node with JSON output

sensor:
#RT2 measurements

  • name: “Mesh Voltage RT2”
    state_topic: “msh/2/json/testnet/!fa666218”
    value_template: >-
    {% if value_json.from == -93958068 and value_json.payload.voltage != 0 %}
    {{ (value_json.payload.voltage | float) | round(2) }}
    {% else %}
    {{ states(‘sensor.mesh_voltage_rt2’) }}
    {% endif %}
    unit_of_measurement: “V”
  • name: “Mesh Batt RT2”
    state_topic: “msh/2/json/testnet/!fa666218”
    value_template: >-
    {% if value_json.from == -93958068 and value_json.payload.battery_level != 0 %}
    {{ (value_json.payload.battery_level | float) | round(2) }}
    {% else %}
    {{ states(‘sensor.mesh_batt_rt2’) }}
    {% endif %}
    unit_of_measurement: “%”
  • name: “Mesh Ch Util RT2”
    state_topic: “msh/2/json/testnet/!fa666218”
    value_template: >-
    {% if value_json.from == -93958068 and value_json.payload.channel_utilization != 0 %}
    {{ (value_json.payload.channel_utilization | float) | round(2) }}
    {% else %}
    {{ states(‘sensor.mesh_ch_util_rt2’) }}
    {% endif %}
    unit_of_measurement: “%”
  • name: “Mesh Air Util TX RT2”
    state_topic: “msh/2/json/testnet/!fa666218”
    value_template: >-
    {% if value_json.from == -93958068 and value_json.payload.air_util_tx != 0 %}
    {{ (value_json.payload.air_util_tx | float) | round(2) }}
    {% else %}
    {{ states(‘sensor.mesh_air_util_tx_rt2’) }}
    {% endif %}
    unit_of_measurement: “%”

“msh/2/json/testnet/!fa666218” is the full topic that the mesh device will post to “testnet” is whatever you named your channel and “!fa666218” is the device id what is the uplink and downlink node for the mesh. the “-93958068” is the node that is sending the information to the MQTT node. A good way to figure out what devices are in the mesh is by using a program like “MQTT Explorer” in windows at least. There are other utilities and i have a few that are specifically for seeing/sending JSON messages in the MQTT broker from/to the mesh. Below is an example of a message sent in the mesh showing the “from” and the message payload. RT2 is the name of my node so all I did was carry that over as the name of the entity.

{
“channel”: 0,
“from”: -93953512,
“id”: 1393808275,
“payload”: {
“text”: “Test”
},
“sender”: “!fa666218”,
“timestamp”: 1687404793,
“to”: -1,
“type”: “text”
}

When i get time to post the video on everything else such as sending and receiving messages directly from Home Assistant etc. I will make sure to get it posted here. There are a lot of useful automations that can be done with text information such as arming and disarming alarmo in home assistant or getting the weather report from your weather station in the back yard.

4 Likes

This looks very interesting!

I JUST spent tons of time last Friday doing exactly this to get the battery levels of mine into HA and I only today see your post lol.

Thanks for sharing!

1 Like

Hello!
I also killed my Friday with reated project, stucked at mqtt from broker (dridged with local broked +msh) I see messages on MQTT explored as such: "
Z
��������*0�1��j��I�L��vH� 爺�5s��f��:m_�0-ڸ�\7y��5V�6’=(m�eE�@H`���������LongFast
" to me is it related to encryption if yes, how to decrypt?

msh_2023-12-31_15-34

+in documentation it say to use such format:
state_topic: “msh/2/json/LongFast/!67ea9400”

why tere is /json/ there? in explores I dont see such prefix. tried both, in HA values are " unknown" .

Thanks in advance!

Change the output to JSON in the mqtt settings on the meshtastic device and you should be good to go.

Thanks for replay, do you mean this setting - " Whether to send/consume JSON packets on MQTT"

I tried enable/disable… nothing chnaged.

I run V2.2.17… firmware.
I tried to disabe enrypting.

+broked bridge settings what I made:

Bridge configuration

connection bridge-01
address mqtt.meshtastic.org:1883
remote_username meshdev
remote_password large4cats

topic msh/2/c/LongFast/# both 2

and I see my TTGO in MQTT explorer…

I could try to set up other HA to eliminate this bridge need, just to test am I right with this if I put diredt MQTT.meshatastic… into HA but no shortcuts, dont want to give up :slight_smile:

That is protobuf output you are seeing, you need to turn off encryption and turn on json

pardon for noob question, but can it be done within WEB UI config? I found this guide, but that will take extra days to learn Node red Here is a newb MQTT tutorial for 1.3 - #28 by Nanovitruvius

I just noticed this message. You can enable mqtt via the web interface. I had to go the web interface route before the settings were in the Android app.

Hello!

Could it be that firmware 2.2.19 did some changes, as I sat back to this, re-flashed device and it now sending to topic msh/2/2/json/LongFast/!.. and I am getting sensor to HA.
but if I want to use encryption, where to get PSK? this is still not clear… Thanks!

PSK would be from your MQTT server setting and you will need to change the port connection. Port 8883 is the default encrypted port for the MQTT add-on in HA.

Hello, thanks for replay!
Ok, will start with basic… I sat up client-router (with wifi access) - it works fine, I can get sensor data, send messge to client (without wifi)… but how to get sensor data from client, thru router… on mqtt explorer I jsut see status -online, no telemetry thru mesh.
if this will be fixed, then I will move to next level- encyprion :slight_smile: did research on youtube, but did not found such topic. thanks!

They should also come in via mqtt after the radio has been on for roughly a minute. Environment telemetry data should send instantly when the radio boots. Make sure if it’s environmental data you want to see, you have it turned on in the telemetry settings menu under the radio configuration menu. Voltage, channel utilization, airtime utilization and battery % all send about a minute after the radio turns on and is part of the default settings to send every 15 minutes (900 seconds).

Also if you have multiple channels on your radio the first channel you setup(default) will be the mqtt topic in which the data is sent.

Hello, thanks for replay!
not sure what setting I missed…
For Router, I see data in this topic:
msh/EU_433/2/json/LongFast/!XXXXX
and for Client, I found only this topic on msh broker (by search functiion)
msh/2/2/stat/!XXXXXX and it show: online
*I see sensors data on display and also in display it show " joined XXX - my Router " name" ) chat messhages come and go thru.



Thanks!