Feeding Lua Float into MAVLink Encode Gets NIL Ouput

I am trying to read CAN data with a Lua script (which I can do successfully) and then encode a MAVLink message with the data that I am pulling from that CAN input. I am running into an issue where I read in the CAN data successfully (the printed value is what I expect), but when I try and encode the MAVLink message, I get a “expected number, got nil” error in the mavlink_msgs.lua file (line 158 where it tries to execute the “pack” function). I have verified that I am getting the correct message ID (225 for EFI_STATUS) and that the cht_val is of type “number” which as i understand it, is a float as far as lua is concerned (which is also what the “encode” function is expecting for all of its table values). Here is a snippet of my code that I am working with:

– Load CAN driver1. The first will attach to a protocol of 10, the 2nd to a protocol of 12
– this allows the script to distinguish packets on two CAN interfaces
local driver1 = CAN:get_device(5)
–local driver2 = CAN:get_device2(5)
local mavlink_msgs = require(“MAVLink/mavlink_msgs”)

– Verified is printing 225
local EFI_STATUS_ID = mavlink_msgs.get_msgid(“EFI_STATUS”)
gcs:send_text(6, "EFI msg ID = " … tostring(EFI_STATUS_ID))

local msg_map = {}
msg_map[EFI_STATUS_ID] = “EFI_STATUS”

– Unclear what to set this to for my situation or how much it matters
mavlink:init(20,1)

– Verified is printing “true”
local rec_id = mavlink:register_rx_msgid(EFI_STATUS_ID)
gcs:send_text(6, "receiveID? = " … tostring(rec_id))

– convert binary
function tobinary(num)
local binary = “”
for i = 8, 0, -1 do
binary = binary … ((num & (1<<i)) > 0 and “1” or"0")
end
return binary
end

– cht value, 1000
function show_CHT(dnum, frame, idx1, idx2)
local var = tonumber((frame:data(idx1)*256 + frame:data(idx2)) * 0.1)
gcs:send_text(6,string.format(“CAN[%u] msg: CHT = %f C”, dnum, var))
gcs:send_named_float(‘ECHT’, var)
logger:write(‘ECHT’,‘CHT(C)’,‘f’,var)
if var <= 90 then
gcs:send_text(0, “Cylinder Head Temp LOW”)
elseif var > 90 and var <= 130 then
gcs:send_text(6, “Cylinder Head Temp in range.”)
elseif var > 130 and var <= 170 then
gcs:send_text(1, “Cylinder Head Temp HIGH”)
elseif var > 170 then
gcs:send_text(0, “Cylinder Head Temp CRITICAL”)
end
return var
end

local CHTrate = 50 – CHT, MAP, voltage, throttle percent
local RPMrate = 60 – RPM, fuel flow rate
local baroRate = 1000 – barometer
local MATrate = 500 – MAT, coolant temp
local AFRrate = 10000 – AFR, warm up enrichment
local currRate = 1000 – Gen Temp, ECU Temp, Throttle Servo Current, Duct Servo Current, Fuel Pump Current
local EWTRate = 1000 – EWT, Fuel Remaining
local IngRate = 2000 – Ingnition and Injector Current
local SysStaRate = 1000 – System Status
local EngStaRate = 1000 – Engine Status
local CHT_time = 0
local RPM_time = 0
local baro_time = 0
local MAT_time = 0
local AFR_time = 0
local curr_time = 0
local EWT_time = 0
local Ing_time = 0
local SysSta_time = 0
local EngSta_time = 0

– get time in ms
function get_time_sec()
return millis():tofloat()
end

function update()
– Eventually I will read in all the engine data, but for now I am focusing on CHT which is why all – the others are commented out.
frame = driver1:read_frame()
local currentTime = get_time_sec()
if frame then
local frameStr = tostring(frame:id())
shortStr = string.sub(frameStr, 7)
local frameNum= tonumber(shortStr)
if frameNum == 5520 then
if currentTime - CHT_time >= CHTrate then
–show_voltage(1, frame, 0)
–show_TPS(1, frame, 1)
–cht_val = show_CHT(1, frame, 2, 3)
cht_val = tonumber((frame:data(2)*256 + frame:data(3)) * 0.1)
–show_MAP(1, frame, 6, 7)
CHT_time = currentTime
gcs:send_text(0, "cht_val = " … tostring(cht_val))
gcs:send_text(0, tostring(type(cht_val)))
end
end
end
local msg, chan = mavlink:receive_chan()
gcs:send_text(6, "receive chan = " … tostring(chan))

local efi = {}
        efi.ecu_index = 0
        efi.rpm = 0
        efi.fuel_consumed = 0
        efi.fuel_flow = 0
        efi.engine_load = 0
        efi.throttle_position = 35
        efi.spark_dwell_time = 0
        efi.barometric_pressure = 0
        efi.intake_manifold_pressure = 0
        efi.intake_manifold_temperature = 23
        efi.cylinder_head_temperature = cht_val
        efi.ignition_timing = 0
        efi.injection_time = 0
        efi.exhaust_gas_temperature = 0
        efi.throttle_out = 0
        efi.pt_compensation = 0
        efi.health = 0
        efi.ignition_voltage = 0
        efi.fuel_pressure = 0

-- This line throws the errors
mavlink:send_chan(0, mavlink_msgs.encode("EFI_STATUS", efi))

if (msg ~= nil) then
    local parsed_msg = mavlink_msgs.decode(msg, msg_map)
    if (parsed_msg ~= nil) then
        gcs:send_text(6, "parsed msg id = " .. tostring(parsed_msg.msgid))
    end
else
    gcs:send_text(0, "Message = NIL")
end    

return update, 10

end

return update()