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

Basic Code Clips for LUA starters


robmac

Question

To help those that are not familiar with LUA a starter library of clips that work in Fibaro.

Please add any you find useful.

Link to comment
Share on other sites

  • Answers 151
  • Created
  • Last Reply

Top Posters For This Question

Top Posters For This Question

Posted Images

Recommended Posts

  • 0
  • Inquirer
  • The hour and minutes are strings convert to numbers e.g. 7 < tonumber(hour) to compare to other numbers

    Please login or register to see this code.

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • As the Sunset and Sunrise is a string it is hard to compare to the current time to check if it is day or night.

    This is a workaround:

    Create a variable nightTime

    Create a scene with

    Please login or register to see this code.

    NOTE the variable is only set as the time passes sunset and sunrise values so it must start at the correct value.

    Use the variable in other scenes if nightime == "1" then turn my light on when movement... etc

    You can adjust night by adding minutes to sunset or removing from sunrise.

    Link to comment
    Share on other sites

    • 0

    This is very useful, Rob. Thanks for posting.

    I'm just getting started with LUA. I used to do programming (long time ago though) so understand that part. I'm searching for a simple list of functions available in fibaro (eg figaro:debug(). Would you have anything like that?

    In particular, I am now looking for the function to write logging data to the Event log. Any clue what that is?

    Thanks!

    Ronald

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • For the built in this is the only doc I am aware of. There is a post saying English version is WIP:

    Please login or register to see this link.

    for other things I am learning by trial and error: For basic actions and properties of devices try by building a block script and converting.

    For some devices I could not see anything I wanted, so I went looking for hints: look in new_properties and new_actions tables. Some of these are exposed not sure how many or how many we can rely to stay. They are undocumented features. There are some interesting ones.

    For what you are asking the only way I can think of is a direct database insert but I would not do that myself. Could interfere with the operation of the system.

    Link to comment
    Share on other sites

    • 0

    -25 and +25 is how many minutes after or before sunrise/sunset

    Please login or register to see this code.

    Link to comment
    Share on other sites

    • 0
    Guest jrkalf

    Hi Rob and Jompa,

    I see you're both having nice scripts finding a way to make a proper sunrise and sunset timer. How do you guys implement this?

    Do I need to make a variable or predefined variable in the "variables panel" for this to work?

    So I need to declare a %%global at the start of the script to get this working?

    I'm fiddling with it, to use this nightTime or Night variable in my frontdoor sensor script to only trigger the hallway lighting during night time

    Please login or register to see this image.

    /emoticons/default_icon_mrgreen.gif" alt=":mrgreen:" />

    I'll won't clutter this thread by posting my code snippet in here, but having a general feel on how to work with the variables would be nice to know and it would make it easier to implement this.

    Link to comment
    Share on other sites

    • 0

    Hi

    yes create a variable for this in variable panel. Here is my scene code

    Please login or register to see this code.

    Link to comment
    Share on other sites

    • 0
    Guest jrkalf
    Hi

    yes create a variable for this in variable panel. Here is my scene code

    Jompa, a standard variable or a predefined variable with a 0 and 1 as value options?

    Reason for asking is I've created a standard variable named Night (yes, I've adjusted NightTime to Night) and had it set at value 0.

    I might interpret the code wrong, but will it only switch to Night when the actual time passes, or is it doing this at "any time" during sunset hour?

    Link to comment
    Share on other sites

    • 0

    Small code for testing if there are any DEAD devices and inform you by email. You can add a code to run it i.ex. every hour.

    --[[

    %% properties

    %% globals

    --]]

    local i = 1

    local TotalDevices = 156 + 1

    while i < TotalDevices do

    local status = fibaro:getValue(i, 'dead');

    if status == "1" then

    fibaro:debug("Device "..i.." DEAD");

    fibaro:call(2, "sendEmail", "DEAD "..i);

    else end

    i = i + 1

    end

    Link to comment
    Share on other sites

    • 0

    Folks, since my update to 3.488, the Sunrise/sunset values seem off. I got home this afternoon at 5pm and all my lights were on. Right now, my system thinks Sunrise is at 7:48 (should be 7:12) and sunset is at 15:42 (should be 18:29).

    I did check my GPS location - it is correctly set to Almere, Netherlands.

    Anyone else experiencing this?

    Please login or register to see this attachment.

    Link to comment
    Share on other sites

    • 0

    ANyone knows how to get the device name in LUA?

    I want to use "real" names in notifications.

    Link to comment
    Share on other sites

    • 0
    Guest Lode

    Here a small sleep function

    Please login or register to see this code.

    Now everywhere in your script you just use

    sleep(value)

    Link to comment
    Share on other sites

    • 0
    Guest deraaij
    ANyone knows how to get the device name in LUA?

    I want to use "real" names in notifications.

    I think you alreday know but for others:

    Please login or register to see this code.

    Here a small sleep function

    Please login or register to see this code.

    Now everywhere in your script you just use

    sleep(value)

    You can also use (sleep for 10 seconds):

    Please login or register to see this code.

    Link to comment
    Share on other sites

    • 0

    Hi all. I gave it a shot into the Lua world today. Intention is to learn on the fly a bit and this will hopefully help me to understand the wiki's around all this later. For now I'm struggling….

    This is wat I did. I reviewed the code of an existing (converted) scene and stole some code from mr Jompa68 (Thanks!).. The new code works fine (as it seems) and should allow me to have a universal Sunset Scene that can be offset and listens to day of the week.

    Please login or register to see this code.

    Now..my questions are:

    1. When you create a new scene you have the option to select “Start when Homecenter 2 Starts”. Once past this page, you cannot change this setting anymore. Is this correct and if so why is this?

    2. I assume this setting tells the system to start this script as part of the power up. Correct?

    3. To ‘unhide’ this setting my thinking is to use fibaro:getSourceTrigger() and test for “autostart”. But what will this do then, after all, the scene may not activated after a powerup etc ??

    4. The ; at the end of statements, when and why are they needed?

    Grateful for any answers!!!

    Regards,

    Hans

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • 1. When you create a new scene you have the option to select “Start when Homecenter 2 Starts”. Once past this page, you cannot change this setting anymore. Is this correct and if so why is this?

    2. I assume this setting tells the system to start this script as part of the power up. Correct?

    3. To ‘unhide’ this setting my thinking is to use fibaro:getSourceTrigger() and test for “autostart”. But what will this do then, after all, the scene may not activated after a powerup etc ??

    4. The ; at the end of statements, when and why are they needed?

    1) if you are in LUA, you can control this by having %% autostart or not. If you do not want the scene to autostart remove that from the code. Checkbox is not necessary.

    2) yes

    3) not sure what your question is but I only use this test if I want the scene to behave differently depending on how the seen is activated. It is not compulsory to use it.

    4) optional- use it if you like it and think it is more readable

    Link to comment
    Share on other sites

    • 0

    Thanks robmac. Pretty clear now. One final question: the LUA code typically starts with lines such as:

    Please login or register to see this code.

    All these lines a 'greyed-out' as they are between --[[ and ]]-- Are these statements still active then?

    Regards,

    Hans

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