====== MOSH ↔ MQTT ↔ Home Assistant Integration ======
===== Overview =====
MOSH integrates with {{:software:home_assistant}} using MQTT as a bridge layer.
The integration works by:
* Publishing MOSH element state to MQTT topics
* Using Home Assistant MQTT Discovery to auto-create entities
* Receiving commands from Home Assistant via MQTT and forwarding them into MOSH
This allows Home Assistant to control MOSH nodes without any custom integration code.
-----
===== Architecture =====
MOSH Core
↓
MOSH MQTT Module
↓
MQTT Broker (Mosquitto, etc.)
↓
Home Assistant (MQTT Integration)
-----
===== Requirements =====
* Running MOSH bridge with MQTT module enabled
* MQTT broker (e.g., Mosquitto)
* {{:software:home_assistant}} with MQTT integration configured
-----
===== MQTT Topic Structure =====
MOSH uses a structured topic hierarchy:
mosh///state
mosh///set
mosh///availability
Example:
mosh/testnode1/relay1/state
mosh/testnode1/relay1/set
mosh/testnode1/relay1/availability
-----
===== MQTT Discovery =====
MOSH publishes discovery messages to:
homeassistant///config
Example (switch):
homeassistant/switch/mosh_testnode1_relay1/config
-----
===== Example Discovery Payload (Switch) =====
{
"name": "Relay 1",
"unique_id": "mosh_testnode1_relay1",
"state_topic": "mosh/testnode1/relay1/state",
"command_topic": "mosh/testnode1/relay1/set",
"availability_topic": "mosh/testnode1/relay1/availability",
"payload_on": "ON",
"payload_off": "OFF",
"state_on": "ON",
"state_off": "OFF",
"device": {
"identifiers": ["mosh_testnode1"],
"name": "Test Node 1",
"manufacturer": "MOSH",
"model": "Generic Node"
}
}
-----
===== State Publishing =====
MOSH publishes element state changes to:
mosh///state
Example:
mosh/testnode1/relay1/state → ON
mosh/testnode1/relay1/state → OFF
State values should match Home Assistant expectations:
* Switch: `ON` / `OFF`
* Sensor: numeric or string
* Binary sensor: `ON` / `OFF`
-----
===== Command Handling =====
Home Assistant sends commands to:
mosh///set
Example:
mosh/testnode1/relay1/set → ON
MOSH MQTT module must:
* Subscribe to `*/set` topics
* Translate payload into MOSH command
* Call `core.EnqueueCommand(...)`
-----
===== Availability =====
MOSH should publish node availability:
mosh//availability → online/offline
Or per element:
mosh///availability
Home Assistant expects:
* `online`
* `offline`
-----
===== Supported Entity Types =====
MOSH elements should map to Home Assistant components:
^ MOSH Element Type ^ Home Assistant Component ^
| switch | switch |
| sensor | sensor |
| binary_sensor | binary_sensor |
| dimmer | light |
| temperature | sensor (device_class=temperature) |
-----
===== Example: Relay Integration =====
==== Step 1: MOSH publishes discovery ====
homeassistant/switch/mosh_testnode1_relay1/config
==== Step 2: MOSH publishes state ====
mosh/testnode1/relay1/state → OFF
==== Step 3: Home Assistant shows entity ====
``switch.relay_1`` appears automatically.
==== Step 4: User toggles switch ====
Home Assistant publishes:
mosh/testnode1/relay1/set → ON
==== Step 5: MOSH executes command ====
* MQTT module receives message
* Calls core:
EnqueueCommand(node=testnode1, element=relay1, payload={"state":"ON"})
-----
===== MQTT Module Responsibilities =====
The MOSH MQTT module must:
* Connect to broker
* Publish discovery configs
* Publish state updates
* Subscribe to command topics
* Translate MQTT → MOSH commands
* Track node availability
-----
===== Configuration =====
Example MOSH config:
{
"modules": {
"mqtt": {
"enabled": true,
"broker": "tcp://127.0.0.1:1883",
"client_id": "moshbridge",
"base_topic": "mosh"
}
}
}
-----
===== Naming Conventions =====
* Topic prefix: `mosh/`
* Unique ID: `mosh__`
* Device ID: `mosh_`
-----
===== Debugging =====
==== Check MQTT traffic ====
mosquitto_sub -t "mosh/#" -v
==== Check discovery messages ====
mosquitto_sub -t "homeassistant/#" -v
==== Restart Home Assistant MQTT integration ====
Sometimes required after discovery changes.
-----
===== Common Issues =====
^ Issue ^ Cause ^ Fix ^
| Entity not appearing | Discovery not published | Check `homeassistant/.../config` |
| State not updating | Wrong topic | Verify `state_topic` |
| Commands not working | Not subscribed to `/set` | Fix MQTT module |
| Device unavailable | Missing availability topic | Publish `online/offline` |
-----
===== Future Improvements =====
* Support for retained discovery messages
* QoS configuration
* Device class mapping
* Light/dimmer brightness support
* Auto-discovery refresh on reconnect
-----