Home Automation Script and Automation to setup a Meshtastic Node as a Real Device in Home Assistant
Before you continue I recently found out there is already an integration available under the name "Meshtastic MQTT Client" in HACS (or nr 761792106) zie also github. The benefit of this integration is that it does not require JSON. It is likely you wish to follow this integration. When I started my objective was to see a Meshtastic Node as 1 device. After a lot of wrong intents and 'kommaneuken' I decided to abandon YAML (but I still prefer packages to keep everything of a project in 1 file) and do this directly in Home Assistant where mistake are noticed directly. This is the endresult:There is only 1 requirement and that is that you have your own MQTT-broker, a semi-requirement is that inside your own broker you setup a MQTT Bridge to a Meshtastic MQTT broker. How to do this is beyond the scope of this documentation and is much better explained elsewhere like here however in Dutch. WARNING: publish inside your own broker and not in other mans broker !!!!!. When your broker part is setup correctly you may create a new automation and copy the content of file "AUTOMATION - Meshtastic Node - retain topics.yaml" (if you prefer other textual methods you might need to change the intent etc). Before you activate you need to update the TOPiC lines:
topic: msh/pa8f/<zip_code_range>/2/json/LongFast/<your_mqtt_node>
With your details. This automation will publish the incoming JSON topic into seperate and retained topics. The topics are extended with the FROM and TYPE fields, if the TYPE=telemetry this is even more differentiated by type of metric: device, environment and power. The result (2-retained) looks in MQTT-explorer like this:
Note: If your Mesh is big you might want to limit this by selecting only the nodes of your scope.
To discover each Meshtastic Node. You need to run the script "SCRIPT - Meshtastic Node mac4 MQTT Discovery.yaml". Before you run this script you need to change:- mac4 in an unique identifier (I use the mac4 extention of the device)
- nodid into the value of FROM field of the new device
- set each "discover_" variable to true/false if it needs to be included or not
- update the details of the device, "identifiers" and "name" are already updated in previous steps, you may remove the rest but watch out for the last missing komma
NOTE: Not every possible sensor is already included but hopefully this setup is easy enough to extend this.
You can create also an additional template sensor in Settings - Device - Helper, this sensor will calulate the distance of a node to Home with this template: {{ distance('device_tracker.meshtastic_node_mac4_tracker') | round(2) }} See for further required settings the picture below:As you can see you also be able to link this sensor with the device itself. This action is only relevant if the node has a position and in the script "discover_GPS: true" when the script was run for this node.
- 20241201 - Publish first version
- 20241203 - The Device_Tracker is now included in the Device, seems order is pretty important to be successful
- 20241208 - Discovery of an existing integration
Found mostly my inspiration in the Home Assistant Forum to new a few (just a few):
- https://meshtastic.org/docs/software/integrations/mqtt/home-assistant/
- https://community.home-assistant.io/t/creating-a-device-with-multiple-sensor-entities-via-mqtt-discovery/584372/5
- https://community.home-assistant.io/t/mqtt-sensor-how-to-load-attributes/212523/6
- https://community.home-assistant.io/t/split-mqtt-payload-and-send-values-to-entities/257445/4
- https://community.home-assistant.io/t/convert-integer-to-hours-and-minutes/333945/2
- https://community.home-assistant.io/t/passing-trigger-payload-json-to-script/278951/6
- https://community.home-assistant.io/t/2023-7-responding-services/588733/258
- https://community.home-assistant.io/t/example-using-response-from-a-script/599180/13
- https://community.home-assistant.io/t/convert-received-hex-codes-to-usable-decimal-values/465514
- https://community.home-assistant.io/t/changing-a-decimal-value-into-a-hex-value-jinja/11925/4
- https://community.home-assistant.io/t/extract-substring-from-string/94592/2
- https://www.home-assistant.io/integrations/input_text/
- https://www.home-assistant.io/integrations/device_tracker.mqtt/
- ESPresense/ESPresense#144
- iantrich/config-template-card#10