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

[SOLVED]Fibaro HC2 LUA Boolean logic


jodohl

Question

A bit embarrassed to ask for advice on this, but I seem not to be able to understand the structure of simple IF-tests in HC2 Scenes, regardless whether these are created in the Block editor or LUA.

 

I need an IF test with the following structure:

 

....IF ((a AND b) OR (a AND c)) THEN....

 

Due to the lack of parantheses, I am not able to understand the logical structure of the resulting statement, which in my mind seems confusing

 

...IF ((a AND b OR c AND b),

 

I use this code to save electricity on my snow melting mats by twice a day testing whether weather conditions has recently set the variable SNOW to YES in which case mats are turned on for two hours (morning and afternoon) and the variable set back to NO. Seems now my mats turn on twice a day (at times "a" and "c") regardless of the the variable ("b").

 

Are there any "paranthesis" conventions in LUA I have not understood ?

 

Link to comment
Share on other sites

5 answers to this question

Recommended Posts

  • 0

It works with or without parenthesis.

Looks like in your case A and C always match your condition, so condition with variable B will be ignored (according to your IF statement)

If you need more help, please post your code and we can go through to see what is the problem.

 

Link to comment
Share on other sites

  • 0

@jodohl I was not absolutely sure, so I did test it (easy to test by yourself) and @cag014 was right they work both perfect like you need it.

 

Please login or register to see this code.

 

Link to comment
Share on other sites

  • 0
  • Inquirer
  • Thanks a lot for demonstrating the logic!

    My take from your test is that when parentheses are not explicitely written, LUA code implicitly assumes parentheses before and after "and", but not around "or".

    Important to understand, since:

     

    "if a and b or c then" will be executed as

    "if (a and b) or c then" and not as

    "if a and (b or c) then"

     

    Which also shows the limitation of the Block Scene editor which to my knowledge has no parentheses functionality. The last statement will in the Block Editor have to be written as 

    "if a and b or a and c then", equivalent to

    "if (a and b) or (a and c) then" which is equivalent to

    "if a and (b or c) then".

     

    Maybe this is well known stuff to all up to date programmers, but not an oldtimer who learnt his Boolean algebra in the seventies :-)

    Thanks for teaching me! This motivated me to pick up some more LUA coding.

    Link to comment
    Share on other sites

    • 0

    I found some more on this :)

     

    Quote

    Operator precedence in Lua follows the table below, from the higher to the lower priority:

    Please login or register to see this code.

    All binary operators are left associative, except for `^´ (exponentiation) and `..´ (concatenation), which are right associative. Therefore, the following expressions on the left are equivalent to those on the right:

    Please login or register to see this code.

    When in doubt, always use explicit parentheses. It is easier than looking up in the manual and probably you will have the same doubt when you read the code again.

    Quelle: 

    Please login or register to see this link.

    Edited by PreHack
    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.

    ×
    ×
    • Create New...