Low-code Platforms

Lab 07 — Home Assistant Integration

Deadlines:

  • End of lab session (GitHub checkpoint): commit & push your progress to your team repository.
  • Before next lab (eClass submission): upload (1) a .zip with your code and (2) a PDF export of labs/lab07/README.md.

Submission contents:

  • (1) a .zip with your code, and
  • (2) a PDF export of labs/lab07/README.md.

Intro to what you need to do

So far you have built a motion-detection pipeline, packaged it in Docker, modeled your data with JSON-LD, and connected the components through MQTT. The system works, but nobody outside your team can see what the wastebin is doing unless they open a terminal and read log files. In a real deployment, you would need a way for non-technical people to see what is happening. A facilities manager wants to glance at a dashboard and know which bins are full. A maintenance team wants to get an alert when a sensor goes offline. Building this kind of interface from scratch is a lot of work and most of it has been done before.

This is why we are going to use Home Assistant.

What is Home Assistant?

Home Assistant is an open-source home automation platform. It runs locally (on your device, not in the cloud), and it provides a web-based dashboard where you can see the state of sensors, control devices, create automations, and build dashboards, all through a browser.

Originally designed for smart homes (lights, thermostats, door locks), Home Assistant has also become widely used in IoT prototyping in general because it solves several problems at once:

  • Device management: it maintains a registry of all your devices and sensors, each with a current state and history
  • Dashboards: it provides a drag-and-drop UI for building visual dashboards without writing frontend code
  • Automations: you can define rules like “if the bin is more than 80% full, send a notification”
  • Integrations: it speaks dozens of protocols out of the box, including MQTT, HTTP, Zigbee, Z-Wave, Bluetooth, and more
  • History and logging: it stores state changes over time, so you can see trends and debug issues

The reason we are using it in this course is that it gives you a fully functional IoT platform that you can run on your Raspberry Pi. Instead of building a dashboard, a state manager, an automation engine, and a history database from scratch, you get all of that for free. Your job is to connect your pipeline to it.

Home Assistant organizes everything around entities. An entity is anything that has a state (e.g a temperature sensor reading 23°C, a light that is on or off, a motion sensor that is detected or clear, a bin that is 72% full). Every entity has a unique ID, a friendly name, a current state, and optionally a set of attributes (extra data attached to the state). The dashboard shows entities. Automations act on entities. History tracks entities.

Your task in this lab is to make your Smart Wastebin appear as a set of entities inside Home Assistant. The motion sensor becomes an entity. The wastebin itself becomes an entity. When your pipeline detects motion, Home Assistant should show it. When the state changes, the dashboard should update. All of this happens through MQTT.

Part 1 — Run Home Assistant in Docker

You will run Home Assistant as a Docker container on your Raspberry Pi. This is called the “Home Assistant Container” installation method. It gives you the full Home Assistant core without taking over your entire Pi operating system.

Create a directory for Home Assistant configuration:

mkdir -p ~/homeassistant/config

Run Home Assistant:

docker run -d \
  --name homeassistant \
  --restart unless-stopped \
  -v ~/homeassistant/config:/config \
  -v /run/dbus:/run/dbus:ro \
  --network host \
  ghcr.io/home-assistant/home-assistant:stable

A few things about this command:

--network host gives the container full access to the host’s network. Home Assistant needs this to discover devices on the local network and to communicate with your Mosquitto broker on localhost:1883.

-v ~/homeassistant/config:/config mounts a local directory for all of Home Assistant’s configuration. This is where it stores its database, dashboards, automations, and all entity state. Because it is mounted from the host, your configuration survives container restarts and rebuilds.

--restart unless-stopped ensures Home Assistant starts automatically if the Pi reboots.

The first startup takes a few minutes because Home Assistant initializes its database and default configuration. Wait for it to be ready:

docker logs -f homeassistant

Watch for a line like Home Assistant initialized in X.Xs or Frontend loaded. Then open a browser and go to:

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

If you are working directly on the Pi, use http://localhost:8123.

You will see the Home Assistant onboarding screen. Create an account (this is local, not cloud-based):

  1. Set a name, username, and password, remember these
  2. Set your location (this is used for weather and time zone)
  3. Skip any automatic device discovery for now
  4. Click through to the main dashboard

You should now see the Home Assistant dashboard, mostly empty, but working. Take a moment to click around. Look at the sidebar: Overview (the dashboard), Settings, Developer Tools. You will use all of these.

Part 2 — Connect Home Assistant to your MQTT broker

Home Assistant needs to know about your Mosquitto broker. You will add the MQTT integration through the UI.

First, make sure your Mosquitto broker is running and allows anonymous connections (or configure credentials but for this lab, anonymous is fine). If you installed Mosquitto in Lab 06, it should already be running.

Check that Mosquitto is configured to accept external connections. Create or edit the Mosquitto configuration file:

# If you are running Mosquitto as a system service:
sudo nano /etc/mosquitto/conf.d/default.conf

Add:

listener 1883
allow_anonymous true

Restart Mosquitto:

sudo systemctl restart mosquitto

Now add the MQTT integration in Home Assistant:

  1. Go to SettingsDevices & ServicesAdd Integration
  2. Search for MQTT
  3. Select MQTT
  4. Enter the broker address:
    • Broker: localhost (or your Pi’s IP if running in a separate container)
    • Port: 1883
    • Leave username and password empty (anonymous)
  5. Click Submit

Home Assistant should connect successfully. You now have a bridge between your MQTT broker and Home Assistant anything published to the broker can become an entity in HA.

Verify the connection

Go to SettingsDevices & ServicesMQTT tab (you may need to look under “Listen to a topic”). Subscribe to # (all topics) and publish a test message:

  • Topic: test/ha
  • Payload: hello from Home Assistant

You should see the message appear. You can also publish from the terminal:

mosquitto_pub -h localhost -t "test/from-terminal" -m "hello from terminal"

And it should show up in the Home Assistant MQTT listener. The bridge is working.

Part 3 — Understand MQTT Discovery

Home Assistant supports a feature called MQTT Discovery. Instead of manually configuring every entity in YAML files, you can have your devices announce themselves by publishing a special configuration message to a well-known topic. When Home Assistant sees this message, it automatically creates the entity.

The discovery topic follows this pattern:

homeassistant/<component>/<object_id>/config

Where:

  • <component> is the type of entity: binary_sensor, sensor, switch, device_tracker, etc.
  • <object_id> is a unique identifier you choose for this entity
  • The payload is a JSON object describing the entity’s properties

For example, to create a motion sensor entity, you publish a configuration message to:

homeassistant/binary_sensor/pir_01_motion/config

With a payload like:

{
  "name": "PIR Motion Sensor",
  "state_topic": "smartbin/bin-01/pir-01/motion",
  "payload_on": "detected",
  "payload_off": "clear",
  "device_class": "motion",
  "unique_id": "pir_01_motion",
  "device": {
    "identifiers": ["pir-01"],
    "name": "PIR Sensor 01",
    "model": "HC-SR501",
    "manufacturer": "Generic"
  }
}

This tells Home Assistant:

  • Create a binary sensor (something that is either on or off)
  • Its state comes from the MQTT topic smartbin/bin-01/pir-01/motion
  • When that topic receives "detected", the entity is ON; when it receives "clear", it is OFF
  • It has the device class motion, so HA knows to use the right icon and wording
  • It belongs to a device called “PIR Sensor 01” — this groups entities together in the UI

The device block is important. It tells Home Assistant that this entity belongs to a physical device. If you later add more entities to the same device (like a battery level sensor), they will all appear grouped together under “PIR Sensor 01” in the UI.

Try it from the terminal:

mosquitto_pub -h localhost -t "homeassistant/binary_sensor/pir_01_motion/config" -r -m '{
  "name": "PIR Motion Sensor",
  "state_topic": "smartbin/bin-01/pir-01/motion",
  "payload_on": "detected",
  "payload_off": "clear",
  "device_class": "motion",
  "unique_id": "pir_01_motion",
  "device": {
    "identifiers": ["pir-01"],
    "name": "PIR Sensor 01",
    "model": "HC-SR501",
    "manufacturer": "Generic"
  }
}'

