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

Some missing constructs for VD programming


Question

Posted

I have recently played with VD programming and I encountered a few problems in VD programming that I would be very happy to have addressed by Fibaro team in one of the next releases:

 

1. Please add variables to VD scope, shared across buttons and main loop

It is now quite cumbersome to code VDs that have some (even very basic) state and logic.

There now seem to be only the following options of keeping the state:

- Create a global variable (most obvious, but problematic)

- Keep data in the sliders (integers only, generally in range of 0-100)

- Keep data in physical device parameters (if you happen to have one that allows it!)

- Use external web service API

Each of these has serious problems, probably quite obvious for everyone.

 

How about adding a possibility to define within the VD a set of parameters, accessed - once defined - in the same way as the "IPAddress" and "TCPPort" are accessed, namely:

Please login or register to see this code.

I do not think it would be a big problem for the Firabo team and would be a great help for the crowd developers.

 

2. Please add option to define a range for VD sliders.

The sliders now always have range of 0-100.

But this is not very useful for cases like I had with sprinkler control, where I wanted to give user a possibility to delay the sprinkler in range of 0-24 hours.

Please add a possibility to define min, max and step values for the slider.

 

3. Please add an API function (or metadata) to CREATE a global variable, a "constructor" code block

I know this may be tricky security-wise, but in order to automatically create global variables that are needed by the VDs or scenes, the developers are forced to do even more dangerous tricks (as using webservice API with open superadmin usernames and passwords).

Therefore I ask - please add a method (e.g. fibaro:createGlobalVariable()) to allow developers creating global variables lege artis.

It would be even better (especially security-wise), if you added (in VD and LUA scene) a special "constructor" block of code which is executed only once, during the first run of a scene or installation of a VD, where such methods could be used safely (because only super admin has rights to install/define a VD and scene).

 

Thanks in advance!

 

  • Thanks 1

6 answers to this question

Recommended Posts

  • 0
Posted

All of the above can be achieved with several lines of the code, but I agree it would be good to have more elegant way to have it.

  • 0
  • Inquirer
  • Posted

    @gucio1234 Few lines of code, really? I wonder how. Would you mind sharing links to some examples?

    • 0
    Posted

    @qnstie ,

     

    Here are examples and comments :

     

    1.

    As for now we cannot share variables between main loop of the VD and the buttons/sliders.

    Yes, I must admit I was too fast confirming that all of your concerns may be resolved, but the following are examples of the code for your other concerns.

     

     

    2.

    This code in the definition of the 'Slider2' interchanges standard slider value 0-100  to  0-1 with steps 0.1 .

    The result is placed in the local variable 'suwak'

     

    Please login or register to see this code.

     

     

     

    3.

    Here is the code to create global variables from the main loop of the VD if they are not already present.

    This is part of the code is taken without any changes from my VD for monitoring APC UPS. However the idea is originally done by @Sankotronic. I used it with his permission.

    Please login or register to see this code.

     

    • 0
  • Inquirer
  • Posted

    @gucio1234 Thanks for the examples, but:

     

    Ad. 2: The idea was to have the min, max and the current values DISPLAYED in the UI, so that the user would not guess what value he/she selected.

    For instance - if the slider was about setting the hours of delay, he/she would be able to move the finger until "12" is shown - not to guess that "12" is probably somewhere close to the middle of the slider...

    I admit I did not write that clear enough (it seemed obvious). Your solution is of course correct to set the value, but does not provide any feedback to the user who moves the slider.

     

    Ad. 3: The whole idea was NOT to use the webservice (the line with HC2 = Net.FHttp(...) ) because it is a dangerous workaround, which requires you to put in the code (in open text) the credentials of the superuser! Gee, how about the security of that solution (not mentioning how ugly and long the code is).

    Of course, for now it is the only way to achieve the auto-creation of the variables (and I use the same code in my VDs now, sadly).

     

    Thanks anyway and take care.

    • 0
    Posted

    Hi @qnstie,

     

    1) There are some restrictions on HC regarding shared code functions and variables. I use global variable tables to share values between code. LUA likes tables and have good support for them. This way only one global variable is enough since table can hold many values nicely organised and easily accessed by code.

     

    2) I use label and buttons to change values that can't be nicely done with sliders. It takes some more space on VD but at least it is very clear what it does.

     

    3) Adding global variables from code is not that complicated and it does not require to send any credentials anywhere. You can check example code bellow that adds one global table and one predefined variable to global variable panel, code is used in main loop of VD:

    Please login or register to see this code.

    The beauty of above code is that can also be used in scenes.

     

    Enjoy coding! :-) 

    Hi @qnstie,

     

    Just to mention that I'm not using numerical global variables (upper part of global variable panel) but only table and predefined for which I found nice way to be translated to different languages depending on language selection on HC2.

    • 0
  • Inquirer
  • Posted

    @Sankotronic Absolutely awesome, thank you very much!

    I had no idea about the existence of the "api" object, I believe that there are not too many people who are aware about it (judging at the code shared on this forum or on Github). This alone resolves 90% of the serious problems with HC2 coding I encountered so far.

    BTW, this does not seem to be documented anywhere. Do you know if it is a feature that we can rely on (not something that will be removed at the next firmware update)?

     

    Thanks also for the hint with the labels to indicate the current values. While it is a space-consuming workaround, it makes a lot of sense.

    Also your solution to keep the variables in a table is very clever, while I would very much preferred to have "class" variables in a VD or scene (it would be much cleaner), keeping all the variables of a given VD or scene in one global (well named) variable is way cleaner than polluting the variables with multiple individual strings or numericals.

     

    Thank you again, you made my day.

     

    @Fibaro I still think that my original requests are viable but thanks to @Sankotronic they are all much less important to me now. Is there any documentation mentioning the "api" object (and maybe more "secret" goodies I am not aware about)? I cannot find it anywhere, not even on developer.fibaro.com (all the examples and descriptions there mention the actual webservice HTTP calls). Thanks in advance.

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