I have tasmota smart plug that I want to turn on as soon as I turn on my laptop. For this I got a script from ChatGPT (I am not good with Mikrotik scripting).
# Script to check ARP table for the laptop's MAC address and send an HTTP request to the Tasmota switch
:local laptopMAC "FC:34:97:02:75:77"
:local tasmotaIP "192.168.88.223"
:local found false
/ip arp
:log info "Checking ARP table for laptop..."
:foreach arpEntry in=[find] do={
:local macAddress [get $arpEntry mac-address]
:if ($macAddress = $laptopMAC) do={
:set found true
}
}
:if ($found = true) do={
:local url ("http://" . $tasmotaIP . "/cm?cmnd=Power%20On")
/tool fetch url=$url keep-result=no
}
After this I added a scheduler to run this every 1 minute. But this script turn on my switch even though my laptop is OFF. Is there a robust way to detect when my laptop is turned on and then send http request?
Thank you very much for your response. I think this will work perfectly fine.
Is there a way to detect if a request has been sent the first time ping was successful? I just thinking out loud if I can track the staus of request; something like this:
requesttobesent = true
do ping laptop:
if ping successful and requesttobesent = true:
send http request
set flag requesttobesentc=c false
if ping failed:
requesttobesent = true
I just don’t want to keep sending the http request to tasmota device (I am not sure if there is a downside to this). Do you think this should work and is a good approach?
Reason why you failed previously is due to that both ARP and MAC entries are cached by switches and routers.
The MAC entries for a particular interface is normally deleted when the interface goes down and same with ARP.
But in your case if the interface doesnt go down (on the router) because you for example have router ↔ switch ↔ laptop then the ARP entry will remain in the ARP cache for up to several hours (depending on what is the ARP timeout configured, I prefer to let MAC timeout be default 5min and then change the ARP timeout so its less than MAC timeout for example set it to 4min).
But when using netwatch thats is a more active poll through (for example) ping (icmp echo-request) to see if the host (laptop in your case) replies and if it does its likely that its only (note that in some network there are proxy-arp being runned so for those cases you could get a reply even if the host is down because then its the router who is doing the proxy-arp who replies instead).