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


Question

Posted (edited)

Greeting!

I try to get the scene id from a VD. As I like to have less variables to be entered by users, I try to find a way to identify the scene that is required by VD with a clever way. My idea is to get all scenes and filter them by type 

Please login or register to see this code.

After that, I want to call every scene with args from VD and read the Global Variable sceneId

Please login or register to see this code.

If args are matching, Scene will write on a global variable with a value of the sceneId

Please login or register to see this code.

Suppose this mechanism may work but as it has big blocks of coding, I like to ask if someone has a greater idea or build it and like to share.

 

Thanks!

Edited by FSE

12 answers to this question

Recommended Posts

  • 1
Posted (edited)
On 5/26/2019 at 5:07 PM, FSE said:

How I put a unique key in scene and search for it from a VD? 

Any example please?

 

 

In Scene

Please login or register to see this code.

In VD

Please login or register to see this code.

and then cache the scenes if you do it a lot.

Edited by jgab
  • Thanks 1
  • 0
  • Inquirer
  • Posted

    I tested by myself this scenario....It massive FAILS as all scenes are triggered on same time! If I had a different setup of my scenes to read/filter args ti may solve issue.

    I'm back to zero....any help is more than welcome.

     

    • 0
    Posted

    You put a unique key in the scenes’ lua code and search for that key from the VD (by looping over all scenes and look in the lua property)

    Is that what you want?

     

    • 0
    Posted

    My scenes use common global variable in which they save their unique data and then other scenes and VD's can find exactly scene that is required to do the job :-) 

    It seems simple, but code must also be able to scan this global variable and correct data and clean garbage if user delete scene or change code of the scene to do something else.

    • 0
    Posted
    2 minutes ago, Sankotronic said:

    My scenes use common global variable in which they save their unique data and then other scenes and VD's can find exactly scene that is required to do the job :-) 

    It seems simple, but code must also be able to scan this global variable and correct data and clean garbage if user delete scene or change code of the scene to do something else.

    How do you avoid race condition if the scenes update the same global? You funnel writes through a common scene or some there clever trick?

     

    • 0
    Posted
    1 minute ago, jgab said:

    How do you avoid race condition if the scenes update the same global? You funnel writes through a common scene or some there clever trick?

     

    I haven't implemented any special tricks since it seems that system itself is taking care of shared or multiple access to global variables. I might be wrong, but when I was implementing translations and make mistake in code then I noticed that multiplying writes to global variable still happens but greatly slows down complete system.

     

    So scene code on each run just checks if its static data exists in common global variable and is up to date, if not then writes fresh data otherwise does nothing. There is another code that runs once per day and cleans garbage from this global variables that remains after any scene is deleted. I do same with VD's, only their static data is written to common global variable so once stored then is only accessed to read. Translation is preformed by changing one flag for each VD in a row with slight delay which is same as delay in main loop of the VD. This ensures that main loop will register this flag as changed, set it back to original value and then preform translation of the VD.

     

    Until now with more than 60 VD's and dozen of scenes I have not encountered any problems.

    • 0
    Posted (edited)
    48 minutes ago, Sankotronic said:

    I haven't implemented any special tricks since it seems that system itself is taking care of shared or multiple access to global variables. I might be wrong, but when I was implementing translations and make mistake in code then I noticed that multiplying writes to global variable still happens but greatly slows down complete system.

     

    So scene code on each run just checks if its static data exists in common global variable and is up to date, if not then writes fresh data otherwise does nothing. There is another code that runs once per day and cleans garbage from this global variables that remains after any scene is deleted. I do same with VD's, only their static data is written to common global variable so once stored then is only accessed to read. Translation is preformed by changing one flag for each VD in a row with slight delay which is same as delay in main loop of the VD. This ensures that main loop will register this flag as changed, set it back to original value and then preform translation of the VD.

     

    Until now with more than 60 VD's and dozen of scenes I have not encountered any problems.

     

    Well, if you have code like

    Please login or register to see this code.

    it is  a problem waiting to happen...

    A) scene 1 reads the value

    B) then scene 2 reads the value

    C) scene 1 add its parameter and writes back its updated value,

    D) then scene 2 overwrites the value with its updated value, that don't contain the value that scene 1 added.

     

    It probably doesn't happen very often but when it happens it is usually difficult to find the bug. If scenes starts up simultaneously, like after a restart with several scenes trying to update it, you increase your chance of it happening...

    Edited by jgab
    • 0
  • Inquirer
  • Posted
    5 hours ago, jgab said:

    You put a unique key in the scenes’ lua code and search for that key from the VD (by looping over all scenes and look in the lua property)

    Is that what you want?

     

    How I put a unique key in scene and search for it from a VD? 

    Any example please?

     

    • 0
    Posted
    1 hour ago, jgab said:

    It probably doesn't happen very often but when it happens it is usually difficult to find the bug. If scenes starts up simultaneously, like after a restart with several scenes trying to update it, you increase your chance of it happening...

     

    You are right, that is something that can happen, but then scene which values are for that reason overwritten with another scene will try again to write its data on next check. I did some tests with deleting common global variable and restarting HC2 and result was always that scenes manage to set their data in global variable, at least those that have set %% autostart option. With VD's this problem is even more possible to happen since they all do checks in main loop when main loop is restarted and then at each run.

     

    As I mentioned before, these common global variables are mostly read in 99,9% of the time even at reboot because data that is written there is static, such as settings, role, icon ID etc. and are usually updated when scene or VD are saved after settings are done.

     

     

    • 0
  • Inquirer
  • Posted

    @jgab this is changing a lot of any approach that is done where VD required scenes! Thank you! Amazing!

    I can understand that VD search inside code of every scene to find the predefined string. Right?

    For a strange reason I like to ask if can be done on reverse. Can a scene search a string inside VD??!? ;):):idea:

     

    • 0
  • Inquirer
  • Posted (edited)

    I have a dream to get all VDs and scenes from a unique key inside comments on top of scenes / main loop for VDs.

    The idea is that building a scene that can index these keys, with a short name HomeTable can do all the dirty work for us and solve some issues with users settings (wrong VD id on scenes or reverse).

    I suggest a coding of keys as following:

    VDs      : "vd"-"User id on Fibaro forum (can be found opening profile url, for me is 15225)"-"Short  Name of published VD (for example: Smart_lights)-"Version***"

    Scenes: "scn"-"User id on Fibaro forum (can be found opening profile url, for me is 15225)"-"Short Name of published Scene (for example: AIO_Master_Scene)-"Version***"

    Using text patterns, can be easy to  grep names and build HomeTable, check if a dependency is missing or outdated,etc.

    A more ambitious project is a mechanism to check of new releases and updating user for new version. This may required Fibaro engagement @T.Konopka 

    I know, I'm a dreamer. I hope not to be alone.... :)

     

    Edited by FSE
    • 0
    Posted (edited)
    13 hours ago, FSE said:

    @jgab this is changing a lot of any approach that is done where VD required scenes! Thank you! Amazing!

    I can understand that VD search inside code of every scene to find the predefined string. Right?

    For a strange reason I like to ask if can be done on reverse. Can a scene search a string inside VD??!? ;):):idea:

     

    Yes, put the key in the main loop of the VDs

    Main loop:

    Please login or register to see this code.

    and then in the scene

    Please login or register to see this code.

     

    Edited by jgab
    • Thanks 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...