Enabling Data Collection using Home Assistant

Your Smart Trash Can IoT project now includes a real IoT sensor (PIR motion), a script to print messages when motion is detected, and context-related information for both the device and the physical Trash Can entity. In this guide, we’ll set up Home Assistant to manage data collection, device integration, and automation for the Smart Trash Can project.

Home Assistant

Home Assistant is an open-source home automation platform that focuses on local control and privacy. It allows you to integrate a wide range of devices and set up automation rules. More information can be found at Home Assistant.


Step-by-Step Installation Guide

Step 1: Update and Upgrade Raspbian OS

Before installing any new software, it’s a good practice to update and upgrade your existing packages:

sudo apt update
sudo apt upgrade -y

Step 2: Install Docker on Raspberry Pi

Docker simplifies the process of running Home Assistant in a containerized environment.

# Download and run Docker installation script
curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh

# Add 'pi' user to the 'docker' group
sudo usermod -aG docker pi

Verify Docker installation:

docker --version

Step 3: Install Docker Compose

Install Docker Compose to manage multi-container environments:

sudo apt install docker-compose
docker-compose --version

Step 4: Install Home Assistant

Create a directory for Home Assistant and a docker-compose.yml file:

mkdir homeassistant
cd homeassistant

Create a docker-compose.yml file with the following content:

services:
  homeassistant:
    container_name: homeassistant
    image: ghcr.io/home-assistant/home-assistant:stable
    volumes:
      - ./config:/config
      - /etc/localtime:/etc/localtime:ro
    restart: unless-stopped
    ports:
      - "8123:8123"  # Map container port 8123 to host port 8123

Step 5: Run Home Assistant

Start Home Assistant with Docker Compose:

sudo docker-compose -f docker-compose.yml up

Home Assistant will be accessible at:

http://<your-raspberrypi-ip>:8123

so localhost basically

Step 6: Initial Setup and Login

  1. Open Home Assistant in your browser.
  2. Follow the on-screen instructions to create an account and set up the initial configuration.

Add Contextual Data for the Smart Trash Can

Step 7: Create a Trash Can Entity

When you set up Home Assistant with Docker, your docker-compose.yml file includes this volume mapping:

volumes:
  - ./config:/config

This means the configuration.yaml file is located in the config directory within the same folder where your docker-compose.yml file is saved.

Add a manual entity in configuration.yaml to represent the Trash Can (Try to find and add other attributes that might be usefull):

template:
  - sensor:
      - name: "Smart Trash Can"
        state: "{{ 0 }}"
        attributes:
          status: "Ready"
          location: "Building A, Room 101"
          trash_can_id: "TC-001"
          trash_count: 0

restart the container afterwards

sudo docker-compose restart

Step 8: Display in the Dashboard

You can show the main state of the sensor with a Gauge card for a nice visual representation, and then display all attributes in an Entities card.

Go to Settings > Dashboards > (choose your dashboard) > Edit Dashboard > Raw Configuration Editor and add a new view like this:

views:
  - title: Trash Can
    path: trash_can
    icon: mdi:delete-outline
    cards:
      # A gauge card to visually show the fill percentage
      - type: gauge
        entity: sensor.smart_trash_can
        name: Fill Percentage
        min: 0
        max: 100
        unit: "%"
        
      # An entities card to list the attributes
      - type: entities
        title: Smart Trash Can Details
        entities:
          - type: attribute
            entity: sensor.smart_trash_can
            attribute: status
            name: Status
          - type: attribute
            entity: sensor.smart_trash_can
            attribute: location
            name: Location
          - type: attribute
            entity: sensor.smart_trash_can
            attribute: trash_can_id
            name: Trash Can ID
          - type: attribute
            entity: sensor.smart_trash_can
            attribute: trash_count
            name: Trash Count

What this does:

The Gauge card shows the main sensor state as a fill percentage visually.
The Entities card lists each attribute using the type: attribute configuration (available in newer versions of Home Assistant). This allows you to show the attributes without having to create additional sensors.

Final Notes

Your Smart Trash Can IoT project is now integrated with Home Assistant, providing contextual data, and customizable automation (that we will add at a future lab). You can explore HA and customize your dashboard to suit your needs.

For more details, refer to the Home Assistant Documentation.

Previous
Next