Luup Scenes Events
Micasaverde (Talk | contribs) (New page: == Adding Lua code to scenes and events == If you didn't already come from the Luup_Intro page, start there to see what Scenes & Events are intended for. To add Lua code to a scene, ...) |
Micasaverde (Talk | contribs) |
||
Line 8: | Line 8: | ||
When you edit the Lua code in a scene or event you must click 'Save' before the code is saved. Then you can activate the scene or trigger the event to see what happens when your code is run. This is tedious when you're still debugging your Lua code and want to be able to immediately test your code and debug it without saving in between each time or triggering the event manually. To learn how to do debug your Lua code interactively see: [[Lua_Debugging]] | When you edit the Lua code in a scene or event you must click 'Save' before the code is saved. Then you can activate the scene or trigger the event to see what happens when your code is run. This is tedious when you're still debugging your Lua code and want to be able to immediately test your code and debug it without saving in between each time or triggering the event manually. To learn how to do debug your Lua code interactively see: [[Lua_Debugging]] | ||
+ | |||
+ | Note that all the Lua code in your scenes and events run in a single Lua instance, which is separate from any Lua plugins. This means if you set a global variable in one scene, or create a function inside the Lua code in a scene, then in another scene's Lua code you can use that global variable or call that function. You do not need to worry about conflicting with a Luup plugin, though, since they have their own Lua instance, meaning they have their own global variables and functions. | ||
== Samples == | == Samples == |
Revision as of 20:01, 9 June 2009
Adding Lua code to scenes and events
If you didn't already come from the Luup_Intro page, start there to see what Scenes & Events are intended for.
To add Lua code to a scene, create the Scenes and click 'Add Lua'. Fill in your Lua code in the input box. You can use any [Lua] commands as described in the [Lua reference manual]. You can also use any of the variables and functions that the Luup engine adds to Lua documented here: Luup_Lua_extensions. The Lua code will be run every time the scene is activated either by the user or a scene or a timer. The Lua code is run before the commands that you included in the scene. If your Lua code ends with this: "return false" then the commands in the scene will not be run.
You can also add Lua code to an event. Since events are attached to scenes anyway, there is usually little difference between adding the code to an event, or to the scene the event is part of. If the Lua code in an event returns false, then the event is aborted, meaning the scene that the event is attached to will not be triggered by the event. The main reason for attaching Lua code to an event is if you have multiple events to a scene. For example, you may have a scene called "Turn lights on in hallway" which is triggered by 2 events: 1) The front door opens, and 2) the motion sensor in the hallway is tripped. Perhaps whenever the front door opens you always want the scene to be activated, but you only want the motion sensor to activate the scene before 10:00. In this case, you would add Lua code to the motion sensor's event which does something like (pseudo-code): "if time>10:00 return false". That way the event from the motion sensor will be aborted if it's after 10:00, but the event from the front door will always activate the scene regardless.
When you edit the Lua code in a scene or event you must click 'Save' before the code is saved. Then you can activate the scene or trigger the event to see what happens when your code is run. This is tedious when you're still debugging your Lua code and want to be able to immediately test your code and debug it without saving in between each time or triggering the event manually. To learn how to do debug your Lua code interactively see: Lua_Debugging
Note that all the Lua code in your scenes and events run in a single Lua instance, which is separate from any Lua plugins. This means if you set a global variable in one scene, or create a function inside the Lua code in a scene, then in another scene's Lua code you can use that global variable or call that function. You do not need to worry about conflicting with a Luup plugin, though, since they have their own Lua instance, meaning they have their own global variables and functions.
Samples
This page is a wiki which anyone can edit. If you have some Lua code you think other users might find useful, feel free to add it here:
Ex: Abort a scene if the temperature is outside of a range
add snippets here...