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


Question

Posted

Hi, i want to run a few actions during a timer using fibaro:sleep(20 seconds)

But the timer has to run through the sleep value. 

Say the timer ist 60 seconds. after that a device has to be turned off.

While the timer runs, the device can be turned on and stay on for 30 seconds.

The problem is: If the device will be turned on after the timer passes the 30 seconds, my device has to be turned of when the timer ends. so the device does not run tis 30 seconds. But that does not work.

How does it work, that my timer keeps counting, even if i do actions in the loop? Especially when i use fibaro:sleep...

 

 

6 answers to this question

Recommended Posts

  • 0
Posted

Hi @Fuchs86, and welcome to the forum.  I think people here will need a bit more information to help, can you describe your use case, and also post the code?  Also are you using HC2 or HC3?

  • 0
  • Inquirer
  • Posted

    Hi, i will trie to explain my Idea.

    I have a timeslot of 60 minutes. Within these 60 Minutes a customer can book a appointment of 30 minutes duration.

    For example: Booking is from 10:00 AM till 10:30 AM. The next bokking is at 11:00 AM. So The customer has 60 minutes to start his 30 minutes appointment.

    My scene starts automatically at 10:00  AM and from there on the customer can come and start his appointment by pressing a button(switch).

    The difficult thing ist: If a Customer comes late, so that his 30 Minutes would run into the next session.

    My other problem is that i cant use sleep during the timer because it stops counting in this period.

    I tried it with two timers. Here is what should happen:

     

    --[[
    %% properties
    %% globals
    --]]
     
    local grottenbeleuchtung = 79
    local vernebler = 80

    ---------------Start--------------------------
     local starttimer2 = 30;
     local starttimer = 60;
     local timer = (starttimer); 
                     fibaro:debug("Starte Timer");
                 fibaro:call(grottenbeleuchtung, "turnOn"); 
    repeat


             if timer > 30 
            and  (tonumber(fibaro:getValue(vernebler, "value"))) > 0 then 
                timer2=(starttimer2);  
                           fibaro:debug("Starte Timer2");
                    fibaro:call(vernebler, "turnOn");
                    fibaro:sleep(15*60*1000);
                    fibaro:call(vernebler, "turnOff");
                    fibaro:sleep(10*60*1000);
                    fibaro:call(vernebler, "turnOn");
                    fibaro:sleep(5*60*1000);
                    fibaro:call(vernebler, "turnOff");
             repeat    
                fibaro:sleep(1000); 
                  timer2=timer2-1
                  print("Timer 2:", timer2)
      
            
              until (timer2<1) 
                fibaro:call(vernebler, "turnOff");
            end
            
                   
         
         fibaro:sleep(1000); 
             print("Timer 1:", timer)    
     timer=timer-1
      
                if     timer <= 30
                and  (tonumber(fibaro:getValue(vernebler, "value"))) > 0 then 
                        fibaro:debug("Starte Timer");
                      fibaro:call(vernebler, "turnOn");
                  repeat 
                     fibaro:sleep(1000); 
                      timer=timer-1
                      print("Timer 3:", timer) 
                 until (timer<1) 

                    fibaro:call(vernebler, "turnOff");

                end

     until (timer<1) 

    fibaro:killScenes(sceneId)
     

     

     

    • 0
    Posted
    10 minutes ago, Fuchs86 said:

    So The customer has 60 minutes to start his 30 minutes appointment.

    If you change your policy to "the customer has 30 minutes to start his 30 mins appointment", then they won't clash with the next appointment. :-)

     

    Rather than sleeping, perhaps you should consider setting up a local time variable called "EndTime".  When the routine starts, calculate the end time and store it in that variable.  Then come back every minute and compare current time with EndTime to see if the finish time has been reached.

    • 0
  • Inquirer
  • Posted

    I will try that. Can u give me a short example with the codes i have to use?

    • 0
  • Inquirer
  • Posted

    And i need the code to get the actual timer value at the time the Button gets pressed. 

    • 0
    Posted

    You can use this as a basis.  Cut down version of what I use to open blinds in the morning.  The main part of the code is at the bottom, it checks for what triggered the scene and directs to other functions.  repeatFunction() is what checks the time every minute.  In my case, it is always checking for 07:30, but you can write code to change that to set the variable as the current time (in doMyActions). 

     

    Code not tested, just provided as a starting point for you to play with.  Good luck :-)

     

    --[[
    %% autostart
    %% properties
    %% weather
    %% events
    45 CentralSceneEvent
    %% globals
    --]]

    -- Setup variable
    openTime = "07:30";

    -- Create a function to do whatever you need:
    function doMyActions()
       -- PUT YOUR ACTION CODE HERE, including setting openTime = string.format("%02d", currentDate.hour) .. ":" .. string.format("%02d", currentDate.min)


    end

    -- Create a function to keep coming back to check if it's time
    function repeatFunction()
       -- First check if we've hit the right time to consider opening the blinds
       local currentDate = os.date("*t");
       local startSource = fibaro:getSourceTrigger();  -- (does this line do anything?)
          if (string.format("%02d", currentDate.hour) .. ":" .. string.format("%02d", currentDate.min) == openTime) 
          then   

                   doMyActions()
                   fibaro:debug('I've done what you asked at the appointed time of ' .. openTime);      
             end
       end
       setTimeout(repeatFunction, 60*1000) --repeat the function in one minutes time
    end

    -- Main script, with different functions based on the scene trigger
    local sourceTrigger = fibaro:getSourceTrigger();

    if (sourceTrigger["type"] == "autostart") -- ie the script was started automatically
       then
          repeatFunction() -- go and kick off the repeating function
          fibaro:debug('repeatFunction has been started'); 
      
    elseif (sourceTrigger["type"] == "other") -- ie the scene has been triggered manually
       then
          doMyActions()

      
     
      end
      
    end
     

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