Automation Layer
Smart Home Control
Home Assistant orchestrates 22+ IoT devices across 8 zones using MQTT and Zigbee protocols. All automation rules execute locally — zero cloud dependency for critical operations including emergency shutdowns.
Core Components
Home Assistant Core
WebSocket API · WAMP Protocol
Central automation controller running locally on the MiniPC. Manages device registry, automation rules, state machine, and exposes WebSocket API to RAFIQ Core for bidirectional control.
MQTT Broker (Mosquitto)
Message Bus · QoS 0-2
Central message broker handling all device telemetry and command traffic. Runs on localhost:1883 with persistent sessions, retained messages, and LWT (Last Will & Testament) for device health.
Automation Engine
Local-First · Rule Executor
HA automation engine evaluates 14 active rules against real-time state. All emergency automations run synchronously with no external dependency — gas detection to valve close in under 200ms.
Zigbee2MQTT Bridge
Zigbee Coordinator · USB Dongle
Translates Zigbee protocol (IEEE 802.15.4) to MQTT. Manages pairing, device discovery, and OTA updates for 18 Zigbee devices including all lights, sensors, and curtain motors.
Device Catalog · 22 Total
- Home Assistant runs on MiniPC (Ubuntu)
- Automation rules stored in local YAML
- MQTT broker on localhost — zero latency
- Emergency gas/fire: <200ms response
- Works without internet connection
- Valve control direct via GPIO relay
- State machine persisted to SQLite
- Remote access via Supabase Realtime
- Sync automation history to cloud
- SMS alerts via cloud gateway (fallback)
- Mobile app pushes via FCM
- AI recommendations from health data
- Dashboard accessible from phone remotely
- Cloud status never blocks local actions
Active Automations
14 automations · local-first execution
| Name | Trigger | Actions | Type |
|---|---|---|---|
| Sleep Mode | Time 23:00 / Voice | Lights off → AC 22°C → Curtains close → TV off | Routine |
| Morning Routine | Time 07:00 | Lights 30% → Curtains open → AC off → Alarm off | Routine |
| Gas Emergency | MQ-2/4 > threshold | Valves off → Windows open → Alarm → SMS | Emergency |
| Fire Evacuation | Smoke + Temp spike | All lights 100% → Doors unlock → Alarm → Call | Emergency |
| Away Mode | All phones off LAN | Lights off → AC off → Alarm arm → Locks secure | Security |
| Welcome Home | Phone joins LAN | Entry light on → AC on → Disarm alarm | Routine |
| Leak Detection | Water sensor wet | Main valve off → Alert → Log event | Emergency |
| Night Security | Motion after 00:00 | Lights flash → Camera record → Alert | Security |
Configuration Examples
# Home Assistant Automation — Emergency Gas Protocol
alias: "RAQEEB Gas Emergency Response"
description: "Triggered when MQ-2 or MQ-4 exceeds safety threshold"
trigger:
- platform: mqtt
topic: "rafiq/raqeeb/alert"
payload: "GAS_DETECTED"
- platform: state
entity_id: sensor.mq2_lpg_ppm
above: 1000
- platform: state
entity_id: sensor.mq4_methane_ppm
above: 800
condition:
- condition: not
conditions:
- condition: state
entity_id: input_boolean.emergency_acknowledged
state: "on"
action:
- service: switch.turn_off
target:
entity_id:
- switch.gas_valve_kitchen
- switch.gas_valve_bathroom
- service: cover.open_cover
target:
entity_id: cover.living_room_curtains
- service: siren.turn_on
target:
entity_id: siren.hallway_alarm
data:
tone: emergency
volume_level: 1.0
- service: notify.rafiq_sms_gateway
data:
message: "⚠️ GAS ALERT: Valves closed, evacuation initiated at {{ now() }}"
- service: mqtt.publish
data:
topic: "rafiq/core/emergency"
payload: '{"type":"GAS","level":"CRITICAL","ts":"{{ now().isoformat() }}"}'
qos: 2
retain: true
mode: single# MQTT Device Control — Direct Command Protocol
# Topic structure: rafiq/home/{zone}/{device}/{command}
# Turn off all lights in bedroom zone
mosquitto_pub -h localhost -p 1883 \
-t "rafiq/home/bedroom/lights/set" \
-m '{"state":"OFF","transition":2}' \
-q 1
# Set AC to sleep mode (22°C, low fan)
mosquitto_pub -h localhost -p 1883 \
-t "rafiq/home/bedroom/ac/set" \
-m '{"mode":"cool","temp":22,"fan":"low","swing":"auto"}' \
-q 1
# Emergency valve shutdown
mosquitto_pub -h localhost -p 1883 \
-t "rafiq/home/all/valves/emergency_close" \
-m '{"action":"CLOSE_ALL","reason":"GAS_DETECTED","ts":1716322231}' \
-q 2 --retain
# Subscribe to all device state changes
mosquitto_sub -h localhost -p 1883 \
-t "rafiq/home/#" \
-v
# Example state update received:
# rafiq/home/kitchen/valve → {"state":"CLOSED","psi":0,"ts":1716322231}
# rafiq/home/bedroom/lights → {"state":"OFF","brightness":0,"ts":1716322231}RAFIQ Core subscribes to state_changed events via WebSocket. All device state updates stream in real-time to the event bus.
RAFIQ triggers HA services (light.turn_on, climate.set_temperature) via JSON-RPC over WebSocket, authenticated by long-lived token.
RAQEEB ESP32 bypasses WebSocket via direct MQTT. HA listens to emergency topics and triggers automations even if RAFIQ Core is down.