Could you clarify what the tag1 variable represents? It seems the issue might be in your Lua script’s syntax. Lua uses double square brackets ["key"] instead of curly quotes “key”, so, instead of data[“Temp”], try using data["Temp"] and, in sum’s returning script, { "sum": %=sum% }.
Additionally, is the Modbus data being received on your MQTT client in other JSON or custom formats?
Good Day Teltonika people,
Any update for this? I am also waiting for your answer.
Is there any manual or user guide on how to work with Lua scripts for formating the Data to server data?
There are a few potential issues with your current implementation:
You’re currently using two separate Lua scripts, whereas typically, you’ll only need a single Lua script within the data configuration to process and prepare your data before sending it to MQTT. P.S. A separate script approach will also be mentioned below.
The variable references and formatting need a slight adjustment to align with how the router’s Lua environment handles data mapping and JSON formatting.
The data access method and output structure should be unified and handled cleanly within one script.
Here are some Lua script and format examples you could use, which might be a useful starting point.
Script example
-- Data Configuration Lua Script (use this in the Data section)
local result = {}
-- Process each tag from Modbus
for tag_name, tag_value in pairs(data) do
-- Apply offset only to specific tags if needed
if tag_name == "Temp" then
result[tag_name] = tonumber(tag_value) + 5 -- Adding 5 as offset
else
result[tag_name] = tonumber(tag_value) -- Other tags without offset
end
end
-- Convert to JSON string for MQTT
local json = require("json")
return json.encode(result)
Lua format xample
--Available variabes:
-- data_sender.section - config section name
-- data_sender.version - LUA plugin version
-- env - data to be formated
--
--String value must be returned
local prefix_map = {
Test_Temp = "Temperature: ",
Analog_input = "Output: :"
}
local function format_value(data)
local name, value = data.name, data.data
local prefix = prefix_map[name]
if name and value and prefix then
return prefix .. value .. "\n"
end
return ""
end
local function handle_format_request(data)
local result = ""
for _, v in ipairs(data) do
if type(v) == "table" then
result = result .. format_value(v)
end
end
return result
end
Separate scripts approach:
Example
-- Read value from Modbus data (replace "Temp" with your Modbus tag)
local tag1 = tonumber(data["Temp"]) or 0
-- Add offset
local sum = tag1 + 5
-- Return result as a JSON string
return string.format('{ "sum": %d }', sum)
This script takes your Modbus TCP data with tag “Temp”, adds 5, and returns it as a JSON string.
You don’t actually need a separate script here if your data config already returns JSON. The Data to Server service will send this payload directly to MQTT.
But if you want to wrap or modify it here too:
-- Access the 'sum' field from env
local sum = tonumber(env.sum) or 0
-- Return final JSON string
return string.format('{ "sum": %d }', sum)