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

cant get while do loop to work


Question

Posted (edited)

Hi Friends

 

I am trying to create a simple scene where, as long as the door is open the alarm rings, and the moment the door is closed the alarm should stop. I am trying to achieve this via while do loop but without success. 

I open the door alarm rings, but it does not switches off when door is closed. Below is the code 

 

The logic is as follows .

 

If Door Ajar == Armed and breached

then

alarm turns on

and keep on running while the door is ajar

the moment door is closed the alarm stops

 

 

what is happening is the while loop gets stuck and you cannot even disarm the alarm in that case  ....

 

--[[
%% properties
204 value
204 armed
%% weather
%% events
%% globals
--]]

local startSource = fibaro:getSourceTrigger();
if (
 ( (tonumber(fibaro:getValue(204, "value")) > 0 and tonumber(fibaro:getValue(204, "armed")) > 0) )
or
startSource["type"] == "other"
)
then
    while ( tonumber(fibaro:getValue(204, "value")) ~= 0 ) do
    fibaro:call(194, "turnOn");
    end

end

Edited by sam9s

9 answers to this question

Recommended Posts

  • 0
Posted (edited)

@sam9s no need that if statement at top because it is same as while condition ( ~-= 0 is same as > 0 except your device has a negative value.....)
 

Please login or register to see this code.

 

Edited by Sirhideo
  • 0
  • Inquirer
  • Posted

    Ok tnaks below is the updated code, but its not turning on the alarm, even though it shows breached ..

     


    local startSource = fibaro:getSourceTrigger();
    if (
     ( tonumber(fibaro:getValue(204, "armed")) > 0) .... removed the breached condition as you said its already there.
    or
    startSource["type"] == "other"
    )
    then
        while ( tonumber(fibaro:getValue(204, "value")) > 0 ) do -- when condition match, loop below

            if ( tonumber(fibaro:getValue(204, "value")) == 0 ) then -- when this condition matched then...
               fibaro:call(194, "turnoff");
               fibaro:abort() -- whole scene abort
            end 
             fibaro:call(194, "turnon"); -- when while condition is positive this statment keeps loop
             fibaro:sleep(1000) -- loop every 1 second
        end

    end

     

     

    This code is not turning on the alarm. but HC2 shows gives the mnessage of breached and I can then disarm the scene, but alarm is not getting triggered ..... can you assist

    • 0
  • Inquirer
  • Posted

    any assistance will be highly appriciated ....

    • 0
    Posted
    1 hour ago, sam9s said:

    any assistance will be highly appriciated ....

     

    Try change code  for turnOn  and  turnOff

    • 0
  • Inquirer
  • Posted

    No does not work, surprising to test I created another test scene with only while loop to test if the while logic is working, and to my dismay the moment I put while condition, the IF condition in side stops working. If I replace the while do loop with another IF then statement then the inside IF condition works, but obviously only once, which is not what I want hence the need for while loop, but while loop is not working as intended,. not sure where the issue is .. :(  ..please if someone can help ....

    • 0
    Posted (edited)

    @sam9s

     

    Let's make this improvement to see if that helps your requirement.

     

    Please login or register to see this code.

     

     

     

    Edited by Sirhideo
    • Like 1
    • 0
    Posted

    I don't think the issue  is the code, usually during the alarm all scenes are stopped. Please make sure your scene allowed to run

    Please login or register to see this image.

    /monthly_2018_05/Capture1.PNG.443d903184b91aa74ecfe97f98974b03.PNG" width="326" alt="Capture1.PNG.443d903184b91aa74ecfe97f98974b03.PNG" />

    • 0
  • Inquirer
  • Posted
    7 hours ago, Sirhideo said:

    @sam9s

     

    Let's make this improvement to see if that helps your requirement.

     

    Please login or register to see this code.

     

     

     

    Hi Sirhideo,

     

    Thank you so much this code helped, even though it didnt work exactly, but I got the logic and rephrased the code as below and all worked 

     

    local startSource = fibaro:getSourceTrigger();
    local doorID = xxx
    local alarmID = xxx
    local doorCheck = 0
    local counterTime = 0
    if (
     ( (tonumber(fibaro:getValue(204, "value")) > 0 and tonumber(fibaro:getValue(204, "armed")) > 0) )
    or
    startSource["type"] == "other"
    )
    then
        fibaro:call(194, "turnOn");
        doorCheck = 1;
    else
        fibaro:abort(); -- doesn't match your requirement, abort scene
    end

    while doorCheck == 1 do
        if (( tonumber(fibaro:getValue(204, "value")) == 0 )
         or
          startSource["type"] == "other"
           )
        then
         fibaro:call(194, "turnOff"); -- the moment door is closed the alarm stops 
         fibaro:debug('Door is closed, scene end')
         fibaro:abort();
        end
         counterTime = counterTime + 1; -- and keep on running while the door is ajar
         fibaro:sleep(1000)
         fibaro:debug('..Waiting for Door Close .. ' ..counterTime)
    end

    7 hours ago, cag014 said:

    I don't think the issue  is the code, usually during the alarm all scenes are stopped. Please make sure your scene allowed to run

    Please login or register to see this link.

    That was not the case here, I know what that option does.

    • 0
    Posted

     Just out of my curiosity, I think code below could provide same functionality.

    Off course I might be wrong...

    Please login or register to see this code.

     

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