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

Question

Posted

Hi

I am trying to add 5 hours to sunset and then use that time as an off time for some lights, am using the following code:

local fmt = string.format

function str2sec(str) local h,m,s=str:match("^(%d%d):(%d%d):?(%d*)") return 3600*h+60*m+(s~="" and s or 0) end

function sec2str(sec) return fmt("%02d:%02d%s",math.floor(sec/3600),math.floor((sec % 3600)/60),sec % 60 > 0 and fmt(":%02d",sec%60) or "") end

local SunSetTimeVariable = fibaro.getGlobalVariable("Sunset") --this is a stored value of sunset time updated every day.

local LightOffTime = sec2str(str2sec(SunSetTimeVariable)+(300*60))

 

when I debug this I get:

sunset 20:41

LightOffTime 25:41

 

This seems to be an error as there is no such time as 25 hours?

 

Can someone please help me where I have gone wrong please?

6 answers to this question

Recommended Posts

  • 0
Posted
5 hours ago, Jay Ess said:

Hi

I am trying to add 5 hours to sunset and then use that time as an off time for some lights, am using the following code:

local fmt = string.format

function str2sec(str) local h,m,s=str:match("^(%d%d):(%d%d):?(%d*)") return 3600*h+60*m+(s~="" and s or 0) end

function sec2str(sec) return fmt("%02d:%02d%s",math.floor(sec/3600),math.floor((sec % 3600)/60),sec % 60 > 0 and fmt(":%02d",sec%60) or "") end

local SunSetTimeVariable = fibaro.getGlobalVariable("Sunset") --this is a stored value of sunset time updated every day.

local LightOffTime = sec2str(str2sec(SunSetTimeVariable)+(300*60))

 

when I debug this I get:

sunset 20:41

LightOffTime 25:41

 

This seems to be an error as there is no such time as 25 hours?

 

Can someone please help me where I have gone wrong please?

If you are adding time you need to make sure you don't spill over 24 hours. Take the seconds modulo 24*3600 (seconds in 24 hours) and you get the rest.

local LightOffTime = sec2str((str2sec(SunSetTimeVariable)+(300*60)) % (24*60*60))

  • 0
  • Inquirer
  • Posted

    @jgab

    Thank you for your help, it works a treat. Can you explain what the % is doing please?

    • 0
    Posted
    1 hour ago, Jay Ess said:

    @jgab

    Thank you for your help, it works a treat. Can you explain what the % is doing please?

    % is called the 'Modulus Operator' and is the remainder after an integer division.

    26 % 24 => 2

    and that is what we use to make sure that if the time exceeds 24hours we wrap around.

    % also has the nice property that 

    20 % 24 => 20

    so we can use it even when the time is less than 24 hour

    • 0
    Posted

    ...and it's quite useful

    Please login or register to see this code.

    will print
     

    Please login or register to see this code.

     

    Adding one makes it give the sequence 1,2,3,4,1,2,3,4 

    because % binds harder than + so i%4+1 == (I%4)+1

    Please login or register to see this code.

     

    Assume you want to take the next in an array and make the index wrap around

    Please login or register to see this code.

     

    • 0
  • Inquirer
  • Posted

    @jgab

    Thank you for your help.

    • 0
    Posted (edited)

    If you code a QA and do a lot of time manipulation you could add this 24hour time type

    Please login or register to see this code.

     

    Edited by jgab
    • Like 1

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