Note the -r flag, the discovery message should be retained so that Home Assistant picks it up even if it restarts after the message was published.

Now publish a state update:

mosquitto_pub -h localhost -t "smartbin/bin-01/pir-01/motion" -m "detected"

Go to your Home Assistant dashboard. You should see a new entity: PIR Motion Sensor, showing “Detected”. Publish "clear" to the same topic and watch it update.

mosquitto_pub -h localhost -t "smartbin/bin-01/pir-01/motion" -m "clear"

Your Python code will do the same thing programmatically.

Part 4 — Create the Smart Wastebin entity

Now create a more complete set of entities representing your Smart Wastebin. You need to think about what information you want to expose in Home Assistant and how to structure it.

At minimum, create these entities:

The motion sensor (binary sensor)

You already did this in Part 3. Refine it if needed.

The wastebin itself (as a device with attributes)

Create a sensor entity that represents the wastebin’s overall status. This is where you can attach attributes with extra information:

mosquitto_pub -h localhost -t "homeassistant/sensor/wastebin_01_status/config" -r -m '{
  "name": "Wastebin Status",
  "state_topic": "smartbin/bin-01/status",
  "value_template": "{{ value_json.state }}",
  "json_attributes_topic": "smartbin/bin-01/status",
  "unique_id": "wastebin_01_status",
  "device": {
    "identifiers": ["bin-01"],
    "name": "Smart Wastebin 01",
    "model": "Smart Wastebin v1",
    "manufacturer": "ECE CK801 Team"
  }
}'

Then publish state with attributes:

mosquitto_pub -h localhost -t "smartbin/bin-01/status" -m '{
  "state": "active",
  "location": "Lab Room 101",
  "last_motion": "2026-04-10T14:32:01Z",
  "total_events_today": 42
}'

The value_template extracts the state field from the JSON payload to use as the entity’s main state. The json_attributes_topic tells HA to read the rest of the JSON fields as attributes — so location, last_motion, and total_events_today all appear as extra information on the entity.

A motion event counter (sensor)

Create a sensor that tracks how many motion events have been detected:

mosquitto_pub -h localhost -t "homeassistant/sensor/wastebin_01_motion_count/config" -r -m '{
  "name": "Motion Event Count",
  "state_topic": "smartbin/bin-01/pir-01/event_count",
  "unit_of_measurement": "events",
  "icon": "mdi:motion-sensor",
  "unique_id": "wastebin_01_motion_count",
  "device": {
    "identifiers": ["bin-01"],
    "name": "Smart Wastebin 01"
  }
}'

Notice this entity uses the same device.identifiers as the wastebin status ["bin-01"]. This tells Home Assistant that the motion counter belongs to the same device as the status sensor. They will appear grouped together in the UI under “Smart Wastebin 01”.

Think about what other entities would be useful. Some ideas:

  • Last motion time — a sensor showing when the last motion event occurred
  • Pipeline latency — a sensor showing the current pipeline latency in ms
  • Sensor online/offline — a binary sensor tracking whether the PIR sensor is alive
  • Lid state — a binary sensor (open/closed) if you model the lid as triggered by motion

You do not need to implement all of these, but think about which ones add value and implement at least one or two beyond the minimum.

Part 5 — Publish from your pipeline

Now modify your producer.py from Lab 06 so that it not only publishes raw events to your data topic, but also updates the Home Assistant entities.

There are two things to do:

1. Publish discovery messages on startup

When the producer starts, it should publish the MQTT discovery configuration messages (retained) so that Home Assistant knows about the entities. This is a one-time setup that happens at startup:

Import JSON handling utilities

Define a function to publish discovery information:
    Accept MQTT client, container identifier, and device identifier

    Create a configuration object for a motion sensor:
        Set a display name
        Build the state topic using the identifiers
        Define values for active and inactive states
        Assign a sensor classification (motion)
        Generate a unique identifier
        Add device metadata:
            Include identifiers list
            Include device name
            Include model and manufacturer

    Convert the configuration object into a JSON string

    Publish the JSON string to a discovery topic:
        Construct the topic path using the device identifier
        Use appropriate delivery guarantees
        Mark the message as retained

    Note:
        Additional sensors or entities would follow the same pattern:
            Create configuration
            Serialize
            Publish to corresponding discovery topic

Call this function after connecting to the broker, before the main sensing loop starts.

2. Publish state updates alongside events

In your main loop, when a motion event is detected, publish to both your data topic (for the JSONL consumer) and the Home Assistant state topics:

# Publish the full event to the data topic (for your consumer / JSONL logger)


# Publish the simple state to the HA entity topic

When motion clears (cooldown ends, state returns to idle)

The data topic carries the full JSON event record (timestamps, seq, latency, JSON-LD context, everything). The HA state topic carries just the simple value that Home Assistant expects ("detected" or "clear").

Run it and watch

  1. Make sure Mosquitto is running
  2. Make sure Home Assistant is running
  3. Start the producer with HA discovery enabled
  4. Open the Home Assistant dashboard in your browser
  5. Trigger motion events and watch the entity update in real time

Go to SettingsDevices & ServicesMQTTDevices. You should see your devices listed. Click on one to see all its entities and their current states.

Part 6 — Create a motion counter with automations

Home Assistant is not just a way to visualise things, it can act on state changes. An automation is a rule that says “when this happens, do that.” Every automation has three parts:

  • Trigger: what event starts it (e.g., motion detected)
  • Condition (optional): extra checks that must be true (e.g., only during working hours)
  • Action: what to do (e.g., send a notification, increment a counter, call a service)

You will use a built-in Home Assistant feature called a Counter helper to count motion events, and an automation to increment it every time motion is detected. This is entirely inside Home Assistant so no Python code needed.

Create the counter

  1. Go to SettingsDevices & ServicesHelpers tab
  2. Click + Create Helper → choose Counter
  3. Configure it:
    • Name: Wastebin Motion Count
    • Icon: mdi:motion-sensor (or pick one you like)
    • Initial value: 0
    • Step: 1
    • Restore: checked (so it survives restarts)
  4. Click Create

You now have an entity called counter.wastebin_motion_count that starts at 0 and can be incremented, decremented, or reset through Home Assistant services.

Create the automation to increment it

  1. Go to SettingsAutomations & ScenesCreate AutomationCreate new automation
  2. Give it a name: Count motion events
  3. Add Trigger → choose State → select your PIR Motion Sensor entity → set “To” to detected
  4. Skip conditions for now
  5. Add Action → choose Call Service → select counter.increment
    • Target: select counter.wastebin_motion_count
  6. Save the automation

Now trigger motion events. Every time the PIR sensor detects motion, the counter increments by one. You can see it updating on the dashboard in real time.

Add a notification automation

Create a second automation that sends a notification when motion is detected:

  1. Create Automation → name it Motion alert
  2. Add TriggerState → PIR Motion Sensor → “To” detected
  3. Add ActionCall Servicepersistent_notification.create
    • Message: Motion detected at Smart Wastebin 01 — {{ now().strftime('%H:%M:%S') }}
    • Title: Wastebin Alert
  4. Save

Trigger motion. A notification should appear in the Home Assistant sidebar under the bell icon.

Try a more useful automation

The two automations above is a small demonstration. Now implement at least one automation that is more realistic. Some ideas:

  • High activity alert: add a Condition e.g only fire if the counter is above a threshold (e.g., counter.wastebin_motion_count state is above 50). Use a Numeric state condition. This simulates an alert for a heavily-used bin.
  • Working hours only: add a Time condition so alerts only fire between 08:00 and 20:00.
  • Daily counter reset: create a Time-triggered automation that fires at midnight and calls counter.reset on the motion counter. This gives you a daily count.

View the YAML

