functionfindFunction(id)for i, fun inipairs(functions)doif fun.id == id thenreturn functions[i]endendendfunctionhandleTrigger(topic, payload, retained)local data = json:decode(payload)-- Verify that the door was openedif data.value == openState then-- Turn onlocal payload = json:encode({ value = onValue })
mq:pub(controlTopic, payload,false,0)-- Start timer for off..
timer:after(cfg.timeout,function()local payload = json:encode({ value = offValue })
mq:pub(controlTopic, payload,false,0)end)endendfunctiononStart()-- Get all values for the triggerlocal triggerFunction =findFunction(cfg.triggerFunction)
triggerTopic = triggerFunction.meta.topic_read
-- Manually call the updated function to set the initial state of the variablesonFunctionsUpdated()
mq:sub(triggerTopic,0)
mq:bind(triggerTopic, handleTrigger)endfunctiononFunctionsUpdated()-- Get all values for the triggerlocal triggerFunction =findFunction(cfg.triggerFunction)
openState =tonumber(triggerFunction.meta.state_open)
closedState =tonumber(triggerFunction.meta.state_closed)-- Get all values for the control functionlocal controlFunction =findFunction(cfg.actuatorFunction)
controlTopic = controlFunction.meta.topic_write
onValue =tonumber(controlFunction.meta.state_on)
offValue =tonumber(controlFunction.meta.state_off)end