Enabling Data Exchange using MQTT

Your Smart Trash Can scenario involves a physical IoT device (like a Raspberry Pi with a PIR motion sensor) and Home Assistant (HA) running inside a container (e.g., Docker). The Raspberry Pi will publish sensor data (fill percentage, motion detection, etc.) to an MQTT broker. Home Assistant, in turn, will subscribe to these MQTT topics to dynamically update its sensor entities and dashboard views.

Overview

MQTT (Message Queuing Telemetry Transport) is a lightweight publish/subscribe messaging protocol well-suited for IoT. Your Raspberry Pi will act as a publisher, sending data to an MQTT broker. Home Assistant, as the subscriber, will receive updates and present them as sensors in its interface.

Key Steps:

  1. Run Home Assistant in a container.
  2. Set up an MQTT broker (like Eclipse Mosquitto) in a separate container.
  3. Configure Home Assistant to connect to the MQTT broker.
  4. Create MQTT sensors in Home Assistant configuration.
  5. Publish sensor data from your Raspberry Pi to the MQTT broker.
  6. Visualize the Smart Trash Can attributes in Home Assistant’s dashboard.

Prerequisites

  • Docker environment already set up.
  • Home Assistant container is up and running (e.g., docker run or docker-compose).
  • A Raspberry Pi with a PIR sensor or other device capable of publishing MQTT messages.

Setting Up the MQTT Broker in a Container

Since we are not using Home Assistant’s Supervisor environment (the add-on store), you can run Mosquitto directly as a standalone container.

Configuration for Mosquitto

Create a mosquitto.conf and a passwords.txt starting in the same folder as the .yml docker compose file, and more specifically inside ./mosquitto/config (follow that folder structure):

Moquitto conf file

listener 1883
allow_anonymous false
password_file /mosquitto/config/passwords.txt

Create a Password File

docker run --rm -it -v $(pwd)/mosquitto/config/passwords.txt:/mosquitto/config/passwords.txt eclipse-mosquitto mosquitto_passwd -c /mosquitto/config/passwords.txt mqttuser
# Enter a password when prompted

This will create a user mqttuser with your chosen password.

Sample docker-compose.yml Snippet

Change the ‘docker-compose.yml’ we created just for HA

services:
  homeassistant:
    image: ghcr.io/home-assistant/home-assistant:stable
    container_name: homeassistant
    volumes:
      - ./config:/config
    restart: unless-stopped
    network_mode: bridge
    ports:
      - "8123:8123"

  mosquitto:
    image: eclipse-mosquitto:latest
    container_name: mosquitto
    restart: unless-stopped
    volumes:
      - ./mosquitto/data:/mosquitto/data
      - ./mosquitto/log:/mosquitto/log
      - ./mosquitto/config:/mosquitto/config
    ports:
      - "1883:1883"

After running "sudo docker-compose -f docker_compose.yml down" "sudo docker-compose -f docker_compose.yml up", you will have both Home Assistant and Mosquitto running. They will be on the same Docker network by default. You can use the service name mosquitto as the host when configuring HA since they share a Docker network.

Also make sure to do a sanity check of the mqtt broker by running:

sudo apt-get install mosquitto-clients
# Enter a password when prompted

Subscribe to a test topic named test/topic to listen for incoming messages. Run this command:

mosquitto_sub -h localhost -p 1883 -u mqttuser -P <your_mqtt_broker_password> -t "test/topic" -d

Open another terminal window and publish a test message to the test/topic:

mosquitto_pub -h localhost -p 1883 -u mqttuser -P <your_mqtt_broker_password> -t "test/topic" -m "Hello from MQTT!"

Configure MQTT Integration in Home Assistant

Now that the MQTT broker is running, tell Home Assistant to connect to it. Assuming Home Assistant and Mosquitto share the network, you can refer to the broker by its service name mosquitto.

  1. Open Home Assistant at http://<your-docker-host>:8123.
  2. Go to Settings > Devices & Services > Add Integration and add MQTT.
  3. When prompted:
    • Host/Broker: mosquitto (the Docker service name, if on the same network)
    • Port: 1883 (unencrypted, as configured above)
    • Username: mqttuser
    • Password: <the password you set>

Alternatively, you can configure MQTT in configuration.yaml:

