--[[ If any SLAVE relay is on then the MASTER relay should be on too. Assumption: When the script starts all slaves are OFF The script will update that assumption as events are received. --]] module(...,package.seeall) require("xap") require("xap.bsc") info={ version="1.0", description="any slave on, master on" } slaveStates={'off','off','off','off'} masterState="off" local function has_value (tab, val) for index, value in ipairs(tab) do if value == val then return true end end return false end -- Filter Callback local function anyOn(master) -- Strip the Relay ID from the source and record the state local id = xap.getValue("xap-header","source"):sub(-1) slaveStates[tonumber(id)] = xap.getValue("output.state","state") -- Is any slave relay on? if has_value(slaveStates,'on') then newState='on' else newState='off' end if masterState ~= newState then bsc.sendState(master, newState) masterState = newState end end function init() local masterRelay = "dbzoo.livebox.Master:relay.1" f = xap.Filter() f:add("xap-header","source","dbzoo.livebox.Slave:relay.*") f:add("xap-header","class","xapbsc.event") f:callback(function() anyOn(masterRelay) end) -- Make sure we always have the correct state. -- Handle HAH reset with master/slaves still on f = xap.Filter() f:add("xap-header","source",masterRelay) f:add("xap-header","class","xapbsc.info") f:callback(function() masterState = xap.getValue("output.state","state") end) f = xap.Filter() f:add("xap-header","source", "dbzoo.livebox.Slave:relay.*") f:add("xap-header","class", "xapbsc.info") f:callback(function() anyOn(masterRelay) end) end