Welcome to Smart Home Forum by FIBARO
Dear Guest,
as you can notice parts of Smart Home Forum by FIBARO is not available for you. You have to register in order to view all content and post in our community. Don't worry! Registration is a simple free process that requires minimal information for you to sign up. Become a part of of Smart Home Forum by FIBARO by creating an account.
As a member you can:
- Start new topics and reply to others
- Follow topics and users to get email updates
- Get your own profile page and make new friends
- Send personal messages
- ... and learn a lot about our system!
Regards,
Smart Home Forum by FIBARO Team
Question
sbowtell 0
Hi,
I’m relatively new to Lua and Fibaro. I have two questions about running Lua functions and accessing Lua tables from other scenes.
Running Functions From Other Scenes:
I have built a scene called "Initialise" which contains a collection of Lua functions. One of these functions is called Arm() and takes the parameter "_status" which indicates how the Intruder Detection System should be set. The "_status" can be:
Disarm = disarms all motion sensors, door and window sensors
Away = arms all motion sensors, door and window sensors
Home = arms all the door and window sensors downstairs (does not arm motion sensors or upstairs window sensors)
I want to call this Arm() function from other scenes, for example I want a scene called "Arm Away" which has a single line of Lua Arm("Away"), and another scene called "Arm Home" which has a single line of Lua: Arm("Home") and another scene called "Disarm" which has a single line of Lua Arm("Disarm"). This way I can have different scenes available to the user which are used to set the Intruder Detection System but I don't need to replicate the same code in all three scenes.
Unfortunately this doesn't work. Although I run the “Initialise” scene which contains the Arm() function (but doesn’t contain any code to call the Arm() function) when I try to run my “Arm Away” scene (which contains a single line of Lua Arm(“Away”)) I get a message saying
[ERROR] 12:59:57: line 1: attempt to call global 'Arm' (a nil value)
Accessing Tables From Other Scenes
I am trying to develop portability into my Lua code for home automation. One of the reasons for this is I have a Fibaro system in two houses and so having generic code which can just be ported into another Fibaro environment does not require any recoding is a real advantage. Another reason is so that if I add a new motion sensor to my house I don't need to change any code to make sure it is included in the code to Arm all the motion sensors.
The way I achieve this is by naming all the zWave devices according to a standard. For example: M0_HALL_MS_01 is the first motion sensor in the Hall on the ground floor of the Main House, G1_BED2_WS_02 is the second window sensor in Bedroom 2 on the first floor of the Guest house. By building a table in Lua of all the devices I can work through that table looking for devices with “MS” in the name when arming all the motion sensors.
So to the question – can I make this table of devices available to other Scenes (not just the “Initialise” scene which was used to create it?). I know how to use Fibaro Global Variables to make data available to multiple scenes but my question relates specifically to a table.
I tried to research this online and from what I understand a Lua variable is local to a function unless it begins with “g_”. So my devices table is called g_devices. When I try to access this table from another scene I get a message saying
[ERROR] 13:01:12: line 1: attempt to index global 'g_devices' (a nil value)
I'd be grateful for any assistance or pointers anyone can provide.
8 answers to this question
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.