PDA

View Full Version : Controlling a light through DM in LUA



jwilson56
November 7th, 2008, 07:43 AM
-- Change to a light on your system, This happens to be a 'switch' type not a dimmer

local path = 'Fordrat8\\ElkM1EZ8\\Light 14'

local device = DeviceManager:GetDeviceUsingPath (path)

if device then
device:Print ()
else
print ('No Device')
end

local controlid = 'Switch'
local control = device:GetControl (controlid)
if control then
print ('Shutoff Workroom Light')
control:SetValue ("Off")
end




-- Now this is for a dimmer light

local path = 'Fordrat8\\ElkM1EZ8\\Light 12'
local device = DeviceManager:GetDeviceUsingPath (path)

if device then
device:Print ()
else
print ('No Device')
end

local controlid = 'Level'
local control = device:GetControl (controlid)
if control then
print ('Turning on Family Room Lights')
control:SetValue (100)
end

dsmes
November 7th, 2008, 02:40 PM
The problem I have with this- How do you tell what Light 14 is?
IMHO, it would be helpful to have some Lua code support the following methods that would make lighting control simpler and more intuitive:
Lights:Control('Workroom Light', 'Off')
Lights:SetLevel('Family Room Lights', 100)

Mike C
November 8th, 2008, 07:13 PM
that would be something like....




local device = DeviceManager:GetDeviceUsingLocationName ('Home\\Basement\\Workroom\Lights')

device:SetControl ('Level',0)

or

device:SetControl ('Switch','Off')

dsmes
November 8th, 2008, 07:49 PM
Thanks Mike! I'll study this. Looks like it's easier than I thought.

jwilson56
November 8th, 2008, 09:48 PM
that would be something like....




local device = DeviceManager:GetDeviceUsingLocationName ('Home\\Basement\\Workroom\Lights')

device:SetControl ('Level',0)

or

device:SetControl ('Switch','Off')



Mike is there a way to use Location/Name (GetDeviceUsingLocationName) in the NR LUA code or something equivalent?

Rob H
November 9th, 2008, 03:47 AM
that would be something like....




local device = DeviceManager:GetDeviceUsingLocationName ('Home\\Basement\\Workroom\Lights')

device:SetControl ('Level',0)

or

device:SetControl ('Switch','Off')


You should be aware however that GetDeviceUsingLocationName only retrieves the first device matching that location/name. If you have more than one then you should probably use GetDevicesUsingLocation which returns a table of the devices at that location. Normally there is only one device but this is not enforced so it's quite possible that the device you get back may not be the one you intended.

jwilson56
November 9th, 2008, 08:47 AM
Yes... Harleydude explained that to me last night on MSN. Personally I do not have a problem using path but wanted to see if there was a way for some other people.

jwilson56
November 9th, 2008, 09:01 AM
Is there any interest in seeing a NR CCF example for turning lights on and off and having dim up and down buttons instead of sliders? I ruffed one out for someone but will polish it up and upload it if others want to see it.

Solosid
November 9th, 2008, 09:41 AM
Hi John,

Yeah there's always interest in how things are done from me....

As I'm learning I'm constantly asking myself...is that right...can it be done different....what if....

so to see some examples from my point of view is the best way to learn

all this LUA stuff & State stuff is overwhelming for me to take in just reading

I would also like to see internet radio, photo album with slideshow?


Cheers

Mark

jwilson56
November 9th, 2008, 06:47 PM
Ok here is the small example that shows how to turn lights on and off and dim with buttons. It also has an example for non dimming switches or appliance modules.

You will have to go through each button and change the 'path' to a light or switch you want to control. Also change the 'state' variable to that same light so that the little light bulb will show the correct status. Last change the % Dim value display Element Name to the correct 'path' also.

Good Luck...and remember there are many ways to get what you want. This is just one.....

Rob H
November 10th, 2008, 02:42 AM
To cut down on all the editing required, it's probably a good idea to put all the buttons in a frame with the name of the path to the device, then use relative naming on the buttons. e.g. suppose the device was Fordrat8\ElkM1EZ8\Light 14 - you would use that as the name of the frame. Then a button in that frame would be called <^>. In fact, anywhere the name of the button is used you would substitute <^>, including in state rules and Lua code.

If used in a Lua string then use [[ ]] notation to delimit the string.

jwilson56
November 10th, 2008, 07:10 AM
Yep forgot that trick. Here is an updated example using the relative naming. Now just rename the frame and your all set to go. Thanks Rob

Again all you need to do to reskin this is to change the graphics to yours and rename the frame to one of your device paths. In fact once you have the graphics reskinnned you can copy and past the frames as many times as you like and just rename the frames to get all your light controls quickly.

etron
November 10th, 2008, 07:13 AM
I am a little confused here. I understand how to get this to work, but why can't we put the 'actions' where they belong, in the button's Actions tab (without having to write extra code ourselves)? It just makes more sense IMO.

quixote
November 11th, 2008, 04:42 PM
Even though I can now control my lights through Lua, I have yet to be able to figure out how to use this in a way that will give me the percentage of the light:



local device = DeviceManager:GetDeviceUsingLocationName ('Den\\Sconces')
device:GetControlValue ('Level')


Can someone please clarify how I could take the returned value and use it in another line of script? I've tried printing the table (device), but it generates a huge list of variables that includes what I assume to be everything from the device manager.

Thanks in advance.

harleydude
November 11th, 2008, 05:13 PM
Try this


local device = DeviceManager:GetDeviceUsingLocationName ('Den\\Sconces')
local level = device:GetControlValue ('Level')

quixote
November 11th, 2008, 05:26 PM
Bingo! Thanks, Rick.