You can inspect any automation as YAML. Go to the automation, click the three dots (⋮), and select Edit in YAML. This shows the underlying structure:

alias: Count motion events
description: Increment the wastebin motion counter on each detection
triggers:
etc.

Copy the YAML of your automations into your report.

Part 7 — Build a simple dashboard

Home Assistant’s default Overview dashboard automatically shows your entities, but you can create a custom dashboard that presents the information more clearly.

  1. Go to Overview → click the three dots (⋮) → Edit Dashboard
  2. Click + Add Card
  3. Try different card types:
    • Entity card for the motion sensor (shows detected/clear with color)
    • Entities card to group several entities together (status, last motion, counter)
    • History Graph card to show motion state changes over time
    • Gauge card for the motion counter
  4. Arrange the cards to create a meaningful view of your wastebin

Build a dashboard that shows at minimum:

  • The motion sensor state (detected / clear) with its history
  • The motion event counter
  • The wastebin status with its attributes
  • A history graph showing activity over time

Take a screenshot of your dashboard for the report.

Why this lab is structured this way

Building a dashboard and state management system from scratch would take weeks. Home Assistant gives you all of that out of the box with its entity registry, state history, a responsive web UI, automations, and dozens of integrations. The trade-off is that you are working within someone else’s framework, which means learning its conventions (MQTT discovery topics, device classes, value templates).

But this is exactly how real IoT systems work. You rarely build everything from scratch. You build the edge logic (sensing, processing, pipeline) and connect it to a platform that handles visualization, alerting, and management. Home Assistant is one such platform. AWS IoT Core, Azure IoT Hub, and ThingsBoard are others. The pattern is the same: publish structured data to a broker, let the platform consume it.

Report questions

Answer the following in your labs/lab07/README.md after the implementation and experiments are complete.

Home Assistant basics

RQ1: What is Home Assistant and what problem does it solve? Why use it instead of building a custom dashboard?
RQ2: What is the difference between the “Home Assistant OS” and “Home Assistant Container” installation methods? Why did we use the Container method?
RQ3: What is an entity in Home Assistant? Give three examples of entities in your setup and their current states.

MQTT integration

RQ4: How does Home Assistant learn about your sensors? Explain the MQTT discovery mechanism, what topic do you publish to, and what does the payload contain?
RQ5: Why should discovery messages be published with the retain flag (-r)?
RQ6: What is the device block in a discovery message? What happens in the Home Assistant UI when multiple entities share the same device.identifiers?
RQ7: What is the difference between a state_topic and a json_attributes_topic? When would you use each?

Entity design

RQ8: List all the entities you created. For each one, give: the entity type (binary_sensor, sensor, counter, etc.), the state topic (if MQTT-based), and why you chose that type.
RQ9: What device_class did you use for your motion sensor? What does the device class affect in the Home Assistant UI?
RQ10: What additional entities did you create beyond the minimum? Why did you choose those?
RQ11: How did you group your entities under devices? Draw or describe the device → entity hierarchy.

Automations and counter

RQ12: How does the Home Assistant Counter helper work? What services can you call on it?
RQ13: Paste the YAML of your “Count motion events” automation. Explain each part (trigger, condition, action).
RQ14: What other automation(s) did you create? Paste the YAML and explain the trigger, condition (if any), and action.
RQ15: Give one example of an automation that would be useful in a real Smart Wastebin deployment that involves a condition (not just trigger → action). Describe the trigger, the condition, and the action.

Pipeline integration

RQ16: Your producer now publishes to two kinds of topics: the data topic (full JSON events for the consumer) and the HA state topics (simple values for Home Assistant). Why not use the same topic for both?
RQ17: Show a screenshot of your Home Assistant dashboard with your wastebin entities visible.
RQ18: What happens in Home Assistant when the producer is stopped? Does the motion sensor show “unavailable”, “clear”, or something else? How could you improve this?

Reflection

RQ19: Compare the effort of building a custom web dashboard vs. using Home Assistant. What do you gain? What do you give up?
RQ20: Home Assistant runs locally on the Pi, no cloud needed. Why does this matter for an edge IoT deployment?
RQ21: If your project had 10 wastebins with 3 sensors each, how would the MQTT discovery approach scale compared to manually configuring 30 entities?

Project hint: Smart Wastebin

Home Assistant is going to be the operational interface for your project.

The counter and automations you built are a preview of what the full system can do. Think about:

  • A fill-level gauge that changes color as the bin fills up
  • An automation that sends a notification when fill level exceeds 80%
  • A daily report automation that logs the total motion events and resets the counter
  • A “last emptied” timestamp that updates when maintenance marks a bin as emptied

Think about what your final dashboard should show. A facilities manager looking at it should be able to answer at a glance: which bins need emptying? Which sensors are offline? When was each bin last serviced? What is the trend in usage over the past week?


What should be finished before you leave the lab

Before the end of the session you should have: installed and configured Home Assistant in Docker, connected Home Assistant to your Mosquitto broker via the MQTT integration, created entity discovery messages for at least a motion sensor, wastebin status, and a last-motion timestamp, tested entity creation and state updates from the terminal, modified your producer to publish HA discovery messages on startup and state updates during operation, created a Counter helper and an automation that increments it on motion, created at least one additional automation (notification, high-activity alert, daily reset, or similar), built a simple dashboard showing your wastebin entities, updated labs/lab07/README.md with code, screenshots, and report answers, and pushed to GitHub.

Final checklist (Lab 07)

  • Home Assistant running in Docker on the Pi
  • Home Assistant accessible via browser at port 8123
  • MQTT integration configured and connected to Mosquitto
  • MQTT discovery messages published for motion sensor entity
  • MQTT discovery messages published for wastebin status entity
  • MQTT discovery messages published for at least one additional entity
  • Entities appear in Home Assistant UI with correct states
  • producer.py publishes discovery messages on startup
  • producer.py publishes state updates to HA topics during operation
  • Counter helper created for motion events
  • Automation created that increments counter on motion detection
  • At least one additional automation created and tested
  • Automation YAML included in report
  • Simple dashboard built showing wastebin entities
  • Dashboard screenshot included in report
  • labs/lab07/README.md contains code, screenshots, and report answers
  • Commit and push completed

Deliverables and submission

What must exist in the repository (by end of lab)

/
├── README.md
├── labs/
│   ├── lab01/
│   ├── ...
│   └── lab07/
│       ├── README.md
│       ├── requirements.txt
│       ├── producer.py
│       ├── consumer.py
│       └── pirlib/
│           ├── __init__.py
│           ├── sampler.py
│           └── interpreter.py

Do not include:

  • venv/
  • __pycache__/
  • *.pyc
  • output/ or *.jsonl
  • Home Assistant config directory
  • large temporary files unless explicitly requested

What labs/lab07/README.md must contain

Two clearly separated parts:

  1. Code / runbook — include your discovery message payloads, topic structure, automation YAML, how to run the system, and dashboard screenshots
  2. Answers to report questions

Same style as previous labs.


End of lab session — GitHub checkpoint

Before leaving:

  • commit your progress
  • push to your team GitHub repository

Minimum expectation:

  • all deliverables tracked by Git
  • latest commit pushed
  • commit message is clear

Before next lab — eClass submission

Submit both:

  1. Code archive (.zip)
  2. PDF export of labs/lab07/README.md

Required PDF filename format:

  • lab07_REPORT_<team>.pdf

What follows is a greek version of the same lab

Προθεσμίες:

  • Τέλος εργαστηριακής συνεδρίας (GitHub checkpoint): κάντε commit & push την πρόοδό σας στο αποθετήριο της ομάδας σας.
  • Πριν το επόμενο εργαστήριο (υποβολή στο eClass): ανεβάστε (1) ένα .zip με τον κώδικά σας και (2) ένα PDF export του labs/lab07/README.md.

Περιεχόμενο υποβολής:

  • (1) ένα .zip με τον κώδικά σας, και
  • (2) ένα PDF export του labs/lab07/README.md.

