Is there a way to do this? If I create scenes that include LUA code to access device variables I need to edit it whenever I have to replace batteries in a sensor. Some sensors transmit an ID that changes whenever the device battery is replaced. When that happens I have to create a new device and delete the old one. I give the new device the same name. I’d rather not have to edit the LUA code to use the new device number. If the LUA code could retrieve the device number using the device name it would not need to be modified.
I agree, it would be REALLY nice if they had provided this as part of luup (also scene and room names while they’re at it).
Here’s what I use instead (not case sensitive)…
function findDeviceByName( name )
local num, obj
name = (name or ""):upper()
for num, obj in pairs( luup.devices ) do
if ( obj.description or "" ):upper() == name then return num, obj end
end
return nil
end
This function returns two values if successful–the device number, and the device “object” (the table containing the device data). If it does not find a matching device, it simply returns nil.
For scenes, change [tt]luup.devices[/tt] to [tt]luup.scenes[/tt], and [tt]description[/tt] to [tt]name[/tt]. For rooms, it’s [tt]luup.rooms[/tt] and [tt]name[/tt]. Oh, and of course, change the function name so it makes sense, too.
i took a cue from akbooer and create two arrays of devices at startup
function defineDevices()
for i,d in pairs(luup.devices) do
-- Device array by name
DEV[d.description] = i
-- device array by device number
DEV_NAME[i]= d.description
end
end
that way i can call devices by name like
luup.variable_get("urn:upnp-org:serviceId:Dimming1", "LoadLevelTarget", DEV["KITCHEN SWITCH"])
Well spotted! I was looking for my original post on this, but hadn’t yet found it.
In fact, these days I might just use the power of arrays in Lua and use a single one for both…
function defineDevices()
for i,d in pairs(luup.devices) do
DEV[d.description] = i
DEV[i]= d.description
end
end
then the lookup works in both directions…
local dev_no = DEV["Kitchen"]
local dev_name = DEV[31]
[quote=“akbooer, post:4, topic:198314”]
function defineDevices()
for i,d in pairs(luup.devices) do
DEV[d.description] = i
DEV[i]= d.description
end
end
then the lookup works in both directions…
local dev_no = DEV["Kitchen"]
local dev_name = DEV[31]
[/quote]
Flipping brilliant… hadn’t thought about doing it that way!!!