AnswersFile

Question

How can I log my MikroTik router current ping to Home Assistant?

Answer

You can log MikroTik router ping latency to Home Assistant using a RouterOS script that performs flood-ping tests, calculates the average round-trip time, and publishes the result via MQTT.

Prerequisites

  • MikroTik router with RouterOS that supports IoT MQTT package
  • MQTT broker (like Mosquitto) configured and accessible from your router
  • Home Assistant with MQTT integration configured
  • MQTT broker connection configured on your MikroTik router (see MikroTik MQTT setup)

Step 1: Configure MQTT Broker on MikroTik

If you haven't already, 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 Ping Script

Create a script named pinglog that performs multiple ping tests and calculates the average latency. You can create this in WebFig (System → Scripts → Add New) or via command line.

:local avgRtt 0
:local numPing 2
:local toPingIP 1.1.1.1
:local broker "my mqtt"

:for tmpA from=1 to=$numPing step=1 do={
    /tool flood-ping count=1 size=38 address=$toPingIP do={
        :set avgRtt ($"avg-rtt" + $avgRtt)
    }
    /delay 1s
}

:local result ($avgRtt / $numPing)
:log info "Ping Average for $toPingIP: $result ms"
/iot mqtt publish broker=$broker topic="homeassistant/sensor/router/ping" message=$result

Step 3: Configure Home Assistant MQTT Sensor

Add the following sensor configuration to your Home Assistant configuration.yaml:

mqtt:
  sensor:
    - name: "Router Ping"
      state_topic: "homeassistant/sensor/router/ping"
      unit_of_measurement: "ms"
      icon: mdi:wan
      device_class: duration

Step 4: Schedule the Script

Create a scheduler to run the ping script at your desired interval (e.g., every 30 seconds):

/system scheduler add \
    name=ping-logger \
    interval=30s \
    on-event="/system script run pinglog"

Step 5: Test the Script

Run the script manually to verify it works:

/system script run pinglog

Check your router logs to see the ping average, and verify the sensor appears in Home Assistant.

How It Works

The script uses MikroTik's flood-ping tool to send multiple ping packets and collect latency data. Here's the breakdown:

  • numPing: Number of ping tests to perform (default: 2)
  • toPingIP: Target IP address to ping (1.1.1.1 is Cloudflare DNS)
  • flood-ping: Sends ping with 38-byte packet size and retrieves average RTT
  • Calculation: Averages the RTT from all ping tests
  • Publishing: Sends the result to MQTT topic that Home Assistant monitors

Customization Options

  • Change target IP: Modify toPingIP to ping a different host (e.g., 8.8.8.8 for Google DNS)
  • Adjust sample count: Increase numPing for more accurate averages (but slower execution)
  • Change interval: Modify the scheduler interval to test more or less frequently
  • Packet size: Adjust size=38 to test with different packet sizes

Important Notes

  • The script uses a 1-second delay between pings to avoid overwhelming the network
  • Flood-ping provides more accurate latency measurements than standard ping
  • Running ping tests too frequently can consume router CPU resources
  • Make sure the broker name matches your configured MQTT broker
  • The sensor will appear in Home Assistant automatically after the first data is published

Why Use Flood-Ping?

MikroTik's flood-ping tool is designed for latency testing and provides more accurate RTT measurements than the standard ping tool. It sends packets rapidly and calculates statistics in real-time, making it ideal for network monitoring applications.