Skip to main content

Splitting Lua Script into Multiple Files

To split example ucm.lua file into several files and use it in the blueprint follow the steps below.

Create Lua Directory

  • Create a new directory named src. You can use another name if you wish.
  • Put into this directory two files main.lua and command.lua.
Lua scrtip entry point

When you are using multiple files you should have main.lua file as an entrypoint of your script.

-- Load module command from command.lua file
command = require("command")

-- Obtain device properties and send it to Enapter Cloud
function send_properties()
-- We could have called Modbus API here, but use
-- constant value for the example simplicity
serial_number = "AC001215"

-- Obtain device telemetry attributes and send it to Enapter Cloud
function send_telemetry()
-- Again, we use constant value for simplicity
h2_concentration = 0.01,
-- Pass active_alerts into telemetry
alerts = command.active_alerts

-- Register periodic function, will send properties every 30 seconds
scheduler.add(30000, send_properties)

-- Register periodic function, will send telemetry every 1 second
scheduler.add(1000, send_telemetry)

-- Register "beep" and "silence" commands, the function passed as a second argument
-- will be executed every time user starts the command from UI
enapter.register_command_handler("beep", command.beep)
enapter.register_command_handler("silence", command.silence)

Update Manifest File

Update communication_modules section in manifest.yml. Replace lua_file section with lua to set Lua directory:

product: ENP-RS485
dir: src

To learn more about lua section see the reference.

Upload Blueprint

Now you can upload the blueprint.

Hardware diversity is welcome. Integrate any device into a unified energy network.
© 2022 Enapter
Developer toolkit