Table of Contents

MOSH ↔ MQTT ↔ Home Assistant Integration

Overview

MOSH integrates with home_assistant using MQTT as a bridge layer.

The integration works by:

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


MQTT Topic Structure

MOSH uses a structured topic hierarchy:

mosh/<node_id>/<element_id>/state
mosh/<node_id>/<element_id>/set
mosh/<node_id>/<element_id>/availability

Example:

mosh/testnode1/relay1/state
mosh/testnode1/relay1/set
mosh/testnode1/relay1/availability

MQTT Discovery

MOSH publishes discovery messages to:

homeassistant/<component>/<unique_id>/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/<node>/<element>/state

Example:

mosh/testnode1/relay1/state → ON
mosh/testnode1/relay1/state → OFF

State values should match Home Assistant expectations:


Command Handling

Home Assistant sends commands to:

mosh/<node>/<element>/set

Example:

mosh/testnode1/relay1/set → ON

MOSH MQTT module must:


Availability

MOSH should publish node availability:

mosh/<node>/availability → online/offline

Or per element:

mosh/<node>/<element>/availability

Home Assistant expects:


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

    EnqueueCommand(node=testnode1, element=relay1, payload={"state":"ON"})
    

MQTT Module Responsibilities

The MOSH MQTT module must:


Configuration

Example MOSH config:

{
  "modules": {
    "mqtt": {
      "enabled": true,
      "broker": "tcp://127.0.0.1:1883",
      "client_id": "moshbridge",
      "base_topic": "mosh"
    }
  }
}

Naming Conventions


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