Your Smart Trash Can project now involves processing raw IoT data to extract meaningful information. You’ve integrated a PIR motion sensor and have MQTT sensors defined in Home Assistant, but motion data alone doesn’t tell you how full the trash can is.
To determine the fill percentage, you need the volume of trash inserted. Instead of relying on a real volume sensor, you will simulate volume data. A Virtual Sensor will combine the motion detection events and the total volume inside the can to compute the can’s fill percentage. The virtual sensor:
- Accepts input data streams (motion, volume).
- Processes these data to derive valuable output (fill percentage).
- Publishes the result back into Home Assistant via MQTT.
Additional Attributes in Home Assistant
Previously, you set up MQTT sensors in Home Assistant for attributes like motion detection. Now, you will introduce an additional MQTT sensor for volume.
Defining MQTT Sensors in configuration.yaml
mqtt:
sensor:
- name: "Smart Trash Can Volume"
state_topic: "smart_trash_can/volume"
unit_of_measurement: "cm³"
sensor.smart_trash_can_volume
represents the current volume of trash inside the can.
(Do not forget to update the view, so the attribute is visible on HA)
Generating Synthetic Volume Data
You don’t have a real volume sensor, so simulate volume values using a Python script. This will publish random volume values periodically to the MQTT broker.
Example Python Script (volume_publisher.py
)
import paho.mqtt.client as mqtt
import time
import random
broker_address = "192.168.1.100" # Replace with your broker’s address
broker_port = 1883
username = "mqttuser"
password = "your_password"
volume_topic = "smart_trash_can/volume"
client = mqtt.Client("volume_publisher")
client.username_pw_set(username, password)
client.connect(broker_address, broker_port, 60)
sleep_period = 5 # Publish volume every 5 seconds
while True:
nb_trash = random.randint(1, 150)
client.publish(volume_topic, nb_trash)
time.sleep(sleep_period)
What This Does:
- Every 5 seconds, it publishes a random volume of trash to
smart_trash_can/volume
. - Home Assistant receives this value and updates
sensor.smart_trash_can_volume
.
Virtual Sensor Python Script (virtual_sensor.py
)
import paho.mqtt.client as mqtt
broker_address = "192.168.1.100"
broker_port = 1883
username = "mqttuser"
password = "your_password"
motion_topic = "smart_trash_can/motion"
volume_topic = "smart_trash_can/volume"
fill_topic = "smart_trash_can/fill_percentage"
# Assume the total capacity of the trash can is known
total_capacity = 1500 # cm³
current_motion = None
current_volume = None
def process_data():
# Compute fill percentage based on the current_volume and total_capacity
if current_volume is not None and total_capacity > 0:
fill_percentage = (current_volume / total_capacity) * 100.0
client.publish(fill_topic, fill_percentage)
def on_connect(client, userdata, flags, rc):
print("Connected with result code", rc)
client.subscribe(motion_topic)
client.subscribe(volume_topic)
def on_message(client, userdata, msg):
global current_motion, current_volume
if msg.topic == motion_topic:
current_motion = msg.payload.decode("utf-8")
elif msg.topic == volume_topic:
try:
volume_value = float(msg.payload.decode("utf-8"))
current_volume = volume_value
except ValueError:
print("Received invalid volume value")
# After updating data, process to compute fill percentage
process_data()
client = mqtt.Client("virtual_sensor")
client.username_pw_set(username, password)
client.on_connect = on_connect
client.on_message = on_message
client.connect(broker_address, broker_port, 60)
client.loop_forever()
What This Script Does:
- Subscribes to both motion and volume topics.
- On each message, it updates
current_motion
orcurrent_volume
. - Calls
process_data()
to compute fill percentage. - Publishes the fill percentage to
smart_trash_can/fill_percentage
.
Home Assistant, subscribed to smart_trash_can/fill_percentage
, updates sensor.smart_trash_can_fill_percentage
accordingly.
Viewing the Results in Home Assistant
Once the virtual sensor script runs, you should already have a Gauge card to visualize the fill percentage:
Summary
- Raw Data Sources: Motion events and simulated volume measurements.
- Virtual Sensor’s Role: A Python script acting as a subscriber to these data, computing the fill percentage.
- Publishing Results: The virtual sensor publishes the computed fill percentage to Home Assistant via MQTT.
- Dashboard Visualization: Home Assistant displays the fill percentage using a Gauge card, making the data meaningful and actionable.
By following these steps, you have learned how to create a volume-based virtual sensor in Home Assistant, transforming raw motion and volume data into a valuable metric: the fill percentage of the trash can.