Εισαγωγή στο τι πρέπει να κάνετε

Μέχρι τώρα έχετε φτιάξει ένα pipeline ανίχνευσης κίνησης, το έχετε πακετάρει σε Docker, έχετε μοντελοποιήσει τα δεδομένα σας με JSON-LD, και έχετε συνδέσει τα components μέσω MQTT. Το σύστημα λειτουργεί, αλλά κανείς εκτός της ομάδας σας δεν μπορεί να δει τι κάνει ο κάδος απορριμμάτων εκτός αν ανοίξει ένα terminal και διαβάσει log files. Σε ένα πραγματικό deployment, θα χρειαζόταν ένας τρόπος για μη τεχνικά άτομα να βλέπουν τι συμβαίνει. Ένας υπεύθυνος εγκαταστάσεων θέλει να ρίξει μια ματιά σε ένα dashboard και να ξέρει ποιοι κάδοι είναι γεμάτοι. Μια ομάδα συντήρησης θέλει να λάβει ειδοποίηση όταν ένας sensor πάει offline. Το να φτιάξετε αυτό το είδος interface από μηδενός είναι πολλή δουλειά — και το μεγαλύτερο μέρος της έχει ήδη γίνει.

Γι’ αυτό θα χρησιμοποιήσουμε το Home Assistant.

Τι είναι το Home Assistant;

Το Home Assistant είναι μια open-source πλατφόρμα home automation. Τρέχει τοπικά (στη συσκευή σας, όχι στο cloud) και παρέχει ένα web-based dashboard όπου μπορείτε να βλέπετε την κατάσταση sensors, να ελέγχετε συσκευές, να δημιουργείτε automations, και να φτιάχνετε dashboards — όλα μέσα από browser.

Σχεδιασμένο αρχικά για έξυπνα σπίτια (φώτα, θερμοστάτες, κλειδαριές πόρτας), το Home Assistant έχει γίνει επίσης ευρέως χρησιμοποιούμενο στο IoT prototyping γενικά, γιατί λύνει πολλά προβλήματα ταυτόχρονα:

  • Device management: διατηρεί ένα registry όλων των συσκευών και sensors σας, κάθε μία με τρέχουσα κατάσταση και ιστορικό
  • Dashboards: παρέχει drag-and-drop UI για τη δημιουργία οπτικών dashboards χωρίς να γράφετε frontend κώδικα
  • Automations: μπορείτε να ορίσετε κανόνες όπως “αν ο κάδος είναι πάνω από 80% γεμάτος, στείλε ειδοποίηση”
  • Integrations: μιλά δεκάδες πρωτόκολλα out of the box, συμπεριλαμβανομένων MQTT, HTTP, Zigbee, Z-Wave, Bluetooth, και άλλα
  • History and logging: αποθηκεύει αλλαγές κατάστασης με την πάροδο του χρόνου, ώστε να μπορείτε να βλέπετε τάσεις και να εντοπίζετε προβλήματα

Ο λόγος που το χρησιμοποιούμε σε αυτό το μάθημα είναι ότι σας δίνει μια πλήρως λειτουργική IoT πλατφόρμα που μπορείτε να τρέξετε στο Raspberry Pi σας. Αντί να φτιάξετε από το μηδέν ένα dashboard, έναν state manager, έναν automation engine, και μια history database, τα παίρνετε όλα δωρεάν. Η δουλειά σας είναι να συνδέσετε το pipeline σας σε αυτό.

Το Home Assistant οργανώνει τα πάντα γύρω από entities. Ένα entity είναι οτιδήποτε έχει κατάσταση (π.χ. ένας temperature sensor που δείχνει 23°C, ένα φως που είναι ανοιχτό ή κλειστό, ένας motion sensor που είναι detected ή clear, ένας κάδος που είναι 72% γεμάτος). Κάθε entity έχει μοναδικό ID, ένα φιλικό όνομα, τρέχουσα κατάσταση, και προαιρετικά ένα σύνολο attributes (επιπλέον δεδομένα συνδεδεμένα με την κατάσταση). Το dashboard εμφανίζει entities. Τα automations ενεργούν σε entities. Το history παρακολουθεί entities.

Το task σας σε αυτό το εργαστήριο είναι να κάνετε το Smart Wastebin σας να εμφανιστεί ως ένα σύνολο entities μέσα στο Home Assistant. Ο motion sensor γίνεται entity. Ο ίδιος ο κάδος απορριμμάτων γίνεται entity. Όταν το pipeline σας ανιχνεύει κίνηση, το Home Assistant πρέπει να το εμφανίζει. Όταν αλλάζει η κατάσταση, το dashboard πρέπει να ενημερώνεται. Όλα αυτά γίνονται μέσω MQTT.


Μέρος 1 — Εκτέλεση του Home Assistant σε Docker

Θα τρέξετε το Home Assistant ως Docker container στο Raspberry Pi σας. Αυτό ονομάζεται μέθοδος εγκατάστασης “Home Assistant Container”. Σας δίνει τον πλήρη Home Assistant core χωρίς να καταλαμβάνει ολόκληρο το λειτουργικό σύστημα του Pi σας.

Δημιουργήστε έναν κατάλογο για τη ρύθμιση του Home Assistant:

mkdir -p ~/homeassistant/config

Τρέξτε το Home Assistant:

docker run -d \
  --name homeassistant \
  --restart unless-stopped \
  -v ~/homeassistant/config:/config \
  -v /run/dbus:/run/dbus:ro \
  --network host \
  ghcr.io/home-assistant/home-assistant:stable

Μερικά πράγματα για αυτή την εντολή:

Το --network host δίνει στο container πλήρη πρόσβαση στο δίκτυο του host. Το Home Assistant το χρειάζεται για να ανακαλύπτει συσκευές στο τοπικό δίκτυο και να επικοινωνεί με τον Mosquitto broker σας στο localhost:1883.

Το -v ~/homeassistant/config:/config κάνει mount έναν τοπικό κατάλογο για όλη τη ρύθμιση του Home Assistant. Εδώ αποθηκεύει τη βάση δεδομένων, τα dashboards, τα automations, και όλη την κατάσταση entity. Επειδή είναι mounted από τον host, η ρύθμισή σας επιβιώνει από επανεκκινήσεις και rebuilds container.

Το --restart unless-stopped εξασφαλίζει ότι το Home Assistant ξεκινά αυτόματα αν το Pi κάνει reboot.

Η πρώτη εκκίνηση χρειάζεται μερικά λεπτά γιατί το Home Assistant αρχικοποιεί τη βάση δεδομένων και την προεπιλεγμένη ρύθμιση. Περιμένετε να είναι έτοιμο:

docker logs -f homeassistant

Παρακολουθήστε για γραμμή σαν Home Assistant initialized in X.Xs ή Frontend loaded. Μετά ανοίξτε browser και πηγαίνετε στο:

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

Αν εργάζεστε απευθείας στο Pi, χρησιμοποιήστε http://localhost:8123.

Θα δείτε την οθόνη onboarding του Home Assistant. Δημιουργήστε λογαριασμό (είναι τοπικός, όχι cloud-based):

  1. Ορίστε όνομα, username, και password — θυμηθείτε τα
  2. Ορίστε την τοποθεσία σας (χρησιμοποιείται για καιρό και ζώνη ώρας)
  3. Παραλείψτε οποιαδήποτε αυτόματη ανακάλυψη συσκευών προς το παρόν
  4. Κάντε κλικ για να φτάσετε στο κύριο dashboard

Θα πρέπει τώρα να βλέπετε το Home Assistant dashboard, ως επί το πλείστον άδειο, αλλά λειτουργικό. Αφιερώστε μια στιγμή για να κάνετε κλικ τριγύρω. Κοιτάξτε το sidebar: Overview (το dashboard), Settings, Developer Tools. Θα χρησιμοποιήσετε όλα αυτά.


Μέρος 2 — Σύνδεση του Home Assistant στον MQTT broker σας

