Dual Sim Failover Script for Mikrotik

Here we have tested on 2 sims, one from Telstra & another from Optus:

{
:global initTimeout 60
:global connectTimeout 60
:global minimumSignalLevel -97

:global switchSIM do={
	:local simSlot [/system routerboard modem get sim-slot]
                                               		:if ($simSlot= "down") do={
		:log info message="Switching to \"up\" sim slot (Telstra)"
/interface lte set [find] apn-profile=telstra		
/system routerboard modem set sim-slot=up
	} else={
		:log info message="Switching to \"down\" sim slot (Optus)"
                            
/interface lte set [find] apn-profile=optus
/system routerboard modem set sim-slot=down
	}
}
:global initialize do={
	:global initTimeout
	:local i 0
	:while ($i < $initTimeout) do={
		:if ([:len [/interface lte find ]] > 0) do={
			:return true
		}			
		:set $i ($i+1)
		:delay 1s
	}
	:return false
}
:global waitConnect do={
	:global connectTimeout
	:local i 0
	:while ($i < $connectTimeout) do={
		:if ([/interface lte get [find name="lte1"] running] = true) do={
			:return true
		}
		:set $i ($i+1)
		:delay 1s
	}
	:return false
}
:if ([$initialize] = true) do={
	:if ([$waitConnect] = true) do={
		:local info [/interface lte info lte1 once as-value]
		:local rssi ($info->"rssi")
		:if ($rssi < $minimumSignalLevel) do={
			:log info message=("Current RSSI ".$rssi." < ".$minimumSignalLevel.". Trying to switch active sim slot.")
			$switchSIM
		}
	} else={
		:log info message="GSM network is not connected. Trying to switch active sim slot."
		$switchSIM
	}
} else={
	:log info message="LTE modem did not appear, trying power-reset"
	/system routerboard usb power-reset duration=5s
}		
}

And then a scheduler to run this script after a certain period of time:

/system scheduler add interval=3m on-event=failoverScript name="SIM Switch"

Hope this will help many geeks :wink:

This is great! Many thanks for this. Will this script fall-back to SIM-A (Telstra) after a time period or if the SIM-A connection is restored? I’m using Optus (SIM-B) as my back-up with cheap 25GB PAYG plan so the ability to auto fall-back to Telstra ASAP is desired.

Hi Mike,
This script will enable you to do that, basically, it will check signal levels & switch the active SIM. Please make sure you do have a scheduler to run this script at a set interval of time, so that, it keeps checking the RSSI & switches it accordingly.
You can either do that using the scheduler option in winbox, or from the terminal, you could use something like:
/system scheduler add interval=3m on-event=failoverScript name=Failover

Hi Sajal - thanks for coming back to me. So the script will default back to SIM A or do I need to trigger it back myself? The RSSI for Optus may be as high (or higher) so that won’t be enough to trigger to revert to SIM A? I would prefer the script revert to SIM A after 4 hours for example? Is that easy enough to do?

Yes that is doable, it all depends, how often you run this script using the scheduler. So, in above example, every 3 minutes it will run the script, if your main SIM is getting back enough signal, it will switch it back to that SIM.

1 Like