Send data from ChirpStack to Home Assistant via MQTT

Viewed 47

I'm really new to ChirpStack or LoRa devices. So sorry if this is something really straightforward to be solved. I failed on trying to find the answers I need here in the community.

I have a water sensor connected to ChirpStack. I would like to send the data I'm currently capturing from the sensor to my Home Assistant instance. The problem is that I could not find a way to wire ChirpStack with HA via MQTT.

  1. At the first time I tried to configure ChirpStack MQTT Forwarder with the IP of MQTT server running in HA in the chirpstack application (the one running with the default port 8080). And it didn't work. I was not even processing the payload with that configuration in MQTT Forwarder.

  2. Then I realized I should, instead, configure ChirpStack MQTT Forwarder with the internal MQTT server to make the communication between the data from HAT (from the application running on port 8080) and ChirpStack (the one running in port 80).

  3. Once I have that configuration, I could get the data from the sensor (I had to implement my own codec to "decript" the payload but everything seems to be working fine at this area).

The next step would be to send the decripted payload (JSON format right now) to HA via MQTT (the one that is running in HA). I tried reading the ChirpStack documentation but I could not find the answer. Neither find someone that implemented this.

Please, could someone help me on that? I appreciate if you could at least point me an example/documentation on how to do that...

BTW, I'm currently running ChirpStach v4.15.0 in a Raspberry Pi with the HAT Seeed WM1302.

Thanks.

4 Answers

Most likely you are looking for:
https://www.chirpstack.io/docs/chirpstack/integrations/mqtt.html

The MQTT integration can be configured in the chirpstack.toml config file, example:

# Global integration related configuration.
[integration]

  # Enabled integrations (global).
  enabled = [
  ]

  # MQTT integration configuration.
  [integration.mqtt]

    # Event topic template.
    event_topic="application/{{application_id}}/device/{{dev_eui}}/event/{{event}}"

    # Command topic.
    #
    # This is the topic on which the MQTT subscribes for receiving (enqueue) commands.
    command_topic="application/{{application_id}}/device/{{dev_eui}}/command/{{command}}"

    # Use JSON encoding instead of Protobuf (binary).
    json=true

    # MQTT server (e.g. scheme://host:port where scheme is tcp, ssl or ws)
    server="tcp://127.0.0.1:1883/"

    # Connect with the given username (optional)
    username=""

    # Connect with the given password (optional)
    password=""

I've tried this configuration but it didn't work.

I created the file chirpstack.toml under the path /etc/chirpstack/ because it didn't exist there.

This is the payload I've added.

[integration]
  enabled = ["mqtt"]

  # MQTT integration configuration.
  [integration.mqtt]

    # Event topic template.
    event_topic="application/{{application_id}}/device/{{dev_eui}}/event/{{event}}"

    # Command topic.
    #
    # This is the topic on which the MQTT subscribes for receiving (enqueue) commands.
command_topic="application/{{application_id}}/device/{{dev_eui}}/command/{{command}}"

    # Use JSON encoding instead of Protobuf (binary).
    json=true

    # MQTT server (e.g. scheme://host:port where scheme is tcp, ssl or ws). The one running in Home Assistant.
    server="tcp://192.168.0.91:1883/"

    # Connect with the given username (optional)
    username="<redacted>"

    # Connect with the given password (optional)
    password="<redacted>"

I started 2 different MQTT Explorer:

  1. One connected to MQTT Server running in ChirpStack; and
  2. another one connected to MQTT Server running in HA.

I found the topic application in the MQTT Explorer connected to ChirpStack but nothing in the one connected to MQTT server running in HA.

My questions:

  1. As far as I understood, those topics are really templates and I don't need to hardcode any application_id or dev_eui or ... there, right?
  2. The IP should be the one of MQTT Server running in HA? Does the last slash ("/") matter?
  3. Is there any specific log for this integration/communication? I've tried the system logs (Status > System Log from menu) but I couldn't find anything related to this integration (the IP configured) but just the logs related to the ChirpStack MQTT Forwarder.
  4. Am I missing something?

Note: It's a bit tricky to debug this environment because the sensor transmit data every 12 hours. So, I'll need to wait a little bit until I validate this integration again.

Thanks in advance

Do you use a separate MQTT Broker for HA and for Chirpstack? If not, you can use the topics of Chirpstack in the configuration file of HA (configuration.yml):

mqtt:
  sensor:
    - name: "Water Meter Volume"
      unique_id: "water_meter_volume"
      state_topic: "application/<applicationid>/device/<deveui>/event/up"
      value_template: "{{ value_json.object.volume }}" <-- this is the volume key in the decoded MQTT JSON message)
      unit_of_measurement: "<check the correct unit>"
      device_class: <check the correct device_class>
      state_class: measurement

If you use two Brokers (one for HA and one for Chirpstack) you need to configure a bridge.

Here is an example:

connection bridge-from-ha-to-chirpstack
address <IP-Address to chirpstack broker>:8883

# Authentifizierung
remote_username USER
remote_password PW
# CLIENTID
clientid ha_bridge_to_chirpstack

# TLS 
bridge_capath /etc/ssl/certs/
bridge_insecure true

try_private false
notifications false
bridge_protocol_version mqttv311

topic application/<applicationid>/device/<deveui>/event/up/# in 0 ha/

If you use this configuration, you have to change the topic in the configuration.yml to ha/application/<applicationid>/device/<deveui>/event/up

The bridge.conf you need to put in \share\mosquitto folder of HA.

I use a separate MQTT Broker. I have one for HA and another one for ChirpStack. I started searching how to configure a bridge since this is something I've never did before. Thanks!!!

If added a example config for the bridge.

Which install did you do? If you did docker the chirpstack.toml file wouldn't be in /etc/chirpstack but /chirpstack-docker/configuration/chirpstack, I'd be surprised if you didn't already have the file and think you might be adding a file somewhere Chirpstack isn't looking.

You do not need to hardcode the values for dev-eui etc for the MQTT integation, those get filled appropriately.

I remember a bunch of people setting up Chirpstack with Home Assitant on the previous forum, searching doesn't seem to give many relevant results for some reason but I can personally remember at least 5 within the past two years. Here's an example: https://forum.chirpstack.io/t/working-example-homeassistant-to-lorawan-chirpstack-downlink-commands/24603

I'm not running containers. I'm running this image chirpstack-gateway-os-4.9.0-full-bcm27xx-bcm2709-rpi-2-squashfs-factory.img in a Raspberry Pi 3B. And the file was not under the path /etc/chirpstack/. In this path I had the files chirpstack.sqlite and region_*.toml (39 files with this pattern). No file called chirpstack.toml.

BTW, I'll take a look at this old forum. Thanks for sharing this reference.