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

Luca Code Variable


Swordpirat
 Share

Question

Hi all,

i need for a counting system to increase or decrease a number store in a variable but...

debugger teel me:

[ERROR] 21:22:08: line 16: attempt to perform arithmetic on global 'ripetizione' (a nil value)

example code for this error:

....

then

fibaro:debug("luce gia accesa incremento vaiabile di 1" );

ripetizione=ripetizione + 1 <-------------------------------------this

end

ho can i manage (sum and difference) global variable?

Link to comment
Share on other sites

16 answers to this question

Recommended Posts

  • 0
  • Inquirer
  • i don't know well lua, but there isn't possiiblity to make a classic loop with variable controller by another scene?

    i want to make a lua script like a (sorry for my english) crepuscolar system with auto incease timer...i hate walk on the stairs and light end countdown....

    ..........................

    scene 1.....

    under 100 lux and movement detected and variable start=0 (script not run and light non powered on)

    launch scene 2

    else if

    start=1 lux under 100 and movement detected then

    ripetizione=ripetizione+1 (switch yet powered on)

    ...........................

    scene 2:

    repeat

    set start =1

    switch on light

    wait 60 sec

    ripetizione=ripetizione-1

    until ripetizione < 0

    set start = 0

    set ripetizione = 1

    switch off light

    ............................

    sorry for my bad english.....

    Link to comment
    Share on other sites

    • 0

    I think the only way on HC2 would be to use a variable defined in the variable panel. These are strings but you can always cast tonumber.

    The LUA scenes on HC2 are sandboxed and do not give you access to many features including those you would need to do this in a cleaner way.

    I think you can do what you want in one scene though. Just check what triggered the scene to run and increase or decrease the counter.

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • I think the only way on HC2 would be to use a variable defined in the variable panel. These are strings but you can always cast tonumber.

    The LUA scenes on HC2 are sandboxed and do not give you access to many features including those you would need to do this in a cleaner way.

    I think you can do what you want in one scene though. Just check what triggered the scene to run and increase or decrease the counter.

    only in 1 scene?

    mmm...it's possible?

    Link to comment
    Share on other sites

    • 0

    yep

    check the instance count

    if first instance update variable and start loop with sleep and decrements variable each minute

    Please login or register to see this code.

    if second instance add to variable and kill the instance without loop

    Please login or register to see this code.

    [/code]

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • ok but for increase and decrease variable i must use function .... no?

    Link to comment
    Share on other sites

    • 0

    no you just need to initialize the variable to a value. As you will get the value from the function call to get a variable value

    You will be running a couple of threads of code so you may get odd behavior in some rare instances but that would be true with two scenes also.

    Please login or register to see this code.

    you can do that in a less verbose way in LUA but that is easier to read

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • debugger tell me ...

    [ERROR] 11:18:21: line 25: attempt to perform arithmetic on local 'ripetizionetemp' (a nil value)

    ...

    scene programming fibaro is not Like LUA it's a surrogate....

    Link to comment
    Share on other sites

    • 0

    you must have a variable ripetizione in the variable panel or you will be trying to do arithmetic on nil and get that error

    go to panels variable panel on HC2

    Hit add

    type ripetizione for the name and the code will work

    A neater way to write the lines is

    Please login or register to see this code.

    or

    Please login or register to see this code.

    and yes it is a sandbox so access to a lot of important features is blocked.

    I don't understand the logic of the restriction as the box should never be exposed to internet so you could only break it for yourself.

    I would love wider access as there is a lot that could be done but the environment and setmetatable etc are all blocked so you do not have access to the LUA environment tables.

    You can therefore not add your own code libraries or LUA globals as you were trying.

    Going back to your original post... you get this error because you are trying to do arithmetic on a nil value as you are with the code I posted if you do not define a variable in the HC2 panel.

    If you need to do this type of thing consider using an "or" in the assignment statement to ensure that the variable is initialized to a value if it is the first time through the code.

    Please login or register to see this code.

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • a question:

    i've complete code and i use a repeat until cycle but lua but debugger not like end of cycle….it tell me:

    [ERROR] 15:24:35: line 47: attempt to compare number with nil

    ...

    but "ripetizione" is not nil i've set a tea number….

    the code…

    local var3

    local var4

    local total1

    local contatore

    local startSource = fibaro:getSourceTrigger();

    if (

    ( tonumber(fibaro:getGlobalValue("Termperatura ")) == tonumber("10") )

    or

    startSource["type"] == "other"

    )

    then

    --

    fineciclo = 0

    repeat

    fibaro:debug("Inizio Ciclo")

    fibaro:setGlobal("IrDentro", "1")

    fibaro:call(124, "turnOn");

    fibaro:sleep(10*1000)

    var3=1

    fibaro:debug("variabile var3 settata a 1")

    fibaro:debug(var3)

    var4=fibaro:getGlobal("Ripetizione")

    fibaro:debug("variabile var 4 che è ripetizione prima del decremento")

    fibaro:debug(var4)

    total1= var4-var3

    fibaro:debug("sottraggo,la variabile totale è")

    fibaro:debug(total1)

    --scrivo il dato nell variaible

    fibaro:setGlobal("Ripetizione", total1)

    fibaro:debug("copio ripetizione in variabile contatore che passo al fineciclo")

    -- fibaro:debug(contatore)

    until Ripetizione > 0 >----

    fibaro:setGlobal("IrDentro", "0")

    fibaro:setGlobal("Ripetizione", "1")

    fibaro:call(124, "turnOff");

    fibaro:debug("Spengo la Luce")

    end

    Link to comment
    Share on other sites

    • 0

    I have not checked the logic you have used as we all have different styles but your error comes from this line.

    until Ripetizione > 0 >----

    Ripetizione is not the variable in fibaro panel and is not initialized so is nil

    assuming all else is correct... try

    Please login or register to see this code.

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • doesn't work...

    Please login or register to see this image.

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

    [ERROR] 17:13:54: line 48: bad argument #2 to 'tonumber' (base out of range)

    Link to comment
    Share on other sites

    • 0

    OK need a break from what I am doing.

    You have an IR sensor and you want it to turn on and keep the light on while movement is detected?

    have you got the IR sensor triggering the scene?

    [ Added: 2013-08-25, 18:17 ]

    Please login or register to see this code.

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • yes, sure my rule for switch on light is:

    -IR sensor violated and not armed id: 116

    -Lux sensor < 500 lux id:119

    sorry but i'm newbie of LUA....

    for start scene what do you means?

    Link to comment
    Share on other sites

    • 0

    have edited the code but I don't have any light sensors or IR in my system just now so not sure I have used correct property.

    if it does not work post what you get back when you put the following lines in your browser address bar

    /api/devices?id=116

    /api/devices?id=119

    Hi Fibaro,

    It would be good if global variables were valid across instances of scene so we could write this like...but just now all variables are local to an instance of a scene

    Please login or register to see this image.

    /emoticons/default_icon_confused.gif" alt=":-?" />

    Please login or register to see this code.

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • the script works perfectly......rob pm...

    Please login or register to see this image.

    /emoticons/default_icon_wink.gif" alt=";-)" />

    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.

     Share

    ×
    ×
    • Create New...