A simple WiFi device monitor for MikroTik RouterOS.
The script watches selected WiFi devices by MAC address and sends Telegram notifications when they connect or disconnect.
Supports both:
-
/interface wifi -
/interface wireless
Tested on RouterOS 7.18.2 stable.
-
Monitor WiFi devices by MAC address
-
Telegram notifications on connect/disconnect
-
Protection against false disconnect notifications
-
Boot protection after router restart
-
Automatic WiFi driver detection
-
Supports both
wifiand legacywireless -
Protection against multiple script instances
-
Persistent device state
-
Automatic cleanup of old device states
-
Error handling with
:onerror -
Telegram errors do not stop the monitor
-
MAC address cache for faster checks
-
Safe initialization on the first run
Devices are added to the WiFi Access List.
The comment must start with:
MONITOR:
For example:
MONITOR:Kitchen TV
MONITOR:Phone
MONITOR:Laptop
The text after MONITOR: is used as the device name in Telegram notifications.
The script:
-
Reads monitored devices from the Access List.
-
Detects the available WiFi driver.
-
Reads the registration table.
-
Builds a list of currently connected MAC addresses.
-
Checks monitored devices against this list.
-
Counts consecutive failed checks for missing devices.
-
Marks the device as offline after the configured threshold.
-
Marks it online again when it appears in the registration table.
-
Sends a Telegram notification when the state changes.
-
Saves the current state and failure counter.
-
MikroTik RouterOS 7.x
-
Tested on RouterOS 7.18.2 stable
-
WiFi interface using either
wifiorwireless -
Telegram bot
-
Telegram sender script
The WiFi driver is detected automatically.
There are three Telegram-related scripts in the project.
The standard Telegram sender.
It uses RouterOS native URL conversion:
:convert $strMessageText to=url
This works well for ASCII/Latin text.
For example:
WiFi device connected
The script expects the message in:
strMessageText
A small helper script for Cyrillic text.
Cyrillic text read from RouterOS configuration fields behaves as CP1251-compatible bytes in the tested environment.
For example:
Кухня
is read as:
CA F3 E5 ED FF
The helper converts it to UTF-8 URL encoding:
%D0%9A%D1%83%D1%85%D0%BD%D1%8F
Input:
strInputText
Output:
strEncodedText
The script also handles normal ASCII characters, spaces and special characters.
There is also an alternative version of send_to_telegram_rus.
The difference is that it runs:
cp1251_to_utf8_url
before sending the message.
For example:
MONITOR:Кухня & TV
becomes:
MONITOR%3a%D0%9A%D1%83%D1%85%D0%BD%D1%8F%20%26%20TV
This version should be used if device names or notification text can contain Cyrillic characters.
| Script | Cyrillic | ASCII | Extra script |
|---|---|---|---|
send_to_telegram |
Limited | Yes | No |
Alternative send_to_telegram_rus |
Yes | Yes | cp1251_to_utf8_url |
If you only use English/Latin text, the standard sender is enough.
If you use Russian or other Cyrillic text, use the alternative sender together with cp1251_to_utf8_url.
Edit the Telegram sender and set:
:local tgBotToken "YOUR_BOT_TOKEN"
:local tgChatID "YOUR_CHAT_ID"
The sender uses the Telegram Bot API:
https://api.telegram.org
The router must be able to connect to Telegram over HTTPS.
The actual Telegram request is used as the delivery attempt. There is no separate HTTPS check before every message.
Create a new System Script in RouterOS and add the main monitoring script.
For example:
monitor
Create:
send_to_telegram
If you need Cyrillic support, also create:
cp1251_to_utf8_url
Then use the alternative Telegram sender.
Add the devices you want to monitor to the WiFi Access List.
Use the following format in the Comment field:
MONITOR:Device Name
Examples:
MONITOR:Kitchen TV
MONITOR:Phone
MONITOR:Laptop
The MAC address is used to identify the device.
The default interval is 30 seconds.
Example:
/system scheduler
add interval=30s on-event="/system script run monitor" name=monitor
The scheduler interval should match the SchedulerInterval value in the monitor script.
The monitor has a few main parameters.
Default:
4
Number of consecutive checks where the device is missing before it is marked as offline.
With a 30 second scheduler:
4 x 30 seconds = about 2 minutes
This helps to avoid false disconnect notifications.
Default:
120
Time in seconds after router startup during which the monitor initializes its state.
This prevents false offline notifications after a reboot.
Default:
30
Expected scheduler interval in seconds.
This value is also used by the execution lock.
The monitor uses an execution lock to prevent multiple instances from running at the same time.
This can happen if:
-
the previous run takes longer than expected
-
the script is started manually
-
the scheduler starts while another instance is still running
The lock uses:
:timestamp
and:
:tonsec
The lock has an expiration time, so a broken or interrupted script run does not leave the monitor permanently locked.
The main monitoring process is protected with RouterOS :onerror.
Telegram sending is also isolated from the main monitoring code.
If Telegram is unavailable, the monitor itself continues working.
RouterOS can show the actual /tool fetch error in the log, for example:
Download from api.telegram.org FAILED: Idle timeout - connecting
The Telegram script also logs:
SendTelegram failed: Telegram API unavailable or request failed
The monitor stores state information globally.
For each device it keeps:
<MAC>-state
and:
<MAC>-fail
MAC addresses are converted to safe storage keys.
For example:
AA:BB:CC:DD:EE:FF
becomes:
AA-BB-CC-DD-EE-FF
This allows the monitor to keep the device state between script runs.
On the first run the monitor initializes the state of all configured devices.
It does not immediately send offline notifications for devices that are currently not connected.
This prevents a lot of false notifications after:
-
installation
-
router reboot
-
script restart
-
configuration changes
When a device is removed from the Access List, its old state is also removed.
This keeps the global state storage clean and prevents old device entries from accumulating.
English:
🟢 Device connected: Kitchen TV
🔴 Device disconnected: Kitchen TV
With Cyrillic:
🟢 Устройство подключено: Кухня ТВ
🔴 Устройство отключено: Кухня ТВ
For Cyrillic messages use:
cp1251_to_utf8_url
together with the alternative Telegram sender.
The scripts were tested on RouterOS 7.18.2 stable.
The following RouterOS features were checked during development:
-
Dynamic array keys
-
Array key removal
-
foreach key,value -
registration-table as-value -
access-list as-value -
:totime -
Global array replacement
-
:timestamp -
:tonsec -
Timestamp arithmetic
-
:onerror -
:convert ... to=url -
:convert ... to=hex -
Cyrillic text from RouterOS configuration fields
-
CP1251-compatible byte conversion
-
UTF-8 URL encoding
-
Telegram HTTP POST requests
-
Telegram error handling
Some RouterOS scripting constructs caused compatibility problems during testing, so the final version avoids them.