Jump to content

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


  • 1

How to run Lua Functions from another Scene


sbowtell
 Share

Question

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.

 

 

 

 

 

Link to comment
Share on other sites

8 answers to this question

Recommended Posts

  • 1

I am not not deep enough into lau language particularly function / execution scope etc but could you define user common functions storing these as a textual definition in a global variable and then "load and parse on the fly" into your executing scene?

Step 1

Write, test common functions as normal eg My.ARM as per original post

step 2

Store function definition "code" in global variable

Then first step in any scene is

A. Fetch global functions

B. Parse/load into execution scope

C. Use as normal functions in rest of scene as if defined there?

Can we do step B.?

Make sense?

Link to comment
Share on other sites

  • 0
  • Inquirer
  • @marecki_0luk1 - Thanks for the reply.  Does this mean that if people have a piece of code that is identical to two different scenes the code must be replicated in each scene?  Seems very inefficient as every time I improve a function I need to copy and paste it into all the other scenes.  Global functions is a good description of what I am trying to achieve.  

    In relation to my other question, I'm not trying to call a scene with a parameter I'm trying to access a table that was built in another scene.  Basically I'm trying to work out how to access "global tables".  But I'm guessing now this is also not possible.

    Link to comment
    Share on other sites

    • 0

    Yes people have the same code in few scenes. I understand what you are trying to achive but there is only possible to define global variable, you can cheat and store value as "{X, Y}". Go to panels and see variables panel.

    Link to comment
    Share on other sites

    • 0

    Might be able to go on as root and override the lua environment so it is not limited but I am not sure it would be a good way forward. No plan to do this myself.

     

    Have been asking for this for years but the necessary access to the global tables is locked out by design for "security" reasons.

     

    Plugins will be the proper way forward as you can implement your own methods and events. I am waiting for this.

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • Thanks all for the replies.  I can stop banging my head against the wall on this one 

    Please login or register to see this image.

    /emoticons/default_icon_biggrin.gif" alt=":-D" />

    Link to comment
    Share on other sites

    • 0

    sbowtell,

     

    Yup, tis v annoying! However, as someone hinted, you could create/update a table in one scene, convert it to json, store it in a 'defined' variable (in variables panel) and then, in another scene, decode the table stored in the variable and read it for your purposes in the second scene. Works fine:)

     

    KR, Dave

    Please login or register to see this image.

    /emoticons/default_icon_biggrin.gif" alt=":-D" />

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • Thanks all, it's a good suggestions to use {} in a Global Variable and I may look into that in more detail.  Is there a limit to the number of variables and their length?  My devices table alone currently has 110 elements (name = 13 characters) and since the minimum I could get away with in my code is just storing the device name and ID that's over 1,750 characters to store in a single Global Variable.

     

    Finding out Fibaro's limitations is difficult as the documentation is very poor (or more to the point, practically non existent).  

     

    I wrote some code to build an email containing multiple lines.  There is a limit on the number of characters HC2 will permit in an email (I'm restricting the size of the email to 80 characters x 20 lines).  Then I discovered that the 'sendEmail' action behaves differently in v4.049 compared with v4.040.  All of this had to be found out by trial and error.  A lot of trial and a lot of error.

     

    Because it doesn't appear possible to perform a HC2 software update remotely (remote access appears to behave differently and many simple options such as deleting a scene simply don't work remotely) I have to wait until I get back to the property (1,500km away) to update it.

    Link to comment
    Share on other sites

    Join the conversation

    You can post now and register later. If you have an account, sign in now to post with your account.

    Guest
    Answer this question...

    ×   Pasted as rich text.   Paste as plain text instead

      Only 75 emoji are allowed.

    ×   Your link has been automatically embedded.   Display as a link instead

    ×   Your previous content has been restored.   Clear editor

    ×   You cannot paste images directly. Upload or insert images from URL.

     Share

    ×
    ×
    • Create New...