Το Home Assistant πρέπει να μάθει για τον Mosquitto broker σας. Θα προσθέσετε το MQTT integration μέσω του UI.

Πρώτα, βεβαιωθείτε ότι ο Mosquitto broker τρέχει και επιτρέπει ανώνυμες συνδέσεις (ή ρυθμίστε credentials — αλλά για αυτό το εργαστήριο, το anonymous είναι εντάξει). Αν εγκαταστήσατε το Mosquitto στο Lab 06, θα πρέπει ήδη να τρέχει.

Ελέγξτε ότι το Mosquitto είναι ρυθμισμένο να δέχεται εξωτερικές συνδέσεις. Δημιουργήστε ή επεξεργαστείτε το configuration file του Mosquitto:

# Αν τρέχετε Mosquitto ως system service:
sudo nano /etc/mosquitto/conf.d/default.conf

Προσθέστε:

listener 1883
allow_anonymous true

Επανεκκινήστε το Mosquitto:

sudo systemctl restart mosquitto

Τώρα προσθέστε το MQTT integration στο Home Assistant:

  1. Πηγαίνετε στο SettingsDevices & ServicesAdd Integration
  2. Αναζητήστε MQTT
  3. Επιλέξτε MQTT
  4. Εισάγετε τη διεύθυνση του broker:
    • Broker: localhost (ή την IP του Pi σας αν τρέχει σε ξεχωριστό container)
    • Port: 1883
    • Αφήστε username και password κενά (anonymous)
  5. Κάντε κλικ Submit

Το Home Assistant πρέπει να συνδεθεί επιτυχώς. Έχετε τώρα μια γέφυρα μεταξύ του MQTT broker σας και του Home Assistant — οτιδήποτε δημοσιεύεται στον broker μπορεί να γίνει entity στο HA.

Επαλήθευση της σύνδεσης

Πηγαίνετε στο SettingsDevices & Services → καρτέλα MQTT (μπορεί να χρειαστεί να ψάξετε κάτω από “Listen to a topic”). Κάντε subscribe στο # (όλα τα topics) και κάντε publish ένα test μήνυμα:

  • Topic: test/ha
  • Payload: hello from Home Assistant

Θα πρέπει να δείτε το μήνυμα να εμφανίζεται. Μπορείτε επίσης να κάνετε publish από το terminal:

mosquitto_pub -h localhost -t "test/from-terminal" -m "hello from terminal"

Και θα πρέπει να εμφανιστεί στον Home Assistant MQTT listener. Η γέφυρα λειτουργεί.


Μέρος 3 — Κατανόηση του MQTT Discovery

Το Home Assistant υποστηρίζει μια λειτουργία που ονομάζεται MQTT Discovery. Αντί να ρυθμίζετε χειροκίνητα κάθε entity σε YAML αρχεία, μπορείτε να έχετε τις συσκευές σας να ανακοινώνουν τον εαυτό τους κάνοντας publish ένα ειδικό configuration message σε ένα γνωστό topic. Όταν το Home Assistant δει αυτό το μήνυμα, δημιουργεί αυτόματα το entity.

Το discovery topic ακολουθεί αυτό το pattern:

homeassistant/<component>/<object_id>/config

Όπου:

  • <component> είναι ο τύπος entity: binary_sensor, sensor, switch, device_tracker, κ.λπ.
  • <object_id> είναι ένα μοναδικό αναγνωριστικό που επιλέγετε εσείς για αυτό το entity
  • Το payload είναι ένα JSON object που περιγράφει τις ιδιότητες του entity

Για παράδειγμα, για να δημιουργήσετε ένα motion sensor entity, κάνετε publish ένα configuration message στο:

homeassistant/binary_sensor/pir_01_motion/config

Με payload όπως:

{
  "name": "PIR Motion Sensor",
  "state_topic": "smartbin/bin-01/pir-01/motion",
  "payload_on": "detected",
  "payload_off": "clear",
  "device_class": "motion",
  "unique_id": "pir_01_motion",
  "device": {
    "identifiers": ["pir-01"],
    "name": "PIR Sensor 01",
    "model": "HC-SR501",
    "manufacturer": "Generic"
  }
}

Αυτό λέει στο Home Assistant:

  • Δημιούργησε ένα binary sensor (κάτι που είναι είτε on είτε off)
  • Η κατάστασή του προέρχεται από το MQTT topic smartbin/bin-01/pir-01/motion
  • Όταν αυτό το topic λαμβάνει "detected", το entity είναι ON· όταν λαμβάνει "clear", είναι OFF
  • Έχει device class motion, οπότε το HA ξέρει να χρησιμοποιεί το σωστό εικονίδιο και διατύπωση
  • Ανήκει σε μια συσκευή που ονομάζεται “PIR Sensor 01” — αυτό ομαδοποιεί entities μαζί στο UI

Το block device είναι σημαντικό. Λέει στο Home Assistant ότι αυτό το entity ανήκει σε μια φυσική συσκευή. Αν αργότερα προσθέσετε περισσότερα entities στην ίδια συσκευή (όπως ένας sensor επιπέδου μπαταρίας), όλα θα εμφανίζονται ομαδοποιημένα κάτω από “PIR Sensor 01” στο UI.

Δοκιμάστε το από το terminal:

mosquitto_pub -h localhost -t "homeassistant/binary_sensor/pir_01_motion/config" -r -m '{
  "name": "PIR Motion Sensor",
  "state_topic": "smartbin/bin-01/pir-01/motion",
  "payload_on": "detected",
  "payload_off": "clear",
  "device_class": "motion",
  "unique_id": "pir_01_motion",
  "device": {
    "identifiers": ["pir-01"],
    "name": "PIR Sensor 01",
    "model": "HC-SR501",
    "manufacturer": "Generic"
  }
}'

Σημειώστε το flag -r — το discovery message πρέπει να είναι retained ώστε το Home Assistant να το παραλαμβάνει ακόμα και αν επανεκκινηθεί μετά τη δημοσίευση του μηνύματος.

Τώρα κάντε publish ένα state update:

mosquitto_pub -h localhost -t "smartbin/bin-01/pir-01/motion" -m "detected"

Πηγαίνετε στο Home Assistant dashboard. Θα πρέπει να δείτε ένα νέο entity: PIR Motion Sensor, που εμφανίζει “Detected”. Κάντε publish "clear" στο ίδιο topic και παρακολουθήστε το να ενημερώνεται.

mosquitto_pub -h localhost -t "smartbin/bin-01/pir-01/motion" -m "clear"

Ο Python κώδικάς σας θα κάνει το ίδιο πράγμα μέσω προγραμματισμού.


Μέρος 4 — Δημιουργία του Smart Wastebin entity

Τώρα δημιουργήστε ένα πιο πλήρες σύνολο entities που αντιπροσωπεύουν το Smart Wastebin σας. Πρέπει να σκεφτείτε ποιες πληροφορίες θέλετε να εκθέσετε στο Home Assistant και πώς να τις δομήσετε.

Κατ’ ελάχιστον, δημιουργήστε αυτά τα entities:

Ο motion sensor (binary sensor)

Αυτό το κάνατε ήδη στο Μέρος 3. Βελτιώστε το αν χρειάζεται.

Ο ίδιος ο κάδος απορριμμάτων (ως device με attributes)

Δημιουργήστε ένα sensor entity που αντιπροσωπεύει τη συνολική κατάσταση του κάδου. Εδώ μπορείτε να προσαρτήσετε attributes με επιπλέον πληροφορίες:

mosquitto_pub -h localhost -t "homeassistant/sensor/wastebin_01_status/config" -r -m '{
  "name": "Wastebin Status",
  "state_topic": "smartbin/bin-01/status",
  "value_template": "{{ value_json.state }}",
  "json_attributes_topic": "smartbin/bin-01/status",
  "unique_id": "wastebin_01_status",
  "device": {
    "identifiers": ["bin-01"],
    "name": "Smart Wastebin 01",
    "model": "Smart Wastebin v1",
    "manufacturer": "ECE CK801 Team"
  }
}'

