(Last Updated: April 28, 2020)

Lua match device



  • A little function to match a device based on, room and/or device name, including wildcard.

    function matchDev (pattern, rooms)
         local wanted = {}
         for _, r in ipairs ((type(rooms) == "table") and rooms or {rooms}) do wanted[r] = r end
         return function(_, last)
             while true do
                 local d
                 last,d = next( luup.devices, last )
                 if not last or (d.description:match( pattern ) and (not rooms or wanted[d.room_num])) then
                     return last,d
                 end
             end
         end
    end
    

    Example #1: Lock all Deadbolt in any room:

    for n,d in Tools.matchDev('^TDB%-') do  -- All doors lock
        luup.call_action ("urn:micasaverde-com:serviceId:DoorLock1", "SetTarget", {["newTargetValue"] = 1}, n)
        --luup.sleep (200)
    end
    

    Example #2: Switch OFF all binary light in specific room

    for n,d in Tools.matchDev('^L%-',{13,16,21,6,5,4,24}) do
        luup.call_action ("urn:upnp-org:serviceId:SwitchPower1", "SetTarget", {["newTargetValue"] = 0}, n)
        --luup.sleep (200)
    end
    

    I'm using a lot of these lua code for scene like "Switching to sleep mode"


Log in to reply