mqtt:
  broker: mosquitto
  username: mqttuser
  password: <the password you set>

Make sure to restart Home Assistant after editing configuration files.

Defining the Smart Trash Can as MQTT Sensors in Home Assistant

Edit the previous sensors we added in configuration.yaml:

# Remove or comment out the entire template block:
# template:
#   - sensor:
#       - name: "Smart Trash Can"
#         state: "{{ 0 }}"
#         attributes:
#           status: "Ready"
#           location: "Building A, Room 101"
#           trash_can_id: "TC-001"
#           trash_count: 0

# Make sure the MQTT integration is configured (if not, add it here)
#mqtt:
  # If your MQTT broker and credentials are set up via the UI, you might not need anything here.
  # Otherwise, specify broker, username, password, etc.

mqtt:
  sensor:
    - name: "Smart Trash Can Fill Percentage"
      state_topic: "smart_trash_can/fill_percentage"
      unit_of_measurement: "%"
      device_class: "pressure"

    - name: "Smart Trash Can Motion Status"
      state_topic: "smart_trash_can/motion"

    - name: "Smart Trash Can Location"
      state_topic: "smart_trash_can/location"

    - name: "Smart Trash Can ID"
      state_topic: "smart_trash_can/id"

    - name: "Smart Trash Can Trash Count"
      state_topic: "smart_trash_can/trash_count"

Restart Home Assistant to load these sensors.

Changing the view to reflect the changes

since you’re no longer using a single template sensor with attributes, but rather individual MQTT sensors, you’ll need to reference each sensor directly instead of using attributes of a single entity.

Where you previously had a single sensor.smart_trash_can with attributes (like status, location, etc.), you now have multiple sensors:

sensor.smart_trash_can_fill_percentage
sensor.smart_trash_can_motion_status
sensor.smart_trash_can_location
sensor.smart_trash_can_id
sensor.smart_trash_can_trash_count

You should update your Lovelace configuration to reference these sensors directly (dashboard menu -> raw configuration editor):

views:
  - title: Trash Can
    path: trash_can
    icon: mdi:delete-outline
    cards:
      - type: gauge
        entity: sensor.smart_trash_can_fill_percentage
        name: Fill Percentage
        min: 0
        max: 100
        unit: '%'

      - type: entities
        title: Smart Trash Can Details
        entities:
          - entity: sensor.smart_trash_can_motion_status
            name: Motion Status
          - entity: sensor.smart_trash_can_location
            name: Location
          - entity: sensor.smart_trash_can_id
            name: Trash Can ID
          - entity: sensor.smart_trash_can_trash_count
            name: Trash Count

Publishing Data from the Raspberry Pi (MQTT Publisher)

Install paho-mqtt on your Raspberry Pi:

pip3 install paho-mqtt

Sample Python Script

import paho.mqtt.client as mqtt

broker_address = "<docker-host-ip>"  # For example: "192.168.1.100"
broker_port = 1883
username = "mqttuser"
password = "your_password"

# Topics
fill_percentage_topic = "smart_trash_can/fill_percentage"
motion_topic = "smart_trash_can/motion"
location_topic = "smart_trash_can/location"
id_topic = "smart_trash_can/id"
trash_count_topic = "smart_trash_can/trash_count"

client = mqtt.Client("smart_trash_can_client")
client.username_pw_set(username, password)
client.connect(broker_address, broker_port, 60)

# Example values
fill_percentage = 75
motion_status = "detected"
location_value = "Building A, Room 101"
trash_can_id = "TC-001"
count = 3

client.publish(fill_percentage_topic, fill_percentage)
client.publish(motion_topic, motion_status)
client.publish(location_topic, location_value)
client.publish(id_topic, trash_can_id)
client.publish(trash_count_topic, count)

client.disconnect()

Run this script. Home Assistant’s entities will update almost immediately with the new values, of course you would need to integrate it with the logic you had in the lab before in order to publish actual readings from the sensor.

Summary

  • Container-based Setup: Running Home Assistant and Mosquitto via Docker containers ensures portability.
  • MQTT Broker & User Setup: Create and secure a Mosquitto broker.
  • MQTT Integration: Connect Home Assistant to the broker and redefine sensors.
  • Publishing Data: Use Python and paho-mqtt to send data.
Previous
Next