Lua Script in data to server section

I want to upload my plc tags coming via modbus tcp to mqtt by data to server section i also want to add some offset in it so i want to use lua script

but by using following script i am not getting data uploaded on mqtt

In data configuration:
Type: Modbus
Format type: Lua Script
script is: local tag1 = tonumber(data[“Temp”])
local sum = tag1 + 5

In collection configuration:
again
Format type: Lua Script
Script is: { “sum”: “%=return sum%” }

but no data is uploaded to by mqtt platform

Hello,

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?

Best regards,

Thanks Martynas for reaching out

  1. I have defined tag1 variable to store the value of Temp variable coming from modbus device.
  2. I have tried to replace double quotes with 2 single quotes as per your recommendation but still data is not uploaded
  3. yes data is uploaded to MQTT client in JSON and Custom formats.

Any update for this because this steps aren’t working.

Still my Error is not being Solved

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?

Hello,

Apologies for the delayed response.

There are a few potential issues with your current implementation:

  1. 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.
  2. The variable references and formatting need a slight adjustment to align with how the router’s Lua environment handles data mapping and JSON formatting.
  3. 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)

Best regards,