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

Please help me with LUA to set lights to previous level after a scene turns a light off


Question

Posted (edited)

Hi all, 

 

I am trying to incorporate a previous state into a lua lighting scene. 

 

For example, if I trigger the scene, it takes reference of the current state of relay ID 81 and also 'value' of Dimmers ID 114 and 109 . 

Scene will then call Relay54 'turnOn' and Dimmer 114 setValue 10 and 109 to  'setvalue', 100 

 

I then wish to return those two loads to their previous state or level after 15 mins. 

 

 

Could somebody please help with this code. Dimmers are behaving properly (ID 114 and 109) but I'm having troubles with value, set value etc for the relay. 

 

At the end of the scene, the relay 54 stays turned on even if it was off before. 

 

 

 

%% properties

%% events
%% globals

--]]

local dining
local garage
local backyard

dining = tonumber (fibaro:getValue (114, 'value'));
fibaro:setGlobal  ('dining', dining);
fibaro:debug  ("dining level set") 
fibaro:debug (dining) 


backyard = tonumber (fibaro:getValue (109, 'value'));
fibaro:setGlobal  ('backyard', backyard);
fibaro:debug  ("backyard level set")
fibaro:debug (backyard) 
 

garage = tonumber ( fibaro:getValue(54, "value"));
fibaro:setGlobal  ('garage', garage);
fibaro:debug  ("garage level set")
fibaro:debug (garage) 

fibaro:call(114, 'setValue', '10');
fibaro:call(109, 'setValue', '100');
fibaro:call(54, 'turnOn');
fibaro:debug ("all lights on from scene activation")
fibaro:debug ("I will now wait 20 seconds")

fibaro:sleep (20000);
fibaro:debug ("wait over set all lights back to previous levels") 

fibaro:call(114, 'setValue', dining );
fibaro:call(109, 'setValue', backyard );
fibaro:call(54, 'setValue', garage );
fibaro:debug ("all lights back to previous levels")

:-)

 

 

 

Edited by sarf77

8 answers to this question

