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

Use global variables with Device ID numbers


Question

Posted

Did anybody else use global variables to define all their device ID's to variables?

i.e. I use the global variable ID_FrontDoor = 125, and in all the LUA code I set a local variable to match the value like this:

local FrontDoorID = tonumber(fibaro:getGlobalValue("ID_FrontDoor"))

This way when any device ID changes because we need to re-include it for whatever reason we only have to change this global variable and all LUA should work as it should.

Did anyone already do this for all their LUA coding?

I see lot's of people needing to re-include when testing the latest Beta's and them all having to change their LUA coding because of the new ID's.

Are there any pitfalls in this approach?

I'd like to hear your thoughts about this.

14 answers to this question

Recommended Posts

  • 0
Posted

hopefully with the advent of soft config we should see less and less new id's! But that is a great idea you have for the short term...

Please login or register to see this image.

/emoticons/default_smile.png" alt=":)" srcset="https://forum.fibaro.com/uploads/emoticons/[email protected] 2x" width="20" height="20" />

  • 0
  • Inquirer
  • Posted

    It's a whole lot of work to change all these LUA coding, but then again you only need to do it once and if you follow the same rules in all LUA codes this could really make things easier.
    I've excluded several door/window sensors by accident when changing batteries.

    Also anybody starting with LUA coding this seems to be a great approach, and makes you very flexible in changing things.

    One pitfall would be one wrong ID and every LUA that uses this ID would stop working.
    But once setup this could be a good base for LUA programming.

    [ Added: 2014-12-21, 16:06 ]
    Took almost a day to do this but it's done and I was able to do it for 95% of my coding.
    Devices that trigger the scene still need to be added under %% properties but you only have to edit the top of the scene's so it very easy to search for those changes.
    All other devices don't need to be added to the top so you only have to change the global Variable.

    And did a lot of coding clean up, the advantage of going through all this coding anyway.
    Now a week test period and hope everything works as it should.
    Everything seems to run as it should but you never know.

    • 0
    Posted

    Good SOA.

    Please login or register to see this image.

    /emoticons/default_wink.png" alt=";)" srcset="https://forum.fibaro.com/uploads/emoticons/[email protected] 2x" width="20" height="20" />

    • 0
    Posted

    I noticed someone else mentioning this and took some time yesterday to implement it.

    It didn't take long since I don't have a lot of scenes running yet.

    However, I didn't like the need to still have to include the properties part in several scenes.

     

    For this reason, I went ahead and created a scene that includes a "mapping" (nested lua tables) that will give me a mapping of "tigger device" -> "action".

    This way, I ended up with one scene that contains all devices in the properties and a small configuration block (device 66 -> start scene 5, of course with global variables).

    If I end up with different ID, I only have to touch this one scene now.

     

    I'm pretty sure a few of you already came up with this themselves. I just wanted to put it out here for the future...

    • 0
  • Inquirer
  • Posted

    I noticed someone else mentioning this and took some time yesterday to implement it.

    It didn't take long since I don't have a lot of scenes running yet.

    However, I didn't like the need to still have to include the properties part in several scenes.

     

    For this reason, I went ahead and created a scene that includes a "mapping" (nested lua tables) that will give me a mapping of "tigger device" -> "action".

    This way, I ended up with one scene that contains all devices in the properties and a small configuration block (device 66 -> start scene 5, of course with global variables).

    If I end up with different ID, I only have to touch this one scene now.

     

    I'm pretty sure a few of you already came up with this themselves. I just wanted to put it out here for the future...

     

    Sorry for my reply after almost a year, should've put a reply e-mail on this topic.

    Can you show what you actually did to accomplish this?

    It's a very good idea but from the way you explain it it's not totally clear what you suggest to do, and for sake of being complete it would be great to see the code.

    Thanks.

    • 0
    Posted

    I'm using json table for all my device IDS and some other stuff (like timeouts, VD buttons and etc.)

    • 0
  • Inquirer
  • Posted

    I'm using json table for all my device IDS and some other stuff (like timeouts, VD buttons and etc.)

     

    Can you show us some examples of that?

    Thank you!

    • 0
    Posted

    First of all you need to create a scene to fill in the data:

    The scene must to use autostart, it should be executed when the HC2 starts (after reboot or upgrades)

    Please login or register to see this code.

    Here is an example how to use the Json table in side scenes.

    Please login or register to see this code.

    Please remember when your device ID has changed and you have updated the Json table you still need to update scene's header

    %% properties
    170 value
    213 value
    235 value
    412 value

     

    According to FIbaro you cannot use variable under %%properties.

    In any case that you need to use the table please include in code:

    local jT = json.decode(fibaro:getGlobalValue("jTable"))

    Works in VD main loops and buttons as well.

    Let me know if you need more info.

    • 0
    Posted

    Thank you cag014! I implemented your idea to my HC2 and it is perfect! It came handy for experimenting with Denon HEOS plugin which was giving me a lot of headache. Also I had to remove and add some devices during developing of my system and this solution of yours saved me a lot of time!

    • 0
  • Inquirer
  • Posted

    Thanks for explaining that cag014 but I don't really see the advantage over my global Variable system.
    I actually think it's harder to see and maintain.

    But each it's own ofcourse.

    • 0
    Posted

    But the main problem is still %% properties

    you have to change that

    • 0
    Posted

    Thanks for explaining that cag014 but I don't really see the advantage over my global Variable system.

    I actually think it's harder to see and maintain.

    But each it's own ofcourse.

     

    Personally I have rearranged the table structure as follow:

     

    here is the list of the rooms

    Please login or register to see this code.

    Now, in case I need to check motion, temperature or any other common sensor/action I can do it in a loop with few short lines only:

    Please login or register to see this code.

    In your case you need to get line by line global value and to use it or you need to create a local table... (for every scene or VD)

    Another advantage that you can use same variable names for different devices, just the room is different.

    To make it clear, as you can see every room has same variable names like "motion, roomTemp, lumens and etc. 

    In addition I have found a big advantage during my last issue with restoring backup from the stick. It very easy to create text file with the scene as your private backup and to recreate the scene in seconds... in your case you will need to restore very long list of variables....

    But at the end you probably right, both ways are OK

    • 0
    Posted

    Thanks guys,

     

    this gives me an idea.

     

    At startup, I can get the room and device list from fibaro and make up the correct list automatically (ie rooms->devicelist).    At the moment I have unique names for devices, I just have to call each motionsensor "motion" etc.  Then all lua code will be self healing.

    • 0
    Posted

    @RohitNz, have a look at this post, you'll find a scene/script to automatically generate the table for you.

    Rene.

     

     

    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...