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

Check if current time is between two times


Edward Trapp

Question

Hi

 

I'd like to share very handy function to check if time is between two other times. This is very useful for example for checking if motion sensor should at this time of day switch warm or cool lights. There are two functions

IsTimeBetween(StartH, StartM, StopH, StopM, TestH, TestM) - which checks if TestH:TestM is between StartH:StartM and StopH:StopM

IsItBetween(StartH,StartM,StopH,StopM) - this one is a wrapper to check against current time of day.

 

Quote

 

local debug = false

local function IsTimeBetween(StartH, StartM, StopH, StopM, TestH, TestM)
  -- Convert all times to minutes for easier comparison
      local TempStart = StartH*60+StartM
      local TempStop = StopH*60+StopM
      local TempTest = TestH*60+TestM
  -- Debug
      if (debug) then
        fibaro:debug("TempStart: " .. TempStart)
        fibaro:debug("TempStop: " .. TempStop)
        fibaro:debug("TempTest: " .. TempTest)
      end
  -- Check for possibility that end time is on next day
  -- (when ending time is before start time)
      if (TempStop<TempStart) then
  -- If it is check if tested time is on next day as well
  -- (when tested time is after midnight but before ending time)
        if (TempTest<=TempStop) then TempTest = TempTest + 24*60 end
        TempStop = TempStop + 24*60
  -- Debug - display times after adjustments
        if (debug) then
            fibaro:debug("- After adjusting times -")
            fibaro:debug("TempStart: " .. TempStart)
            fibaro:debug("TempStop: " .. TempStop)
            fibaro:debug("TempTest: " .. TempTest)
        end
    end
  
  -- Debug
      if (TempStop<TempStart) then
        fibaro:debug("IsTimeBetween() - Invalid parameters!")
        fibaro:abort()
    end
  -- Return
       return (TempStart <= TempTest and TempStop >= TempTest)
end


local function IsItBetween(StartH,StartM,StopH,StopM)
 -- Simple wrapper to check current time
    local time = os.date("*t")
      return IsTimeBetween(StartH, StartM, StopH, StopM, time.hour, time.min)
end

 

 

 

 

 

Example of usage with RGBW controller and Motion sensor:


 

Quote

 

--[[
%% properties
666 value
%% weather
%% events
%% globals
--]]

 

 

******** Paste functions here *************

 

-- Actors' IDs
local MotionSensor_ID = 666

local RGBW_ID = 999

 

-- Define range of time - in this example I want to turn different light settings on my RGBW led stripe at different times of day - Full Light during evening and subtle warm light at night

local EveningStartHour = 20

local EveningStartMinute = 00

local NightStartHour = 23

local NightStartMinute = 00

local StopHour = 05

local StopMinute = 00

 

-- Check if its evening

if (IsItBetween(EveningStartHour, EveningStartMinute, NightStartHour , NightStartMinute)) then

        fibaro:call(RGBW_ID, "setColor", "255","255","255","255")

        fibaro:call(RGBW_ID, "turnOn")

-- Check if its night

elseif (IsItBetween(NightStartHour , NightStartMinute, StopHour, StopMinute)) then
        fibaro:call(RGBW_ID, "setColor", "15","7","0","0")

        fibaro:call(RGBW_ID, "turnOn")

end

 

 

 

This solution isn't idiot proof so if You put absurd hours You will get absurdal behaviour. It isn't also proof to daylight savings time. Otherwise it works just fine. Function like this should come as a standard in Fibaro API. Hope someday it will.

 

 

@Admin - for some reason I couldn't start new topic in crowds solutions so I've put it here.

Edited by Edward Trapp
Link to comment
Share on other sites

10 answers to this question

Recommended Posts

  • 0

timeNow                     = os.date("%H:%M")
local EveningStart     = "20:00"
local NightStart         = "23:00"
local StopHour          = "05:00"
if timeNow > EveningStart and timeNow < NightStart then
  fibaro:call(RGBW_ID, "setColor", "255","255","255","255")
  fibaro:call(RGBW_ID, "turnOn")
elseif timeNow > NightStart and timeNow < StopHour then
  fibaro:call(RGBW_ID, "setColor", "15","7","0","0")
  fibaro:call(RGBW_ID, "turnOn")
end

  • Like 1
Link to comment
Share on other sites

  • 0

Hello Kage,

Nice  LUA was looking for it!

Is it possible to change local evening start with fa sunset minus 45 minutes?

If I do it I will get errors

fa if I change "23:00">>>>"fibaro:getGlobalValue("DayEveningNight") == "Night" " will give error >>

 

Please login or register to see this attachment.

Link to comment
Share on other sites

  • 0
  • Inquirer
  • 5 hours ago, Kage said:

    elseif timeNow > NightStart and timeNow < StopHour then
      fibaro:call(RGBW_ID, "setColor", "15","7","0","0")
      fibaro:call(RGBW_ID, "turnOn")
    end

     

    Kage, thanks for simplified time comparison code - didn't know with LUA it's that simple. Still Your code needs tweaking because its not 2-days-span proof - mathematiclly 01:00 is never > 23:00. I'll rewrite my function with Your method later.

     

    As for Sjakie You might be looking for: fibaro:getValue(1, "sunsetHour") but for now i can't tell You how to easily modify it by 45 minutes ;)

    Link to comment
    Share on other sites

    • 0

    Here is a version that handles sunset/sunrise, +/- offsets, and fibaro globals. 'toTime' that converts an expression to seconds, and a similar 'between' as in your example above.

    Please login or register to see this code.

    with output

    Please login or register to see this code.

     

    Edited by jgab
    small error in regexp
    • Like 1
    Link to comment
    Share on other sites

    • 0
    5 minutes ago, Edward Trapp said:

    It's going to take some time to understand all those brilliant shortcuts but that's as short and sweet as it could be. Thank You @jgab

    It's small because there are no error checks - expects valid expressions or will produce strange results.

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • Yep...

     

    Still amazed with:

     

    Please login or register to see this code.

     

    Is that lua equivalent of (a>=b  ?  x : y)   ?

    Link to comment
    Share on other sites

    • 0
    2 minutes ago, Edward Trapp said:

    Yep...

     

    Still amazed with:

     

    Please login or register to see this code.

     

    Is that lua equivalent of (a>=b  ?  x : y)   ?

    Yes, lua lacks that construct so a common pattern is:  a = <test> and <va1> or <val2>

    which then becomes a bit smaller than an if-then-else.

    However, it’s not equivalent, as <val1> can’t be false or nil...

    Link to comment
    Share on other sites

    • 0

    @sjakie 

    change local EveningStart  = os.date("%H:%M", os.time()+45*60)

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