Μετά κάντε publish κατάσταση με attributes:

mosquitto_pub -h localhost -t "smartbin/bin-01/status" -m '{
  "state": "active",
  "location": "Lab Room 101",
  "last_motion": "2026-04-10T14:32:01Z",
  "total_events_today": 42
}'

Το value_template εξάγει το field state από το JSON payload για να χρησιμοποιήσει ως κύρια κατάσταση του entity. Το json_attributes_topic λέει στο HA να διαβάζει τα υπόλοιπα JSON fields ως attributes — οπότε τα location, last_motion, και total_events_today εμφανίζονται όλα ως επιπλέον πληροφορίες στο entity.

Ένας μετρητής motion events (sensor)

Δημιουργήστε ένα sensor που παρακολουθεί πόσα motion events έχουν ανιχνευθεί:

mosquitto_pub -h localhost -t "homeassistant/sensor/wastebin_01_motion_count/config" -r -m '{
  "name": "Motion Event Count",
  "state_topic": "smartbin/bin-01/pir-01/event_count",
  "unit_of_measurement": "events",
  "icon": "mdi:motion-sensor",
  "unique_id": "wastebin_01_motion_count",
  "device": {
    "identifiers": ["bin-01"],
    "name": "Smart Wastebin 01"
  }
}'

Παρατηρήστε ότι αυτό το entity χρησιμοποιεί τα ίδια device.identifiers με την κατάσταση κάδου ["bin-01"]. Αυτό λέει στο Home Assistant ότι ο μετρητής κίνησης ανήκει στην ίδια συσκευή με τον status sensor. Θα εμφανίζονται ομαδοποιημένα κάτω από “Smart Wastebin 01” στο UI.

Σκεφτείτε ποια άλλα entities θα ήταν χρήσιμα. Μερικές ιδέες:

  • Last motion time — ένας sensor που εμφανίζει πότε έγινε το τελευταίο motion event
  • Pipeline latency — ένας sensor που εμφανίζει την τρέχουσα pipeline latency σε ms
  • Sensor online/offline — ένας binary sensor που παρακολουθεί αν ο PIR sensor είναι ζωντανός
  • Lid state — ένας binary sensor (open/closed) αν μοντελοποιείτε το καπάκι ως ενεργοποιούμενο από κίνηση

Δεν χρειάζεται να υλοποιήσετε όλα αυτά, αλλά σκεφτείτε ποια προσθέτουν αξία και υλοποιήστε τουλάχιστον ένα ή δύο πέρα από τα ελάχιστα.


Μέρος 5 — Publish από το pipeline σας

Τώρα τροποποιήστε το producer.py σας από το Lab 06 ώστε όχι μόνο να κάνει publish raw events στο data topic σας, αλλά και να ενημερώνει τα Home Assistant entities.

Υπάρχουν δύο πράγματα να κάνετε:

1. Publish discovery messages κατά την εκκίνηση

Όταν ξεκινά ο producer, πρέπει να κάνει publish τα MQTT discovery configuration messages (retained) ώστε το Home Assistant να ξέρει για τα entities. Αυτή είναι μια εφάπαξ ρύθμιση που γίνεται κατά την εκκίνηση:

Import JSON handling utilities

Ορίστε μια συνάρτηση για publish discovery πληροφοριών:
    Δέχεται MQTT client, αναγνωριστικό container, και αναγνωριστικό συσκευής

    Δημιουργήστε configuration object για motion sensor:
        Ορίστε display name
        Φτιάξτε το state topic χρησιμοποιώντας τα αναγνωριστικά
        Ορίστε τιμές για active και inactive states
        Αναθέστε sensor classification (motion)
        Δημιουργήστε μοναδικό αναγνωριστικό
        Προσθέστε device metadata:
            Συμπεριλάβετε λίστα identifiers
            Συμπεριλάβετε device name
            Συμπεριλάβετε model και manufacturer

    Μετατρέψτε το configuration object σε JSON string

    Κάντε publish το JSON string σε discovery topic:
        Κατασκευάστε το topic path χρησιμοποιώντας το αναγνωριστικό συσκευής
        Χρησιμοποιήστε κατάλληλες εγγυήσεις παράδοσης
        Σημειώστε το μήνυμα ως retained

    Σημείωση:
        Επιπλέον sensors ή entities ακολουθούν το ίδιο pattern:
            Δημιουργία configuration
            Σειριοποίηση
            Publish στο αντίστοιχο discovery topic

Καλέστε αυτή τη συνάρτηση μετά τη σύνδεση στον broker, πριν ξεκινήσει το κύριο sensing loop.

2. Publish state updates παράλληλα με events

Στο main loop σας, όταν ανιχνεύεται motion event, κάντε publish τόσο στο data topic σας (για τον JSONL consumer) όσο και στα Home Assistant state topics:

# Κάντε publish το πλήρες event στο data topic (για τον consumer / JSONL logger)


# Κάντε publish τη simple κατάσταση στο HA entity topic

Όταν η κίνηση σταματά (cooldown τελειώνει, κατάσταση επιστρέφει σε idle).

Το data topic μεταφέρει το πλήρες JSON event record (timestamps, seq, latency, JSON-LD context, τα πάντα). Το HA state topic μεταφέρει απλώς την απλή τιμή που περιμένει το Home Assistant ("detected" ή "clear").

Τρέξτε το και παρακολουθήστε

  1. Βεβαιωθείτε ότι το Mosquitto τρέχει
  2. Βεβαιωθείτε ότι το Home Assistant τρέχει
  3. Ξεκινήστε τον producer με HA discovery ενεργοποιημένο
  4. Ανοίξτε το Home Assistant dashboard στο browser σας
  5. Προκαλέστε motion events και παρακολουθήστε το entity να ενημερώνεται σε πραγματικό χρόνο

Πηγαίνετε στο SettingsDevices & ServicesMQTTDevices. Θα πρέπει να βλέπετε τις συσκευές σας στη λίστα. Κάντε κλικ σε μία για να δείτε όλα τα entities και τις τρέχουσες καταστάσεις τους.


Μέρος 6 — Δημιουργία motion counter με automations

Το Home Assistant δεν είναι απλώς τρόπος οπτικοποίησης πραγμάτων — μπορεί να ενεργεί σε αλλαγές κατάστασης. Ένα automation είναι ένας κανόνας που λέει “όταν συμβεί αυτό, κάνε εκείνο.” Κάθε automation έχει τρία μέρη:

  • Trigger: ποιο event το ξεκινά (π.χ. ανίχνευση κίνησης)
  • Condition (προαιρετικό): επιπλέον ελέγχοι που πρέπει να είναι αληθείς (π.χ. μόνο κατά τις ώρες εργασίας)
  • Action: τι να κάνει (π.χ. αποστολή ειδοποίησης, αύξηση μετρητή, κλήση service)

Θα χρησιμοποιήσετε μια built-in λειτουργία του Home Assistant που ονομάζεται Counter helper για να μετράτε motion events, και ένα automation για να τον αυξάνει κάθε φορά που ανιχνεύεται κίνηση. Αυτό γίνεται εξ ολοκλήρου μέσα στο Home Assistant — δεν χρειάζεται Python κώδικας.

Δημιουργήστε τον counter

  1. Πηγαίνετε στο SettingsDevices & Services → καρτέλα Helpers
  2. Κάντε κλικ + Create Helper → επιλέξτε Counter
  3. Ρυθμίστε τον:
    • Name: Wastebin Motion Count
    • Icon: mdi:motion-sensor (ή επιλέξτε ένα που σας αρέσει)
    • Initial value: 0
    • Step: 1
    • Restore: checked (ώστε να επιβιώνει από επανεκκινήσεις)
  4. Κάντε κλικ Create

Έχετε τώρα ένα entity που ονομάζεται counter.wastebin_motion_count που ξεκινά από 0 και μπορεί να αυξηθεί, να μειωθεί, ή να γίνει reset μέσω Home Assistant services.

