Hi all,
I’m trying to create a scene using Luup script that checks all of my window & door sensors to see if they’re tripped or not. I know that I can create a bunch of if/then statements with the device id hard coded for each sensor i currently have, then add a new if/then statement with the device I’d whenever I add another sensor. BUT, I’d rather have my scene check for all devices of the correct type, then loop through them. Obviously this is better because my scene automatically checks new sensors without me having to add them manually. I’ve been fooling around with the Luup.devices command to accomplish this, but my first issue is that it appears that there my device ids are not contiguous and it causes my scene to error and stop. Below is my test code which loops through device ids 21 through 32, and logs whether or not a actual device is present for that I’d number. This is not working, as you can see below the script, I copied the Luup log entries that are produced. When this scene runs. The first id number (21) has an actual device (my wife’s garage door sensor,) and the log output is beautiful, but the next ID number (22) has no device and it appears to break. Can anyone explain why it breaks and how I can fix my script? Perhaps there’s an easier way to get all of my sensor devices loaded into an array for easy access???
for d=21,32 do
if luup.devices[d].id == nil then
luup.log(‘=> No device with that id!’)
else
luup.log('===>Device # ’ … d … ’ id: ’ … luup.devices[d].id … ’ name: ’ … luup.devices[d].description … ’ type: ’ … luup.devices[d].device_type … ’ category: ’ … luup.devices[d].category_num)
end
Luup.log entries
50 05/13/14 23:31:30.591 luup_log:0: ===>Device # 21 id: 18 name: C Garage Door type: urn:schemas-micasaverde-com:device:DoorSensor:1 category: 4 <0x2e2bd680>
01 05/13/14 23:31:30.591 e[31;1mLuaInterface::CallFunction_Scene Scene 52 failed [string “function scene_51()…”]:12: attempt to index field ‘?’ (a nil value)e[0m <0x2e2bd680>