find device id by search pattern

…so what’s the problem? I don’t quite understand.

That I will need to make my search pattern very precise. For example, just the word Basement, can give me 3 rooms and Outside can give me 6 results.

Well, if you’re just doing a simple search, then, yes. I thought you would be using the exact name? This is, perhaps, what @rigpapa meant by “a whole new can of worms”.

Searches can also be made to look for “ends with …” or such like, too.

I think I will stick with the room number :wink:

thanks a lot rigpapa and AK for this help this morning. :wink:

rigpapa, AK,

Possible to convert the room parameter into an array to be able to pass more than one room and act like a OR :wink:

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

with this you can write things like…

for n,d in matchDev "^D-" do ... end

for n,d in matchDev ("^D-", 3) do ... end

for n,d in matchDev ("^D-", {1,3,5}) do ... end

…now go and learn Lua properly so you can do it yourself! :wink:

Thanks AK :wink:

It’s in my todo list of 2019… learn Python from 0% and dig a little more into LUA!

AK,

Do you think it’s possible that a FOR loop is running too fast for openLuup and that some “command” can be ignored ?

I’m using a FOR loop to “reset” all my smartswitch devices (from the smartswitch plugin) and having some device that are not also reset correctly and it’s not always the “same”.

Should I add a “wait/sleep” in the loop ?

Would need a bit more info on that like the actual code plus some logs.