Mikrotik - Wireless off at night

Posted in How Did I Do That?

Studies have shown that even low level RF is not good for your health while you rest. I created this to reduce power initially. Then, I modified it to shut down RF completely. The code assumes that there was a power outage and so it constantly sets the power level and wireless status (in case it was missed last time). There is some obsolete code as a result. But, it is helpful.

# Script to turn down radio power based on a schedule
# Schedule this script to run every 10-15 minutes to make
# certain the changeover has occurred.
# User variables are listed below

#####################################
## Set the Radio HighPower and LowPower times here ##
:local RadioHighPowerTime "06:20";
:local RadioLowPowerTime "01:15";
:local LowPower 1;
:local HighPower 22;
:local RadioName "wlan";
:local RadioPowerCut;
:local RadioPower;
:local UseNTPClientStatus "yes";
# set to "no" if clock is being set manually after each reboot #
# set to "yes" if clock is being set using NTP client #

#####################################

#:log info "RadioHighLow Script Starting";
#:log info "Radio Name = $RadioName";

# First check if system clock has been syncronized with local time #
:local NTPSyncState [/system ntp client get status];
#:log info "NTP Client Status = $NTPSyncState";

# Don't perform radio HighPower or LowPower operation, if current real time is unknown
# May disable this by adjusting UseNTPClientStatus variable
:if (($NTPSyncState="synchronized") or ($UseNTPClientStatus="no")) do {
#    :log info "NTP okay";
    :local CurrentTime [/system clock get time];
#    :log info "Current Time = $CurrentTime";
    :set RadioPower [:put [/interface wireless get value-name=tx-power $RadioName]];
#    :log info "Radio Power = $RadioPower";
    :if ($RadioPower = $LowPower) do {
        :set RadioPowerCut true;
    } else {
        :set RadioPowerCut false;
    }
#    :log info "Radio PowerCut = $RadioPowerCut";
                :log info "Current Time = $CurrentTime / Radio Power = $RadioPower / Radio PowerCut = $RadioPowerCut";

# Where the HighPower time is set earlier than the LowPower time #
    :if ($RadioHighPowerTime < $RadioLowPowerTime) do {

# Radio should be HighPower between these times #
        :if (($CurrentTime > $RadioHighPowerTime) and ($CurrentTime < $RadioLowPowerTime)) do {

            :if ($RadioPowerCut=true) do {
                :log info "Radio was LowPower, now switching HighPower";
                :put [/interface wireless set $RadioName tx-power=$HighPower];
                           :put [/interface wireless enable wlan];
            }
        } else {

            :if ($RadioPowerCut=false) do {
                :log info "Radio was HighPower, now switching LowPower";
                :put [/interface wireless set $RadioName tx-power=$LowPower];
                           :put [/interface wireless disable wlan];
            }
        }
    }

# Where the HighPower time is set later than the LowPower time #
    :if ($RadioHighPowerTime > $RadioLowPowerTime) do {

# Radio should be LowPower between these times #
        :if (($CurrentTime < $RadioHighPowerTime) and ($CurrentTime > $RadioLowPowerTime)) do {

            :if ($RadioPowerCut=false) do {
                :log info "Radio was HighPower, now switching LowPower";
                :put [/interface wireless set $RadioName tx-power=$LowPower];
                           :put [/interface wireless disable wlan];
            }
        } else {
            :if ($RadioPowerCut=true) do {
                :log info "Radio was LowPower, now switching HighPower";
                :put [/interface wireless set $RadioName tx-power=$HighPower];
                           :put [/interface wireless enable wlan];
            }

        }
    }

#} else {
#    :log info "System clock problem, unable to perform operation";
}

#:log info "RadioHighLow Script completed";