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

Global variable time "12:00" cannot be stored


jnathalia
 Share

Question

I use global variable set with a time string. This works fine if set with a scene. However if set via the control panel - variables it accepts only integer/real numbers.

 

Should the software allow just numbers and strings, so is this an interface issue which should be redirected to Fibaro or is my approach just false?

Link to comment
Share on other sites

9 answers to this question

Recommended Posts

  • 0

I'd say, if you want to use the panel, then adapt your scenes to use numbers. Then use something like this:

 

Please login or register to see this code.

Link to comment
Share on other sites

  • 0

Please login or register to see this code.

Can you explain to me what you did?

I'm trying to understand/learn...

Please login or register to see this image.

/emoticons/default_icon_wink.gif" alt=";-)" />

 

The first line I understand, but the second and third not.

Link to comment
Share on other sites

  • 0

I'm sorry if I was too terse. I'll make a script with some more details and post it here.

Link to comment
Share on other sites

  • 0

Can you explain to me what you did?

I'm trying to understand/learn...

Please login or register to see this image.

/emoticons/default_icon_wink.gif" alt=";-)" />

 

The first line I understand, but the second and third not.

 

% means modulo. So you will get the last two digits of the time.

E.g if the global variable contains "1035" you will get "35".

The last line gives you all other digits but the last two ones (in this example "10").

Link to comment
Share on other sites

  • 0

Please login or register to see this code.

-- the name of the "Global Variable" in "variableName".

variableName="TimeCoded" -- your global variable name here. Mine is "TimeCoded".

-- Now enter some integer value in the panel.

-- Or use the code below

-- fibaro:setGlobal(variableName,1234) -- 1234 means 12:34 duration or time. It is an integer

-- Read the global variable from the variables panel into "TimeCoded"

-- This makes the code easier to read.

TimeCoded = tonumber(fibaro:getGlobalValue(variableName))

fibaro:debug("Global variable '"..variableName..

"'contains: "..TimeCoded)

-- Let us "decode" that time, meaning, splitting the number

-- from the variables panel into hours & minutes

Minutes = TimeCoded%100 -- keep only last 2 digits

Hours = math.floor(TimeCoded/100) -- keep only digits 3 and higher

fibaro:debug("Global variable split into Hours and Minutes:")

fibaro:debug("Hours: ".. Hours)

fibaro:debug("Minutes: ".. Minutes)

fibaro:debug("")

fibaro:debug("Time comparison test:")

if Hours >= 12 then

if Hours < 13 and Minutes <= 30 then

fibaro:debug("Global says: I am at lunch")

end

fibaro:debug("Global is after 12:30, hurry!")

else

fibaro:debug("Global is before noon, relax!")

end

fibaro:debug("")

-- Now let us compare current time & global variable

fibaro:debug("Comparison with current time:")

currentDate = os.date("*t")

if currentDate.hour >= Hours and currentDate.min > Minutes then

fibaro:debug("Now we are: after the global value")

else

fibaro:debug("Now we are: before the global value")

end

fibaro:debug("")

fibaro:debug("String formatting example (HH:MM) gives:")

-- Want to turn it into "HH:MS" format? Use c-style formatting

-- "%02d" means: print a variable with decimal style, 2 positions

-- and prefix with zeroes.

fibaro:debug(

string.format("The time in the Global Variable is: %02d:%02d",Hours,Minutes)

)

fibaro:debug("")

-- How to convert a string like"01:35" into number 135 so it can be stored in the Global Variable?

TimeString="01:35"

-- This is VERY IDIOMATIC LUA... Pattern matching and iterators and then some.

--

Please login or register to see this link.

--

Please login or register to see this link.

-- underscores mean "not interested in return value"

_,_,Hours,Minutes=TimeString:find("(%d+)

Please login or register to see this image.

/emoticons/default_sad.png" alt=":(" srcset="https://forum.fibaro.com/uploads/emoticons/[email protected] 2x" width="20" height="20" />%d+)")

Minutes = tonumber(Minutes)

Hours = tonumber(Hours)

-- Combine both

TimeCoded = Hours*100 + Minutes

fibaro:debug("TimeCoded is: ".. TimeCoded)

-- Ready to store, if you wanted to. Uncomment next line.

-- fibaro:setGlobal(variableName,TimeCoded)

Link to comment
Share on other sites

  • 0

% means modulo. So you will get the last two digits of the time.

E.g if the global variable contains "1035" you will get "35".

The last line gives you all other digits but the last two ones (in this example "10").

Thanks. Learning all the time, learning all the time...

Please login or register to see this image.

/emoticons/default_biggrin.png" alt=":D" srcset="https://forum.fibaro.com/uploads/emoticons/[email protected] 2x" width="20" height="20" />

Link to comment
Share on other sites

  • 0

Please login or register to see this code.

Nice! Got it!

Please login or register to see this image.

/emoticons/default_icon_mrgreen.gif" alt=":mrgreen:" />

Link to comment
Share on other sites

  • 0
  • Inquirer
  • Thanks for the solution. This is a good recommendation although fundamentally I'm wondering if a string is accepted as global variable and it is... As when a scene is assigning a string to a global variable it is being shown in the variable panel and it also can be used in the logic of other scenes.

    So I would say: Fibaro, change the user interface and accept string input as well (also make the width of the field a bit broader)

    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.

     Share

    ×
    ×
    • Create New...