Wake on lan when device (with MAC) connected to WiFi [Solved]

The idea is this: I want my PC to turn on when I get home in the evening. Usually my phone always has WiFi on, it would be cool when I go to the house to have my PC turn on.
I understand the algorithm:

  1. The script checks the connected wireless devices every N seconds (Mikrotik scheduler can be used)
  2. If the desired MAC address appears among the connected devices →
    2.1.Check last connected time (need to ensure that after turning off the computer at a given M time, it does not turn on immediately back)
    2.2 send a WOL package → update last connected time

It can also be optimized, for example, after turning on the PC to increase the time to check the connection or even disable the script.
With the microtic scheduler, you can also limit the running time of the script (for example, run it every 10 seconds from 6 PM to 2 AM).

I understand how this should work, but I can’t write code properly. I will try tomorrow, but I call you for help, I think it will be useful not only to me. And maybe someone’s done it before.

The first working version. Sends a packet to a specific device (deviceMACToEnable) when another device is connected (triggerDeviceMAC). You can limit time when it will be work (fromTime, toTime).
Set it as a lease script of your DHCP server.

:local triggerDeviceMAC "00:00:00:00:00:00";
:local deviceMACToEnable "11:11:11:11:11:11";
:local deviceInterfaceToEnable "eth2-lan"
:local fromTime "18:00:00";
:local toTime "03:00:00";

:local currentTime [/system clock get time];

:if ($leaseActMAC = $triggerDeviceMAC && $leaseBound = 1 && ($currentTime >= $fromTime || $currentTime <= $toTime)) do={ 
:log info "WOL: MAC connected";
/tool wol mac=$deviceMACToEnable interface=$deviceInterfaceToEnable;
}

Thank you for sharing.