Smart Home Automation with Raspberry Pi

Automate your home with a Raspberry Pi running Home Assistant. Control lights, sensors, and devices from a single dashboard.

Andreas · April 12, 2026 · 10 min read

Introduction

Home Assistant is a privacy-first home automation platform that runs locally on your Pi. Control lights, thermostats, and sensors without cloud dependencies. This guide covers Docker setup, integrations (MQTT, Zigbee), automations, and backup strategy.

Prerequisites

  • Raspberry Pi 4 or 5 (4GB+ RAM strongly recommended)
  • Docker and Docker Compose installed
  • USB stick or SSD for Home Assistant data persistence
  • Smart devices (lights, sensors, switches) — Zigbee or WiFi-based
  • Optional: USB Zigbee coordinator (ConBee II, Sonoff Zigbee 3.0)

Step 1 — Create Home Assistant docker-compose.yml

mkdir -p ~/home-assistant
cd ~/home-assistant

Create docker-compose.yml:

version: '3.8'

services:
  home-assistant:
    image: homeassistant/home-assistant:latest
    container_name: home-assistant
    privileged: true
    restart: always
    ports:
      - "8123:8123"
    volumes:
      - ./config:/config
      - /run/dbus:/run/dbus:ro
      - /etc/localtime:/etc/localtime:ro
    environment:
      TZ: America/New_York
    devices:
      - /dev/ttyUSB0:/dev/ttyUSB0  # For Zigbee USB stick

  mosquitto:
    image: eclipse-mosquitto:latest
    container_name: mosquitto
    restart: always
    ports:
      - "1883:1883"
      - "9001:9001"
    volumes:
      - ./mosquitto/config:/mosquitto/config
      - ./mosquitto/data:/mosquitto/data

Start it:

docker compose up -d

Home Assistant initializes on first run. Access at http://localhost:8123 (takes 30-60 seconds).

Step 2 — First-Run Setup and User Account

  1. Open http://your-pi-ip:8123
  2. Create admin user and password
  3. Name your home (e.g., "My Homelab")
  4. Set timezone and unit system

Home Assistant auto-discovers devices on your network. Allow them to be added.

Step 3 — Add Integrations

MQTT (Mosquitto)

Enable MQTT in Home Assistant:

Settings → Devices & Services → Create Automation → MQTT

Add connection:

broker: mosquitto
port: 1883
username: your-mqtt-user
password: your-mqtt-password

Configure Mosquitto credentials in mosquitto/config/mosquitto.conf:

listener 1883
allow_anonymous false
password_file /mosquitto/config/passwd

listener 9001
protocol websockets

Create password file:

docker exec mosquitto mosquitto_passwd -c /mosquitto/config/passwd yourusername
# Enter password when prompted

Restart Mosquitto:

docker compose restart mosquitto

Zigbee (ZHA Integration)

If using a Zigbee USB stick:

  1. Settings → Devices & Services → Create Integration → Zigbee Home Automation (ZHA)
  2. Select your USB device from dropdown
  3. Name your network

ZHA auto-discovers Zigbee devices within range. Pair devices by putting them in pairing mode (usually a button press) and clicking "Permit Joining" in Home Assistant.

Step 4 — Create Automations

Create config/automations.yaml:

- id: motion_lights
  alias: Turn on lights when motion detected
  trigger:
    - platform: state
      entity_id: binary_sensor.hallway_motion
      to: "on"
  condition: []
  action:
    - service: light.turn_on
      target:
        entity_id: light.hallway
      data:
        brightness: 200

- id: temperature_fan
  alias: Turn on fan when temp exceeds 75F
  trigger:
    - platform: numeric_state
      entity_id: sensor.living_room_temperature
      above: 75
  action:
    - service: climate.turn_on
      target:
        entity_id: climate.living_room_ac

- id: bedtime_routine
  alias: Bedtime routine at 10 PM
  trigger:
    - platform: time
      at: "22:00:00"
  action:
    - service: light.turn_off
      target:
        entity_id:
          - light.bedroom
          - light.living_room
    - service: lock.lock
      target:
        entity_id: lock.front_door

- id: daily_backup
  alias: Create backup daily at 2 AM
  trigger:
    - platform: time
      at: "02:00:00"
  action:
    - service: backup.create

Load automations in config/configuration.yaml:

automation: !include automations.yaml

Restart Home Assistant to apply changes.

Step 5 — Customize Dashboard

Edit config/ui-lovelace.yaml for dashboard layout:

title: Home
views:
  - title: Home
    cards:
      - type: entities
        title: Lights
        entities:
          - light.bedroom
          - light.living_room
          - light.kitchen

      - type: thermostat
        entity: climate.living_room_ac

      - type: entities
        title: Doors
        entities:
          - lock.front_door
          - lock.garage_door

      - type: weather-forecast
        entity: weather.home

Step 6 — Backup Strategy

Home Assistant stores everything in /config. Backup regularly:

Automated daily backup:

#!/bin/bash
BACKUP_DIR="/backup/home-assistant"
mkdir -p $BACKUP_DIR
cp -r ~/home-assistant/config $BACKUP_DIR/config-$(date +%Y%m%d-%H%M%S)

# Keep only last 10 backups
ls -t $BACKUP_DIR/config-* | tail -n +11 | xargs rm -rf

Add to crontab:

crontab -e
# Add: 0 2 * * * /path/to/backup-script.sh

Performance and Resource Usage

Home Assistant on Pi 4 (4GB RAM) uses:

  • CPU: 5-15% at idle
  • Memory: 300-500MB (varies with integrations)
  • Disk: ~500MB for application + databases

Monitor from Home Assistant → Settings → System → System Monitor.

Troubleshooting

Slow dashboard or lag: Disable heavy integrations temporarily. Check database size: ls -lh config/home-assistant_v2.db. Purge old events via Settings → System → Maintenance.

MQTT won't connect: Verify Mosquitto is running: docker compose logs mosquitto. Check credentials in Home Assistant MQTT settings.

Zigbee devices unreliable: Keep USB stick away from WiFi router (interference). Ensure USB power supply is stable. Move USB stick closer to devices.

Home Assistant won't start: Check logs: docker compose logs home-assistant. Verify YAML syntax in configuration.yaml — Home Assistant rejects invalid syntax.

Backup failing: Ensure /config directory has write permissions. Check disk space: df -h. Restart Home Assistant if database is locked.

Home Assistant on Pi gives you a powerful, private smart home without cloud vendor lock-in. Start with basic automations (lights, temperature), then expand to complex workflows as you learn the platform.

Summary

Home Assistant on a Raspberry Pi gives you a private, local smart home hub that doesn't depend on cloud services. With MQTT and Zigbee integrations, you can automate lights, sensors, and appliances — all controlled from a single dashboard.

Related Tools

Comments