--[[ 10/06/2023 El Zorro MQTT Pubishing script It will send data to NodeRed to turn on Marantz Tuner if we are in alarm situation. --]] local lhost = "192.168.1.30" local lport = "1883" local MQTT_DEVICES = { 'INTRUDER_ALERT', 'uptimeMinutes' -- 'Chamber 1 D/W SW' -- 'Chamber 2 D/W SW', -- 'Chamber 3 D/W SW', -- 'Bathroom D/W SW', -- 'Kitchen D/W SW', -- 'Hall Motion Sensor', -- 'Living Motion Sensor SW', -- 'Chamber 1 Home Security SW', -- 'Chamber 2 Home Security SW', -- 'Smoke Detector 1 Alarm' } -- local myDevicename = 'MQTT trigger' -- change to the name of the device that will trigger this script return { on = { devices = { 'Fibaro Wall Plug 1 SW', 'Hall Siren_switch', 'Chamber 1 Siren_switch' }, timer = {'at 17:54', }, variables = { 'INTRUDER_ALERT', 'startDateTime' }, scenes = {}, groups = {}, security = {}, httpResponses = {}, customEvents = {}, system = { 'start' } }, logging = { level = domoticz.LOG_DEBUG, -- change to LOG_ERROR when ok }, execute = function(domoticz) local myMessage = 'enter your message here' -- local myTopic = 'whatever/topic/you/need' local myTopic = 'domoticz/out' local minutes = domoticz.startTime.minutesAgo%60 local function osCommand(cmd) domoticz.log('Executing Command: ' .. cmd,domoticz.LOG_DEBUG) local fileHandle = assert(io.popen(cmd .. ' 2>&1 || echo ::ERROR::', 'r')) local commandOutput = assert(fileHandle:read('*a')) local returnTable = {fileHandle:close()} if commandOutput:find '::ERROR::' then -- something went wrong domoticz.log('Error ==>> ' .. tostring(commandOutput:match('^(.*)%s+::ERROR::') or ' ... but no error message ' ) ,domoticz.LOG_DEBUG) else -- all is fine!! domoticz.log('ReturnCode: ' .. returnTable[3] .. '\ncommandOutput:\n' .. commandOutput, domoticz.LOG_DEBUG) end return commandOutput,returnTable[3] -- rc[3] contains returnCode end local function sendMQTT(message, topic) local MQTTTopic = topic or 'domoticz/out' lcmd = 'mosquitto_pub -h ' .. lhost .. ' -p ' .. lport .. ' -t ' .. "'" .. MQTTTopic .. "'" .. ' -m ' .. "'" .. message .."'" -- print(lcmd) osCommand (lcmd) end -- domoticz.variables("uptimeMinutes").set(domoticz.startTime.minutesAgo) -- for _, variable in ipairs(MQTT_DEVICES) do -- myDevice = dz.devices(device) -- if myDevice.lastUpdate.minutesAgo < 2 then -- local myMessage = tostring(myDevice.active) -- domoticz.log('myMessage: ' .. myMessage,dz.LOG_FORCE) -- local myTopic = 'domoticz/custom/' .. myDevice.idx -- local myTopic = 'domoticz/out/' .. myDevice.idx -- sendMQTT(myMessage, myTopic) -- end name = domoticz.variables(variable).name payload = domoticz.variables(variable).value myMessage = '{' ..'"name" : ' .. '"' .. name .. '"' .. ' , "dtype" : "variables", "nValue" : ' .. payload .. '}' -- print (myMessage) sendMQTT(myMessage, myTopic) end end }