Δημιουργήστε το automation για να τον αυξάνει

  1. Πηγαίνετε στο SettingsAutomations & ScenesCreate AutomationCreate new automation
  2. Δώστε του όνομα: Count motion events
  3. Add Trigger → επιλέξτε State → επιλέξτε το entity PIR Motion Sensor → ορίστε “To” σε detected
  4. Παραλείψτε conditions για τώρα
  5. Add Action → επιλέξτε Call Service → επιλέξτε counter.increment
    • Target: επιλέξτε counter.wastebin_motion_count
  6. Αποθηκεύστε το automation

Τώρα προκαλέστε motion events. Κάθε φορά που ο PIR sensor ανιχνεύει κίνηση, ο counter αυξάνεται κατά ένα. Μπορείτε να τον βλέπετε να ενημερώνεται στο dashboard σε πραγματικό χρόνο.

Προσθέστε ένα notification automation

Δημιουργήστε ένα δεύτερο automation που στέλνει ειδοποίηση όταν ανιχνεύεται κίνηση:

  1. Create Automation → ονομάστε το Motion alert
  2. Add TriggerState → PIR Motion Sensor → “To” detected
  3. Add ActionCall Servicepersistent_notification.create
    • Message: Motion detected at Smart Wastebin 01 — {{ now().strftime('%H:%M:%S') }}
    • Title: Wastebin Alert
  4. Αποθηκεύστε

Προκαλέστε κίνηση. Μια ειδοποίηση πρέπει να εμφανιστεί στο Home Assistant sidebar κάτω από το εικονίδιο καμπάνας.

Δοκιμάστε ένα πιο χρήσιμο automation

Τα δύο παραπάνω automations είναι μια μικρή επίδειξη. Τώρα υλοποιήστε τουλάχιστον ένα automation που είναι πιο ρεαλιστικό. Μερικές ιδέες:

  • High activity alert: προσθέστε Condition π.χ. μόνο να ενεργοποιείται αν ο counter είναι πάνω από κατώφλι (π.χ. κατάσταση counter.wastebin_motion_count πάνω από 50). Χρησιμοποιήστε Numeric state condition. Αυτό προσομοιώνει ειδοποίηση για έναν πολυχρησιμοποιούμενο κάδο.
  • Working hours only: προσθέστε Time condition ώστε οι ειδοποιήσεις να ενεργοποιούνται μόνο μεταξύ 08:00 και 20:00.
  • Daily counter reset: δημιουργήστε automation που ενεργοποιείται βάσει Time στα μεσάνυχτα και καλεί counter.reset στον motion counter. Αυτό σας δίνει ημερήσια καταμέτρηση.

Δείτε το YAML

Μπορείτε να επιθεωρήσετε οποιοδήποτε automation ως YAML. Πηγαίνετε στο automation, κάντε κλικ στις τρεις τελείες (⋮), και επιλέξτε Edit in YAML. Αυτό εμφανίζει την υποκείμενη δομή:

alias: Count motion events
description: Increment the wastebin motion counter on each detection
triggers:
etc.

Αντιγράψτε το YAML των automations σας στην αναφορά σας.


Μέρος 7 — Φτιάξτε ένα απλό dashboard

Το προεπιλεγμένο Overview dashboard του Home Assistant εμφανίζει αυτόματα τα entities σας, αλλά μπορείτε να δημιουργήσετε ένα custom dashboard που παρουσιάζει τις πληροφορίες πιο καθαρά.

  1. Πηγαίνετε στο Overview → κάντε κλικ στις τρεις τελείες (⋮) → Edit Dashboard
  2. Κάντε κλικ + Add Card
  3. Δοκιμάστε διαφορετικούς τύπους card:
    • Entity card για τον motion sensor (εμφανίζει detected/clear με χρώμα)
    • Entities card για να ομαδοποιήσετε αρκετά entities μαζί (status, last motion, counter)
    • History Graph card για να εμφανίσετε αλλαγές κατάστασης κίνησης με την πάροδο του χρόνου
    • Gauge card για τον motion counter
  4. Τακτοποιήστε τα cards για να δημιουργήσετε μια ουσιαστική εικόνα του κάδου σας

Φτιάξτε ένα dashboard που εμφανίζει κατ’ ελάχιστον:

  • Την κατάσταση του motion sensor (detected / clear) με το ιστορικό του
  • Τον motion event counter
  • Την κατάσταση του κάδου με τα attributes του
  • Ένα history graph που εμφανίζει δραστηριότητα με την πάροδο του χρόνου

Τραβήξτε screenshot του dashboard σας για την αναφορά.


Γιατί αυτό το εργαστήριο είναι δομημένο έτσι

Το να χτίσετε από το μηδέν ένα dashboard και ένα σύστημα state management θα χρειαζόταν εβδομάδες. Το Home Assistant σας δίνει όλα αυτά out of the box — entity registry, state history, responsive web UI, automations, και δεκάδες integrations. Το trade-off είναι ότι εργάζεστε εντός του framework κάποιου άλλου, που σημαίνει ότι μαθαίνετε τις συμβάσεις του (MQTT discovery topics, device classes, value templates).

Αλλά ακριβώς έτσι λειτουργούν τα πραγματικά IoT συστήματα. Σπάνια χτίζετε τα πάντα από μηδενός. Χτίζετε τη λογική edge (αίσθηση, επεξεργασία, pipeline) και τη συνδέετε σε μια πλατφόρμα που χειρίζεται visualization, ειδοποιήσεις, και διαχείριση. Το Home Assistant είναι μια τέτοια πλατφόρμα. Τα AWS IoT Core, Azure IoT Hub, και ThingsBoard είναι άλλα. Το pattern είναι το ίδιο: κάντε publish δομημένα δεδομένα σε έναν broker, αφήστε την πλατφόρμα να τα καταναλώσει.


Ερωτήσεις αναφοράς

Απαντήστε τα παρακάτω στο labs/lab07/README.md σας αφού ολοκληρωθεί η υλοποίηση και τα πειράματα.

Βασικές έννοιες Home Assistant

RQ1: Τι είναι το Home Assistant και ποιο πρόβλημα λύνει; Γιατί να το χρησιμοποιήσουμε αντί να φτιάξουμε custom dashboard;
RQ2: Ποια είναι η διαφορά μεταξύ των μεθόδων εγκατάστασης “Home Assistant OS” και “Home Assistant Container”; Γιατί χρησιμοποιήσαμε τη μέθοδο Container;
RQ3: Τι είναι ένα entity στο Home Assistant; Δώστε τρία παραδείγματα entities στη ρύθμισή σας και τις τρέχουσες καταστάσεις τους.

MQTT integration

RQ4: Πώς μαθαίνει το Home Assistant για τους sensors σας; Εξηγήστε τον μηχανισμό MQTT discovery — σε ποιο topic κάνετε publish και τι περιέχει το payload;
RQ5: Γιατί τα discovery messages πρέπει να δημοσιεύονται με το retain flag (-r);
RQ6: Τι είναι το block device σε ένα discovery message; Τι συμβαίνει στο Home Assistant UI όταν πολλαπλά entities μοιράζονται τα ίδια device.identifiers;
RQ7: Ποια είναι η διαφορά μεταξύ state_topic και json_attributes_topic; Πότε θα χρησιμοποιούσατε το καθένα;

Σχεδιασμός entity

RQ8: Αναφέρετε όλα τα entities που δημιουργήσατε. Για κάθε ένα, δώστε: τον τύπο entity (binary_sensor, sensor, counter, κ.λπ.), το state topic (αν βασίζεται σε MQTT), και γιατί επιλέξατε αυτόν τον τύπο.
RQ9: Τι device_class χρησιμοποιήσατε για τον motion sensor σας; Τι επηρεάζει το device class στο Home Assistant UI;
RQ10: Ποια επιπλέον entities δημιουργήσατε πέρα από τα ελάχιστα; Γιατί επιλέξατε αυτά;
RQ11: Πώς ομαδοποιήσατε τα entities σας κάτω από συσκευές; Σχεδιάστε ή περιγράψτε την ιεραρχία device → entity.

