LuCi: calling commands in lua

Hello.
I have a problem with LuCI again...
I have an htm page like this:
applications/luci-app-tiod/luasrc/view/tiod.htm:

<%
local sys = require "luci.sys"

local sessions = sys.exec("/usr/bin/tmux list-sessions")

if not string.match(sessions, "con1") then
    sys.exec("/usr/bin/tmux new -d -s con1 /usr/bin/ttyd -p 2321 tio -b 115200 /dev/ttyS4")
end
%>
<%+header%>
<div style="align: left; width: 100%; height: 100%;">
    <iframe style="width: 100%; min-height: 500px; border: none; border-radius: 3px; resize: vertical;" 
    src="http://192.168.0.171:2321"></iframe>
</div>
<br>
<%+footer%>

I'm using tmux to create multiple sessions on multiple ports for each rs232 connection.
The problem is that I can enter the command manually through the console:
root@NTR-100:~# /usr/bin/tmux new -d -s con1 /usr/bin/ttyd -p 2321 tio -b 115200
/dev/ttyS4.
And then everything works:


But when the page loads, if the tmux session has not yet been created, then this is what happens:

It doesn't connect and keeps rebooting. If you go to this address separately, then everything is the same. And only if you delete this session through the terminal and create it again in the terminal (and not using lua commands), then everything works again.
Moreover, the script works even after the page is loaded, if you write tmux list-sessions, then there is a session there (though I don’t know how to view it completely, perhaps it is written incorrectly).

After some experimentation I realized that the problem was with the ttyd utility.
The fact is that when rendering pages, a tmux session is created with the ttyd process, which creates a terminal on the desired port with the tio command, but it creates it more than once, which breaks ttyd, because for some reason, after creation, abruptly turning off and turning on again, it does not creates a new terminal.
But I found an interesting thing: if you kill the tmux server using bash scripts and create them again (again with bash scripts), then it should work.
I wrote a convenient config for myself and I want that after clicking the “Apply” button, a bash script will be created with the necessary commands and run. How can this be done through lua??

As a result, I solved this problem with a crutch:

<%
local sys = require "luci.sys"

local speed = sys.exec("uci get tiod.@con1[-1].speed"):gsub("\n", "")
local databits = sys.exec("uci get tiod.@con1[-1].databits"):gsub("\n", "")
local parity = sys.exec("uci get tiod.@con1[-1].parity"):gsub("\n", "")
local stopbits = sys.exec("uci get tiod.@con1[-1].stopbits"):gsub("\n", "")

local command = string.format("tio -b %s -d %s -p %s -s %s /dev/ttyS4", speed, databits, parity, stopbits)

sys.call("uci delete ttyd.@ttyd[-1]")
sys.exec("uci add ttyd ttyd")
sys.call("uci set ttyd.@ttyd[-1].port=2321")
sys.call("uci set ttyd.@ttyd[-1].command='"..command.."'")
sys.call("uci commit ttyd")
sys.call("/etc/init.d/ttyd reload")
%>
<%+header%>
<div style="align: left; width: 100%; height: 100%;">
    <iframe style="width: 100%; min-height: 500px; border: none; border-radius: 3px; resize: vertical;" 
    src=""></iframe>
</div>
<script>
    window.addEventListener('DOMContentLoaded', () => {
        const currentIP = window.location.hostname;
        const currentPort = 2321;

        const iframe = document.querySelector('iframe');
        iframe.src = `http://${currentIP}:${currentPort}`;
    });
</script>
<%+footer%>

There are native lua uci methods, using sys.exec/sys.call for getting/setting uci values is not the best way of doing it.

Yes, I am aware of the existence of the uci api, but the add method did not work for me and I still did not understand why, so I implemented it through terminal commands.