22 Devices Active
Automation LayerHA 2024.4
Devices22
Automations14
ExecutionLocal-First

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.

API
WebSocket
Protocol
WAMP
Port
8123
LocalWebSocketPython

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.

Port
1883
QoS
0–2
TLS
Local
MQTTMosquittoPub/Sub

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.

Rules
14 active
Response
<200ms
Cloud
Not required
Local-FirstRule EngineState Machine

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.

Devices
18 Zigbee
Channel
15
Dongle
CC2652P
ZigbeeIEEE 802.15.4Z2M

Device Catalog · 22 Total

Smart TV
Living Room
MQTT×1
Air Conditioner
All Zones
MQTT/IR×4
Smart Lights
8 Zones
Zigbee×8
Curtains
Bedroom/Living
Zigbee×3
Alarm Siren
Hallway
MQTT×1
Water Valves
Kitchen/Bath
MQTT/GPIO×2
Env Sensors
All Rooms
Zigbee×6
Motion Sensors
Entry/Hall
Zigbee×4
Local vs Cloud Automation Executionexecution topology
Local Execution
  • 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
Cloud-Enhanced (Optional)
  • 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

NameTriggerActionsType
Sleep ModeTime 23:00 / VoiceLights off → AC 22°C → Curtains close → TV offRoutine
Morning RoutineTime 07:00Lights 30% → Curtains open → AC off → Alarm offRoutine
Gas EmergencyMQ-2/4 > thresholdValves off → Windows open → Alarm → SMSEmergency
Fire EvacuationSmoke + Temp spikeAll lights 100% → Doors unlock → Alarm → CallEmergency
Away ModeAll phones off LANLights off → AC off → Alarm arm → Locks secureSecurity
Welcome HomePhone joins LANEntry light on → AC on → Disarm alarmRoutine
Leak DetectionWater sensor wetMain valve off → Alert → Log eventEmergency
Night SecurityMotion after 00:00Lights flash → Camera record → AlertSecurity
Emergency Evacuation Sequencegas detected → full response
01
Gas Sensor
MQ-2 / MQ-4 threshold exceeded
02
ESP32 MCU
Independent threshold check
03
Valve Close
GPIO relay off in <50ms
04
MQTT Alert
Publish to rafiq/raqeeb/alert
05
HA Trigger
Automation fires instantly
06
Open Windows
Curtains → open position
07
Alarm Siren
100% volume emergency tone
08
Notify
SMS + App + RAFIQ Core log

Configuration Examples

automations/gas_emergency.yaml
yaml
# 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.sh
bash
# 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}
HA WebSocket API IntegrationRAFIQ Core ↔ Home Assistant
Subscribe Events

RAFIQ Core subscribes to state_changed events via WebSocket. All device state updates stream in real-time to the event bus.

Call Services

RAFIQ triggers HA services (light.turn_on, climate.set_temperature) via JSON-RPC over WebSocket, authenticated by long-lived token.

Emergency Override

RAQEEB ESP32 bypasses WebSocket via direct MQTT. HA listens to emergency topics and triggers automations even if RAFIQ Core is down.