Automations και counter

RQ12: Πώς λειτουργεί ο Counter helper του Home Assistant; Ποια services μπορείτε να καλέσετε σε αυτόν;
RQ13: Επικολλήστε το YAML του automation “Count motion events”. Εξηγήστε κάθε μέρος (trigger, condition, action).
RQ14: Ποιο/ποια άλλα automation(s) δημιουργήσατε; Επικολλήστε το YAML και εξηγήστε το trigger, την condition (αν υπάρχει), και την action.
RQ15: Δώστε ένα παράδειγμα automation που θα ήταν χρήσιμο σε ένα πραγματικό Smart Wastebin deployment και που περιλαμβάνει condition (όχι απλώς trigger → action). Περιγράψτε το trigger, την condition, και την action.

Ενσωμάτωση pipeline

RQ16: Ο producer σας κάνει τώρα publish σε δύο είδη topics: το data topic (πλήρη JSON events για τον consumer) και τα HA state topics (απλές τιμές για το Home Assistant). Γιατί να μην χρησιμοποιήσετε το ίδιο topic και για τα δύο;
RQ17: Δείξτε screenshot του Home Assistant dashboard σας με τα wastebin entities ορατά.
RQ18: Τι συμβαίνει στο Home Assistant όταν σταματά ο producer; Εμφανίζει ο motion sensor “unavailable”, “clear”, ή κάτι άλλο; Πώς θα μπορούσατε να το βελτιώσετε αυτό;

Αναστοχασμός

RQ19: Συγκρίνετε την προσπάθεια δημιουργίας custom web dashboard έναντι χρήσης Home Assistant. Τι κερδίζετε; Τι χάνετε;
RQ20: Το Home Assistant τρέχει τοπικά στο Pi — δεν χρειάζεται cloud. Γιατί αυτό έχει σημασία για ένα edge IoT deployment;
RQ21: Αν το project σας είχε 10 κάδους απορριμμάτων με 3 sensors ο καθένας, πώς θα κλιμακωνόταν η προσέγγιση MQTT discovery σε σύγκριση με τη χειροκίνητη ρύθμιση 30 entities;


Υπόδειξη project: Smart Wastebin

Το Home Assistant θα είναι το λειτουργικό interface του project σας.

Ο counter και τα automations που φτιάξατε είναι μια προεπισκόπηση αυτών που μπορεί να κάνει το πλήρες σύστημα. Σκεφτείτε:

  • Ένα fill-level gauge που αλλάζει χρώμα καθώς γεμίζει ο κάδος
  • Ένα automation που στέλνει ειδοποίηση όταν το επίπεδο πλήρωσης ξεπεράσει το 80%
  • Ένα daily report automation που καταγράφει τα συνολικά motion events και κάνει reset τον counter
  • Ένα timestamp “last emptied” που ενημερώνεται όταν η συντήρηση σημειώνει ότι ένας κάδος αδειάστηκε

Σκεφτείτε τι πρέπει να εμφανίζει το τελικό dashboard σας. Ένας υπεύθυνος εγκαταστάσεων που το βλέπει πρέπει να μπορεί να απαντήσει με μια ματιά: ποιοι κάδοι χρειάζονται άδειασμα; Ποιοι sensors είναι offline; Πότε έγινε τελευταία η συντήρηση κάθε κάδου; Ποια είναι η τάση χρήσης τις τελευταίες εβδομάδες;


Τι πρέπει να έχει ολοκληρωθεί πριν φύγετε από το εργαστήριο

Πριν το τέλος της συνεδρίας θα πρέπει να έχετε: εγκαταστήσει και ρυθμίσει το Home Assistant σε Docker, συνδέσει το Home Assistant στον Mosquitto broker σας μέσω του MQTT integration, δημιουργήσει entity discovery messages τουλάχιστον για motion sensor, wastebin status, και last-motion timestamp, δοκιμάσει δημιουργία entity και state updates από το terminal, τροποποιήσει τον producer ώστε να κάνει publish HA discovery messages κατά την εκκίνηση και state updates κατά τη λειτουργία, δημιουργήσει Counter helper και automation που τον αυξάνει με κίνηση, δημιουργήσει τουλάχιστον ένα επιπλέον automation (ειδοποίηση, high-activity alert, daily reset, ή παρόμοιο), φτιάξει απλό dashboard που εμφανίζει τα wastebin entities σας, ενημερώσει το labs/lab07/README.md με κώδικα, screenshots, και απαντήσεις αναφοράς, και κάνει push στο GitHub.


Τελικό checklist (Lab 07)

  • Home Assistant σε λειτουργία σε Docker στο Pi
  • Home Assistant προσβάσιμο μέσω browser στη port 8123
  • MQTT integration ρυθμισμένο και συνδεδεμένο στο Mosquitto
  • MQTT discovery messages δημοσιευμένα για entity motion sensor
  • MQTT discovery messages δημοσιευμένα για entity wastebin status
  • MQTT discovery messages δημοσιευμένα για τουλάχιστον ένα επιπλέον entity
  • Entities εμφανίζονται στο Home Assistant UI με σωστές καταστάσεις
  • producer.py κάνει publish discovery messages κατά την εκκίνηση
  • producer.py κάνει publish state updates σε HA topics κατά τη λειτουργία
  • Counter helper δημιουργημένος για motion events
  • Automation δημιουργημένο που αυξάνει τον counter στην ανίχνευση κίνησης
  • Τουλάχιστον ένα επιπλέον automation δημιουργημένο και δοκιμασμένο
  • Automation YAML συμπεριλαμβάνεται στην αναφορά
  • Απλό dashboard φτιαγμένο που εμφανίζει wastebin entities
  • Screenshot dashboard συμπεριλαμβάνεται στην αναφορά
  • labs/lab07/README.md περιέχει κώδικα, screenshots, και απαντήσεις αναφοράς
  • Commit και push ολοκληρωμένα

Παραδοτέα και υποβολή

Τι πρέπει να υπάρχει στο αποθετήριο (έως το τέλος του εργαστηρίου)

/
├── README.md
├── labs/
│   ├── lab01/
│   ├── ...
│   └── lab07/
│       ├── README.md
│       ├── requirements.txt
│       ├── producer.py
│       ├── consumer.py
│       └── pirlib/
│           ├── __init__.py
│           ├── sampler.py
│           └── interpreter.py

Μην συμπεριλάβετε:

  • venv/
  • __pycache__/
  • *.pyc
  • output/ ή *.jsonl
  • Home Assistant config directory
  • μεγάλα προσωρινά αρχεία εκτός αν ζητηθεί ρητά

Τι πρέπει να περιέχει το labs/lab07/README.md

Δύο σαφώς διαχωρισμένα μέρη:

  1. Κώδικας / runbook — συμπεριλάβετε τα discovery message payloads σας, την topic structure, το automation YAML, πώς να τρέξετε το σύστημα, και screenshots dashboard
  2. Απαντήσεις στις ερωτήσεις αναφοράς

Ίδιο στυλ με τα προηγούμενα εργαστήρια.


Τέλος εργαστηριακής συνεδρίας — GitHub checkpoint

Πριν φύγετε:

  • κάντε commit την πρόοδό σας
  • κάντε push στο αποθετήριο GitHub της ομάδας σας

Ελάχιστη προσδοκία:

  • όλα τα παραδοτέα παρακολουθούνται από το Git
  • το τελευταίο commit έχει γίνει push
  • το commit message είναι σαφές

Πριν το επόμενο εργαστήριο — υποβολή στο eClass

Υποβάλετε και τα δύο:

  1. Αρχείο κώδικα (.zip)
  2. PDF export του labs/lab07/README.md

Απαιτούμενη μορφή ονόματος PDF αρχείου:

  • lab07_REPORT_<team>.pdf
Previous