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] Have 2 block scenes that I try to combine into one LUA scene, but get the error "unexpected symbol"


Teza

Question

I have two block scenes that works, and when I change these from block to LUA, I get the following code:

 

Scene 1: Set variable value if movement

 

Please login or register to see this code.

 

Scene 2: Set variable value if no movement

 

Please login or register to see this code.

 

Trying to combine these into one LUA scene, I wrote the following code:

 

Please login or register to see this code.

 

Question #1:

 

This creates the following error:  [DEBUG] 12:23:15: [1;31m2018-05-27 12:23:15.977459 [ fatal] Unknown exception: /opt/fibaro/scenes/104.lua:49: unexpected symbol near ')'

What did I miss?

 

 

Code line #46 to #50 is;

 

Please login or register to see this code.

 

 

Question #2:

 

What is the purpose the number in the following codeline, in scene #2 above?

 

Please login or register to see this code.

 

Question #3:

 

What does this codeline from scene #1 and #2 do?

Please login or register to see this code.

 

Edited by Teza
Solved
Link to comment
Share on other sites

12 answers to this question

Recommended Posts

  • 1

I'm sorry, I don't know what to recommend.

 

What might interest you... Fibaro HC builds on Lua. They add stuff. So "setTimout" is not Lua, you will not find that in a Lua book.

 

And pure Lua does not work on a HC because Fibaro also has removed features from Lua (like "require" to load libraries) - to restrict it and make it more safe.

 

If you want to run the examples from any Lua book, but not Fibaro code, you can download this (free) software and run it on your PC

 

Please login or register to see this link.

 

So I'd try the Lua book + Zerobrane to toy with the language on your PC... And in the meantime also continue what you are doing today: copy/past, see how block scenes work and use the forum.

 

Maybe someone else can recommend a good book.

Link to comment
Share on other sites

  • 0
  • Inquirer
  • On my second attempt I do not get the error, but I am still not able to get the variable set to "0".

     

    There is something I am missing.

     

    Please login or register to see this code.

     

    Link to comment
    Share on other sites

    • 0

    With this kind of problem I always use the fibaro:debug() function, just monitor what is happening. I'm definitely not a code-guru, so I always use a lot of these debug functions. ;D

     

    I have had some issues in the past, maybe you could check out if it is local variable problem. When local = <variable> written in a loop or function, the variable in the mainloop will not be effected, only in the loop/function.

     

    I suggest you would add some fibaro:debug() functions, just to understand where it's coming from.

    Link to comment
    Share on other sites

    • 0

    @Lambik good suggestion, but I bet it is the "setTimout" function... @Teza If you are new to programming on a HC, I would avoid setTimeout, use sleep() instead. I understand you converted block scenes, so calling that function was not your intention, it's part of the scene you converted. That won't work.

     

    If you want to understand what it does, and why your code does not work, can I recommend an old topic of mine?

     

     

    If you have programming experience on other platforms... think of setTimeout as a function you call to set a "callback" to fire after your scene ends (your main code uses "while true" so never ends, and that callback never runs) and after a certain time in ms has elapsed.

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • What I have today is a lot of block scenes, and I have problems finding the scene i need to edit.

    Sometime I have two scenes that try to control the same thing, as I have lost the overview.

     

    I want to get rid of this mountain of block scenes and replace a bunch of scenes with a single LUA scene.

     

    By using sleep, you will stop the whole scene, and that won't work as I intend to have all the rooms inside this scene.

    By using settimeout, I understand that the rest of the code will still run.

     

    Problem two is that I will have to repeat the whole code for every room, and I believe that is bad programming, and makes the code look messy.

    What I really would like is to have the timeout code once in the code, and call it when needed from the code for every room.

     

    But hey, all I have is some idea on how programming can work, I don't have any skills at all.

    Therefore I convert block scenes and try to understand how I can edit the code to fit my needs.

    Painfully time consuming, and frustrating :)

     

    The only thing that don't work in the code now is the settimeout, but I got rid of all the error messages it created earlier.

    I am not sure on how to use the debug on the settimeout.

     

    @Lambik 

    Quote

    When local = <variable> written in a loop or function, the variable in the mainloop will not be effected, only in the loop/function.

     

    Is that what I have done wrong here?

     

    @petergebruers

     

    Nice thread, I will need to use some time to understand it, but is seems like a good collection of information about settimeout.

     

    Link to comment
    Share on other sites

    • 0
    30 minutes ago, Teza said:

    Nice thread, I will need to use some time to understand it, but is seems like a good collection of information about settimeout.

    Thanks.

     

    31 minutes ago, Teza said:

    By using settimeout, I understand that the rest of the code will still run.

    No, that is not the complete picture. Your main code must end before the functions scheduled by setTimeout can run. Please try the examples and some other code in my topic. Once you grasp it, it is very rewarding. You can run things in parallel with setTimout... If you follow the links in that topic you will find some code.

     

    The functions scheduled can access only their own data (variables).

     

    So there is a lot of things to study before you see how all this fits together.

     

    Maybe that is why "sleep" is more popular.

     

    This is how it is done without setTimeout... pseudocode:

     

    while true do

      ... check stuff

      ... send commands

       sleep(1000)

    end

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • A bit oftopic question.

     

    I have a book called "C programming, Absolute beginner's guide".

    I bought it some time ago, but have not used it yet.

     

    Will using this book, make me able to create LUA code?

    Link to comment
    Share on other sites

    • 0
    10 minutes ago, Teza said:

    A bit oftopic question.

     

    I have a book called "C programming, Absolute beginner's guide".

    I would not read that, unless you plan on doing mcu or iot development. The language is too different. Lua is quite modern.

     

    The Lua book first edition is free and online:

    Please login or register to see this link.

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • Have looked at it earlier, but that book is aimed at a beginner that already know how to code.

    I will need something more basic.

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • You just helped me a lot by pointing out the direction for learning the needed LUA, for HC.

    Please login or register to see this code.

    Please login or register to see this code.

    Please login or register to see this code.

     

    So the best way for learning the LUA needed for HC, will be to start with simple scripts, understanding them, testing them, changing  them to fit your need and use that learning to learn more advanced scripts.

    Not by spending hours reading a book. Books might be good for reference later on tho?

    Link to comment
    Share on other sites

    • 0
    12 minutes ago, Teza said:

    start with simple scripts, understanding them, testing them, changing  them to fit your need

    Yes. You got an unlucky start, because you stumbled on a script that has "setTimout" in it. There is another one based on the same concept: net.HTTPClient(). But everything else is "normal".

     

     

    12 minutes ago, Teza said:

    Not by spending hours reading a book.

    Reading the book won't make your light turn on, so that is kind of frustrating, I understand that.

     

    There are few things though you might not grasp without reading and are quite profound. As an example, lot's of web interfaces use JSON. It is a nested data structure, and it is converted to "Lua tables". Lua tables are very important. Don't believe me? When you do fibaro:setGlobal you actually call a function stored in a field called "setGlobal" that is part of a variable called "fibaro" that stores a table! Whenever you see a dot or a colon or [] you are dealing with tables.

     

    A third possible source of knowledge is youtube. For example, this channel:

     

     

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • Looks like I solved this one, Thanks!

     

    Please login or register to see this code.

     

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