AnswersFile

Question

How can I send my MikroTik router stats to Home Assistant via MQTT?

Answer

You can send MikroTik router statistics to Home Assistant using MQTT autodiscovery. This requires two RouterOS scripts and the IoT MQTT package on your MikroTik router.

Prerequisites

  • MikroTik router with RouterOS that supports IoT MQTT package
  • MQTT broker (like Mosquitto) accessible from your router
  • Home Assistant with MQTT integration configured
  • MQTT broker connection configured on your MikroTik router
  • Queue Tree entries named "download" and "upload" for bandwidth monitoring (see MikroTik bufferbloat setup)

Step 1: Configure MQTT Broker on MikroTik

First, set up the MQTT broker connection on your router:

/iot mqtt brokers
add name="my mqtt" address=YOUR_BROKER_IP port=1883 \
    username=YOUR_USERNAME password=YOUR_PASSWORD

Step 2: Create the Setup Script

Create a script named mqttsetup that configures Home Assistant MQTT autodiscovery for all sensors. You can create this in WebFig (System → Scripts → Add New) or via command line using /system script add name=mqttsetup source= followed by the script content.

:local broker "my mqtt"
:local base "homeassistant/sensor/router"

# CPU
/iot mqtt publish broker=$broker topic="$base/cpu/config" message="{\"device\":{\"identifiers\":\"router\",\"name\":\"Router\",\"manufacturer\":\"MikroTik\"},\"name\":\"CPU\",\"state_topic\":\"homeassistant/sensor/router/cpu\",\"unit_of_measurement\":\"%\",\"unique_id\":\"router-cpu\"}" retain="yes"

# Memory
/iot mqtt publish broker=$broker topic="$base/memory/config" message="{\"device\":{\"identifiers\":\"router\",\"name\":\"Router\",\"manufacturer\":\"MikroTik\"},\"name\":\"Memory\",\"state_topic\":\"homeassistant/sensor/router/memory\",\"unit_of_measurement\":\"%\",\"unique_id\":\"router-memory\"}" retain="yes"

# Disk
/iot mqtt publish broker=$broker topic="$base/disk/config" message="{\"device\":{\"identifiers\":\"router\",\"name\":\"Router\",\"manufacturer\":\"MikroTik\"},\"name\":\"Disk\",\"state_topic\":\"homeassistant/sensor/router/disk\",\"unit_of_measurement\":\"%\",\"unique_id\":\"router-disk\"}" retain="yes"

# Download
/iot mqtt publish broker=$broker topic="$base/download/config" message="{\"device\":{\"identifiers\":\"router\",\"name\":\"Router\",\"manufacturer\":\"MikroTik\"},\"name\":\"Download\",\"state_topic\":\"homeassistant/sensor/router/download\",\"unit_of_measurement\":\"Kbps\",\"unique_id\":\"router-download\"}" retain="yes"

# Upload
/iot mqtt publish broker=$broker topic="$base/upload/config" message="{\"device\":{\"identifiers\":\"router\",\"name\":\"Router\",\"manufacturer\":\"MikroTik\"},\"name\":\"Upload\",\"state_topic\":\"homeassistant/sensor/router/upload\",\"unit_of_measurement\":\"Kbps\",\"unique_id\":\"router-upload\"}" retain="yes"

# Ping
/iot mqtt publish broker=$broker topic="$base/ping/config" message="{\"device\":{\"identifiers\":\"router\",\"name\":\"Router\",\"manufacturer\":\"MikroTik\"},\"name\":\"Ping\",\"state_topic\":\"homeassistant/sensor/router/ping\",\"unit_of_measurement\":\"ms\",\"unique_id\":\"router-ping\"}" retain="yes"

/log info "Router MQTT autodiscovery published"

Step 3: Create the Publish Script

Create a script named mqttpublish that collects and sends the actual sensor data. Use the same method as Step 2 (WebFig or command line).

:local broker "my mqtt"
:local base "homeassistant/sensor/router"

# CPU
:local cpu [/system resource get cpu-load]
/iot mqtt publish broker=$broker topic="$base/cpu" message=$cpu

# Memory
:local memTotal [/system resource get total-memory]
:local memFree [/system resource get free-memory]
:local mem (($memTotal - $memFree) * 100 / $memTotal)
/iot mqtt publish broker=$broker topic="$base/memory" message=$mem

# Disk
:local diskTotal [/system resource get total-hdd-space]
:local diskFree [/system resource get free-hdd-space]
:local disk (($diskTotal - $diskFree) * 100 / $diskTotal)
/iot mqtt publish broker=$broker topic="$base/disk" message=$disk

# Download/Upload
:local dl ([/queue tree get [find name="download"] rate] / 1000)
:local ul ([/queue tree get [find name="upload"] rate] / 1000)
/iot mqtt publish broker=$broker topic="$base/download" message=$dl
/iot mqtt publish broker=$broker topic="$base/upload" message=$ul

# Ping
:local pingResult [/ping 8.8.8.8 count=1 as-value]
:local pingTime [:pick ($pingResult->0->"time") 0 [:find ($pingResult->0->"time") "ms"]]
/iot mqtt publish broker=$broker topic="$base/ping" message=$pingTime

Step 4: Schedule the Scripts

Create schedulers to run both scripts automatically:

# Run the publish script every 30 seconds
/system scheduler add \
    name=mqtt-publish \
    interval=30s \
    on-event="/system script run mqttpublish"

# Run the setup script once per day at 3 AM
/system scheduler add \
    name=mqtt-setup-daily \
    interval=1d \
    start-time=03:00:00 \
    on-event="/system script run mqttsetup"

Step 5: Run the Setup Script

Execute the setup script once to configure MQTT autodiscovery for the first time:

/system script run mqttsetup

Monitored Statistics

This setup monitors the following router statistics:

  • CPU Load: Current processor usage percentage
  • Memory Usage: RAM usage percentage
  • Disk Usage: Storage usage percentage
  • Download Speed: Current download bandwidth in Kbps
  • Upload Speed: Current upload bandwidth in Kbps
  • Ping Latency: Network latency to 8.8.8.8 in milliseconds

Important Notes

  • The download/upload monitoring requires Queue Tree entries named "download" and "upload" to exist on your router
  • Verify your queue tree names with /queue tree print
  • The setup script runs once per day to ensure autodiscovery configs remain fresh in case Home Assistant restarts
  • Home Assistant will automatically discover the router device and all sensors
  • Adjust the publish scheduler interval (30s) based on your preference
  • Make sure the broker name in the scripts matches the name you configured in Step 1

Why This Works

Home Assistant's MQTT integration supports autodiscovery using a specific topic structure. The setup script publishes configuration messages to homeassistant/sensor/router/[sensor]/config with JSON payloads that define each sensor's properties, grouping them under a single "Router" device. The publish script then sends actual values to the state topics defined in the configuration. This approach eliminates manual sensor configuration in Home Assistant.