Recommended Posts

  • 0
  • Inquirer
  • Posted

     I have also tried this variant with no luck. 

     

    Dimmers working spot on but relay wont play ball. 

     

    --[[
    %% properties

    %% events
    %% globals

    --]]

    local dining
    local garage
    local backyard


    dining = tonumber (fibaro:getValue (114, 'value'));
    fibaro:setGlobal  ('dining', dining);
    fibaro:debug  ("dining level set"); 
    fibaro:debug (dining); 


    backyard = tonumber (fibaro:getValue (109, 'value'));
    fibaro:setGlobal  ('backyard', backyard);
    fibaro:debug  ("backyard level set");
    fibaro:debug (backyard); 
     

    garage = tonumber (fibaro:getValue(54, 'value'));
    fibaro:setGlobal  ('garage', garage);
    fibaro:debug  ("garage level set");
    fibaro:debug (garage); 


    fibaro:call(114, 'setValue', '10');
    fibaro:call(109, 'setValue', '100');
    fibaro:call(54, 'turnOn');
    fibaro:debug ("all lights on from scene activation");
    fibaro:debug ("I will now wait 20 seconds");

    fibaro:sleep (20000);
    fibaro:debug ("wait over set all lights back to previous levels"); 


    fibaro:call(114, 'setValue',dining );
    fibaro:debug ('dining set back')
    fibaro:call(109, 'setValue',backyard );
    fibaro:debug ('backyard set back')
    if ('garage' == '0') then
      fibaro:call (54, 'turnOff')
      end

    fibaro:debug ('garage set back')
      


    fibaro:debug ("all lights back to previous levels");

     

     

    • 0
    Posted (edited)
    2 hours ago, sarf77 said:

    Dimmers working spot on but relay wont play ball. 

    Please login or register to see this code.

     

    This test the string 'garage' against the string '0'.  You want to test the variable garage against the number 0

    Please login or register to see this code.

     

    Edited by jgab
    • 0
    Posted

    Maybe you can try this?

     

     

    --[[
    %% properties
    %% events
    %% globals
    114 value
    109 value
    54 value
    --]]

    local dining = fibaro:getValue(114, "value")
    local backyard = fibaro:getValue(109, "value")
    local garage = fibaro:getValue(54, "value")

    if 
      (tonumber(fibaro:getValue(81, "value")) == tonumber("1"))--- your ID 81 turned on 
      then
     fibaro:setGlobal('backyard', "backyard");
     fibaro:setGlobal('dining', "dining");
     fibaro:setGlobal('garage', "garage");
     fibaro:debug(" Globals backyard dining garage set")
    fibaro:call(114, 'setValue', '10');
    fibaro:call(109, 'setValue', '10');
    fibaro:call(54, 'turnOn');
    fibaro:debug ("all lights on from scene activation")
    fibaro:debug ("I will now wait 20 seconds")
    fibaro:sleep (20000);
    fibaro:debug ("wait over set all lights back to previous levels") 
    fibaro:call(114, 'setValue', dining );
    fibaro:call(109, 'setValue', backyard );
    fibaro:call(54, 'turnOff');
    fibaro:debug ("all lights back to previous levels")
    end


    ---more code to do if ID 81 IS 0 ?????

    • 0
  • Inquirer
  • Posted
    42 minutes ago, jgab said:

    This test the string 'garage' against the string '0'.  You want to test the variable garage against the number 0

    Please login or register to see this code.

     

    jgab, 

     

    Thanks so much for your help. I'm VERY new to LUA and normally just grab code from other sources to achieve my result. This one I did from scratch and this last part had me stumped for hours. You saved me. Thank you :-) 

    Works a treat now. 

     

    The script will now be modified to suit a larger scale where I have a set of gates triggering a USB that set a welcome scene going. It turns on around 12 different mixed loads and some may already be in use when scene starts. When the scene times out it will set everything back as it was. 

     

    im starting to now think I don't need the global variables at all? I'll go back through and rethink that.  

     

     

    Thanks again. 

    • 0
    Posted
    18 hours ago, sarf77 said:

    jgab, 

     

    Thanks so much for your help. I'm VERY new to LUA and normally just grab code from other sources to achieve my result. This one I did from scratch and this last part had me stumped for hours. You saved me. Thank you :-) 

    Works a treat now. 

     

    The script will now be modified to suit a larger scale where I have a set of gates triggering a USB that set a welcome scene going. It turns on around 12 different mixed loads and some may already be in use when scene starts. When the scene times out it will set everything back as it was. 

     

    im starting to now think I don't need the global variables at all? I'll go back through and rethink that.  

     

     

    Thanks again. 

     

    You typically only need to store something in a global if you need to remember something between two different invocations of a scene (or sometimes as extra security if you have a long running scene that may crash, and need to remember what it was doing when restarting).

    If you trigger, set some lights, sleep, restore some lights - then you shouldn't need to store the state in a variable.

    • 0
  • Inquirer
  • Posted
    On 5/25/2019 at 10:05 PM, MARCUSP.I.L said:

    Maybe you can try this?

     

     

    --[[
    %% properties
    %% events
    %% globals
    114 value
    109 value
    54 value
    --]]

    local dining = fibaro:getValue(114, "value")
    local backyard = fibaro:getValue(109, "value")
    local garage = fibaro:getValue(54, "value")

    if 
      (tonumber(fibaro:getValue(81, "value")) == tonumber("1"))--- your ID 81 turned on 
      then
     fibaro:setGlobal('backyard', "backyard");
     fibaro:setGlobal('dining', "dining");
     fibaro:setGlobal('garage', "garage");
     fibaro:debug(" Globals backyard dining garage set")
    fibaro:call(114, 'setValue', '10');
    fibaro:call(109, 'setValue', '10');
    fibaro:call(54, 'turnOn');
    fibaro:debug ("all lights on from scene activation")
    fibaro:debug ("I will now wait 20 seconds")
    fibaro:sleep (20000);
    fibaro:debug ("wait over set all lights back to previous levels") 
    fibaro:call(114, 'setValue', dining );
    fibaro:call(109, 'setValue', backyard );
    fibaro:call(54, 'turnOff');
    fibaro:debug ("all lights back to previous levels")
    end


    ---more code to do if ID 81 IS 0 ?????

    Mate THANK you.

     

    I see the simplicity in that.

     

    I don't understand the difference however between '  '   and "  " . Would you mind explaining?

     

     

    Also, why are we declaring the global variables up top? what does that mean?

     

     

    • 0
    Posted

    The    '  ' and "  " as well as globals use is best explained in official fibaro lua api manual;

    or

    by looking on forum.

    Please login or register to see this link.

    I have a tendency to copy from blocks and  " "  is therefore used in the scene for values and globals.

    Also you can drop the  ==tonumber("1") and replace with == 'value' > 0. for example.

     

     

    Hei, have a look at below site for a little more info about lua and fibaro;

    Please login or register to see this link.

     

    Your above scene can be written in many ways, but agreeing with @jgab no need to store light state in globals, although i like the handy idea of storing the previous value.

    Search for @jgab, as well as many others who ar far more experienced than me, for ideas.

     

    I have a block test scene that i use to look at lua,

    (create a scene in block and temporarily change to LUA to have a look at code.)

    Pay attention to order of  headers.

    Good luck

    • Like 1
    • 0
  • Inquirer
  • Posted
    15 hours ago, MARCUSP.I.L said:

    The    '  ' and "  " as well as globals use is best explained in official fibaro lua api manual;

    or

    by looking on forum.

    Please login or register to see this link.

    I have a tendency to copy from blocks and  " "  is therefore used in the scene for values and globals.

    Also you can drop the  ==tonumber("1") and replace with == 'value' > 0. for example.

     

     

    Hei, have a look at below site for a little more info about lua and fibaro;

    Please login or register to see this link.

     

    Your above scene can be written in many ways, but agreeing with @jgab no need to store light state in globals, although i like the handy idea of storing the previous value.

    Search for @jgab, as well as many others who ar far more experienced than me, for ideas.

     

    I have a block test scene that i use to look at lua,

    (create a scene in block and temporarily change to LUA to have a look at code.)

    Pay attention to order of  headers.

    Good luck

    Thanks very much Marcus for the above resources and advice.

    As you can see I'm sure, I'm doing what i can to get stuff working correctly. After that i will learn to consolidate and streamline my code.

    As for the previous values, I feel that any lighting scene should return to its previous state once finished and I'm trying to make this a standard in our business.

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