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


  • 0

Access variables in multiple instanc


Question

Posted

Hi

 

I would like to start a scene with a button (in my example S1 on an FGS-223), which then runs through a state event machine and takes about 60 seconds. While the scene is running, I still want to be able to react to inputs from the button. I want to do this directly in the scene without using an additional global variable. I cannot interrogate the push button cyclically (polling) because the status of the push button cannot be interrogated by the system, as far as I know.

 

Please login or register to see this code.

In the code example above I did not set the attribute "local" for the "counter"-variable and assumed that it is available for further instances. I now perform a second keystroke to start a second instance and set the variable "counter" back to 0. But this is not the case. The second instance is aborted with fibaro:abort(), but the "counter" variable of the first instance is not reset to 0. Does anyone know of a possibility or an example to be able to react to short keystrokes during a running scene?

8 answers to this question

Recommended Posts

  • 0
Posted

hi!

 

1) global vars for LUA in Fibaro disabled by Fibaro devs

Quote

_G=nil

2) Basic pieces of knowledge about lua 

Please login or register to see this link.

3) 

11 hours ago, Schopf16 said:

scene without using an additional global variable

you can't

... but probably yes :

create VD and set label value as value what you want to save globally :) 

------

 

PS  please update your profile. I don't feel comfortable associating with anonymous :D 

 

  • 0
  • Inquirer
  • Posted

    Thanks for the replay

     

    Quote

    global vars for LUA in Fibaro disabled by Fibaro devs

    I was already afraid Fibaro might have a hand in this.

     

    Quote

    create VD and set label value as value what you want to save globally

    Thanks for the tip, I'll try this one time. Alternatively I found the mail from @jgab. One of his examples shows how global variables can be created at runtime. With this possibility I can still misuse the global variables as a messagebox system. I just have to check how I can delete them.

     

    Quote

    PS  please update your profile

    Done :D

    • 0
    Posted
    2 minutes ago, Schopf16 said:

    With this possibility I can still misuse the global variables as a messagebox system.

     

    Mmmm pass to msgbox system as args: 

     

     

    • 0
    Posted
    1 hour ago, Schopf16 said:

    Thanks for the replay

     

    I was already afraid Fibaro might have a hand in this.

     

    Thanks for the tip, I'll try this one time. Alternatively I found the mail from @jgab. One of his examples shows how global variables can be created at runtime. With this possibility I can still misuse the global variables as a messagebox system. I just have to check how I can delete them.

     

    Done :D

    Using fibaro globals as message boxes are in general tricky due to possible race conditions - producer scene(s) overwriting values before consumer scene(s) reading them.

    I'm using a homemade Lua framework in my own scenes that "posts" all triggers back to a main scene (using such message box... with care), so that I can deal with multiple triggers in the same scene instance. That means that local lua variables survives between triggers and I can do tricks like I guess that you are aiming for above. My model is also an event/message based model that makes communication within and between scenes quite convenient. Trying to code a highly asynchronous environment like sensors in a home automation system is really difficult with sleeps and global variables to force some kind of synchronous behavior in the code....

    Better then to embrace a event/message based programming style. I tend to use very few fibaro globals. Ex. I have a scene that keeps the presence state of the family members (or at least makes educated guesses, looking at iOS presence, calendar, sensors etc) and post events to other scenes that have registered interests in presence events. I don't store that value in a global fibaro Presence variable. If the box needs to be restarted the scene is autonomous enough to quickly figuring out where everyone are again...

    These days I'm more thinking of my scenes as stateful processes (Erlang?) that communicates with events/messages, and sometimes they get messages from external devices... I find that it makes it much easier to structure the system for me...

    The code and examples are here; 

    Please login or register to see this link.

    • 0
    Posted

    Thread hijack:

     

    53 minutes ago, jgab said:

    stateful processes (Erlang?)

    Yes... And if you like "[processes] that communicates with events/messages" then you probably also enjoy programming in Google's "Go". I toy with both languages and often wondered how they could support a (better) home automation engine. Not that you really need them, but erlang's "hot code swap" is nice to have an Go is close enough to c/c++/c#/java so that many people can appreciate it... While Erlang, yeah, you need a different mindset. Once you''ve got it, you see why they have a faithful group of users. Hot code swap still is not a panacea. How do you handle progressive updates? Database upgrades? I fully agree with what you have been posting lately: handling "time" and "events" could be improved, the current model of "a trigger starts a scene" plus lack of time awereness (schedulers, timers, time-outs) could be handled... more elegantly?

    • 0
    Posted (edited)

    So, back to the original problem - detecting several keypresses from a button in a single scene instance (in this example using the fibaro keyfob).

    This can be done by some code funnelling triggers into a single instance. It may not adhere to the original requirement because it uses one (autogenerated) fibaro global as a message box to achieve this trick.

    The difference for the programmer is that instead of calling fibaro:getSourceTrigger in every new scene instance being started and needing to try and remember what has happened before by using fibaro globals, the same main() function will be repeatedly called with new incoming sourceTriggers and lua locals keep their values.

    A bit involved but the code turns the "spawn multiple scene instances" approach that fibaro uses to a more classical event model found in e.g. window manager systems... and it's pretty convenient to write all kind of scenes with this model(!)

    (This kind of scene need "max running instances" to be set to at least 4 - one main instance + number of simultaneous key presses)

    Please login or register to see this code.

    The added benefit is that this scene can be run off-line on a PC/Mac if 

    Please login or register to see this link.

     is included (and thus easily debugged).

    Ex. to test the logic of the keypresses, we set some timers calling main() in the future with the appropriate trigger events (that can also be done on the HC2).

    The above scene is the base for the more elaborate

    Please login or register to see this link.

    framework

    Edited by jgab
    • Like 2
    • 0
  • Inquirer
  • Posted

    That's a very interesting approach. Since my problem cannot be solved without a global variable or virtual device, I will give this example code a chance ;)

     

    Quote

    The added benefit is that this scene can be run off-line on a PC/Mac if 

    Please login or register to see this link.

     is included (and thus easily debugged).

    Debugging a LUA script on the HC2 is indeed anything else than a joy ride. I was allready searching a IDE for debugging on the computer instead of the HC2. Do you have favorits for it?

    I'm using pyCharm for Python and saw there is a plugin available for LUA.

    • 0
    Posted

    I use Zerobrane and do all my Lua/HC2 development with it. You need to add json and fibaro api to get it really working. A tutorial is here 

     

    • Like 1

    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.

    ×
    ×
    • Create New...