Skip to content

Lua source

A source that computes electrical power (W) from a voltage and a current source. Once registered, it appears in every source picker: mixes, logic switches, special functions, other widgets, telemetry screens.

What it demonstrates

  • registerSource callbacks. init sets the unit and decimals; wakeup computes and stores the value with source:value(v).
  • Write only on change. The value is set only when the rounded result differs.
  • Settings per model with configure/read/write.

The script

scripts/powersrc/main.lua
-- Power source: a Lua source computing watts from a voltage and a current
-- sensor. It shows up in every source picker (mixes, logic switches, widgets).
-- https://frskyrc.github.io/ethos-lua-doc/cookbook/lua-source/

local cfg = { volts = nil, amps = nil }

local function sourceInit(source)
  source:unit(UNIT_WATT)
  source:decimals(0)
  source:value(0)
end

local function sourceWakeup(source)
  local v = cfg.volts and cfg.volts:value()
  local a = cfg.amps and cfg.amps:value()
  if v and a then
    local w = math.floor(v * a + 0.5)
    if w ~= source:value() then source:value(w) end   -- write only on change
  end
end

local function sourceConfigure(source)
  local line = form.addLine("Voltage")
  form.addSourceField(line, nil, function() return cfg.volts end, function(s) cfg.volts = s end)
  line = form.addLine("Current")
  form.addSourceField(line, nil, function() return cfg.amps end, function(s) cfg.amps = s end)
end

local function sourceRead(source)
  cfg.volts = storage.read("volts")
  cfg.amps = storage.read("amps")
end

local function sourceWrite(source)
  storage.write("volts", cfg.volts)
  storage.write("amps", cfg.amps)
end

local function init()
  system.registerSource({ key = "power", name = "Power (W)",
    init = sourceInit, wakeup = sourceWakeup,
    configure = sourceConfigure, read = sourceRead, write = sourceWrite })
end

return { init = init }

Use it

Enable it under Model → Lua, pick the voltage and current sources in its settings, then choose Power (W) anywhere a source is offered. For example, a logic switch that turns on above 1500 W, or the built-in Value widget.

Source vs sensor

Lua source (registerSource) Lua-created sensor (model.createSensor)
Appears as A source in pickers A telemetry sensor
Logged, alarms, min/max Like other sources Like real telemetry (sensor page, logging)
Lifetime While the script runs Saved in the model
Best for Derived values for mixes and switches Feeding values into the telemetry system

See Sources & telemetry for sensors.

API used

system.registerSource · Source:value · Source:unit · Source:decimals