Cell Lock and Script Error (I - Invalid)

Hi everyone.
My goal is to lock the parameters I need via a script, then add that script to autorun and apply it after a reconnect. I reviewed this page: https://help.mikrotik.com/docs/spaces/ROS/pages/30146563/LTE+5G and I also used Gemini and ChatGPT since I’m not a developer, but for the past 3 hours, after creating the script, I keep getting an Invalid status. I tried different syntax, I tried adding the script both via terminal and via Winbox (System > Scripts). I tried setting different permissions, or setting it so that permissions aren’t required.

Also, the script does run - I get an “OK” status. Could it be that the LTE interface lte1 is active, meaning the cell lock must be applied when the modem is “not running”, i.e., via modem-init?

Please help me write a correct script.
I won’t post all the non-working versions, but the latest ones looked like this:

/system script set [find name="lte_cell_guard"] dont-require-permissions=yes
:local iface "lte1";
:local targetPci 392;
:local targetEarfcn 1344;
:local dq "\"";

:log info ("LTE guard: enforce lock EARFCN=" . $targetEarfcn . " PCI=" . $targetPci);

:local cmd ("AT+QNWLOCK=" . $dq . "common/4g" . $dq . ",1," . $targetEarfcn . "," . $targetPci);
/interface lte at-chat $iface input=$cmd;

and

/system script add name=lte_cell_guard policy=read,write,policy,test,sensitive dont-require-permissions=yes source={
    :local iface "lte1"
    :local tPci 392
    :local tEarfcn 1344

    :local m [/interface lte monitor $iface once as-value]
    
    :local cPci ($m->"phy-cellid")
    :local cEarfcn ($m->"earfcn")

    :if ($cPci > 0 && $cEarfcn > 0) do={
        :if ($cPci != $tPci || $cEarfcn != $tEarfcn) do={
            :log warning ("LTE Guard: Fix start. PCI " . $cPci . " -> " . $tPci)
            /interface lte at-chat $iface input=("AT+QNWLOCK=\"common/4g\",1," . $tEarfcn . "," . $tPci)
        }
    }
}

As you can see, neither of the two versions worked. I’d like to do it in a “soft” way so that, if needed, I can revert everything using the "restore_band" command.

Could someone please help create a script with my data that definitely works?

ATL R16
RouterOS 7.20.6

Before we get lost in script-land, lets get a handle on whether we even need a script (or further config) in the first place…

  • what channels/frequencies are you trying to lock?
  • Why are you specifically wanting to lock channels?

It might be possible to acheive what you’re trying to do an easier way.

I live outside the city, between two base stations. One is a bit over 2 km away, and the other is a bit over 3 km away. There’s also a fairly tall forest between me and the base stations. All of this results in a fairly weak signal. On top of that, the beam sectors for the bands I need are not positioned ideally - I’m right on the border between sectors.

At first, when I bought the R16 and had it mounted at a height of 6 meters, the device was able to pick up a signal, but it had a lot of “noise.” To try to solve the problem, I installed the antenna on a mast about 12 meters high. It became much better, but still far from ideal. I also had to install the mast on a rotating base and rotate the antenna literally by 1 centimeter at a time to find the optimal direction.

Because of this, I had to dive deeper into the settings. I can write more about it, but in the end, I managed to find the optimal direction and lock onto a good sector. But as I said, I’m on the border between two beam sectors from the tower. That means that after any reconnect, there’s uncertainty — it may or may not connect to the cell I want.

At this point, I managed to find the perfect combination: my primary band is fixed, and I also locked the PCI and EARFCN I need using a command. I was able to get not only about 650/60 speed, but also zero packet loss.

So my task now is to preserve the PCI and EARFCN lock settings across reconnects or reboots. And I want to do it in a “soft” way, so I don’t end up with “no service.” If something goes wrong, I want to be able to simply disable the script and revert everything.