Page 1 of 1
How to use time in LUA?
Posted: Saturday 09 January 2016 11:46
by Bram81
Hey,
Maybe this is a stupid question, but I haven't been able to find the answer on this forum nor on the wiki. I'm writing a lua script that has to run with different actions on a device change depending on the time of day. This works fine:
Code: Select all
time = os.date("*t")
commandArray = {}
if (devicechanged['Slot Voordeur'] == 'Closed' and time.hour >= 12 and time.hour <=17) then
commandArray['Spots Woonkamer']='On'
But this way it's checking for entire hours. How could I for example use the time between 09.15 and 09.44?
Using
gives me an error unexpected symbol near character 226.
I'm pretty new to lua, but I hope someone can help me out..
Re: How to use time in LUA?
Posted: Saturday 09 January 2016 12:45
by jvdz
This is an example with the function I use:
Code: Select all
function timebetween(s,e)
timenow = os.date("*t")
year = timenow.year
month = timenow.month
day = timenow.day
s = s .. ":00" -- add seconds in case only hh:mm is supplied
e = e .. ":00"
shour = string.sub(s, 1, 2)
sminutes = string.sub(s, 4, 5)
sseconds = string.sub(s, 7, 8)
ehour = string.sub(e, 1, 2)
eminutes = string.sub(e, 4, 5)
eseconds = string.sub(e, 7, 8)
t1 = os.time()
t2 = os.time{year=year, month=month, day=day, hour=shour, min=sminutes, sec=sseconds}
t3 = os.time{year=year, month=month, day=day, hour=ehour, min=eminutes, sec=eseconds}
sdifference = os.difftime (t1, t2)
edifference = os.difftime (t1, t3)
isbetween = false
if sdifference >= 0 and edifference <= 0 then
isbetween = true
end
--~ print(" s:" .. s .. " e:" .. e .. " sdifference:" .. sdifference.. " edifference:" .. edifference)
return isbetween
end
commandArray = {}
if timebetween("09:15:00","09:44:00") then
--- perform action
end
return commandArray
Jos
Re: How to use time in LUA?
Posted: Saturday 09 January 2016 12:46
by jannl
You could create a time_in_minutes variable: time_in_minutes = time.minutes + time.hour * 60
if time_in_minutes >= 720 and time_in_minutes <= 1020 then
Could do the trick
Re: How to use time in LUA?
Posted: Saturday 09 January 2016 13:24
by Bram81
jvdz wrote:This is an example with the function I use:
Code: Select all
function timebetween(s,e)
timenow = os.date("*t")
year = timenow.year
month = timenow.month
day = timenow.day
s = s .. ":00" -- add seconds in case only hh:mm is supplied
e = e .. ":00"
shour = string.sub(s, 1, 2)
sminutes = string.sub(s, 4, 5)
sseconds = string.sub(s, 7, 8)
ehour = string.sub(e, 1, 2)
eminutes = string.sub(e, 4, 5)
eseconds = string.sub(e, 7, 8)
t1 = os.time()
t2 = os.time{year=year, month=month, day=day, hour=shour, min=sminutes, sec=sseconds}
t3 = os.time{year=year, month=month, day=day, hour=ehour, min=eminutes, sec=eseconds}
sdifference = os.difftime (t1, t2)
edifference = os.difftime (t1, t3)
isbetween = false
if sdifference >= 0 and edifference <= 0 then
isbetween = true
end
--~ print(" s:" .. s .. " e:" .. e .. " sdifference:" .. sdifference.. " edifference:" .. edifference)
return isbetween
end
commandArray = {}
if timebetween("09:15:00","09:44:00") then
--- perform action
end
return commandArray
Jos
Hi, thank you! Seems to be working now using this
Code: Select all
commandArray = {}
if (devicechanged['Slot Voordeur'] == 'Closed' and timebetween ("07:00:00","16:59:59"))then
commandArray['Slapen']='On'
commandArray['Verwarming Bespaarstand']='On'
The strange thing is that the first commandArray Slapen (which is a scene) is not executed, but the second one Verwarming Bespaarstand is.
Log shows no errors. Any idea on that one?
Re: How to use time in LUA?
Posted: Saturday 09 January 2016 13:28
by jvdz
The scene needs a little different command:
Check here for all details:
https://www.domoticz.com/wiki/Events
Jos
Re: How to use time in LUA?
Posted: Saturday 09 January 2016 14:00
by Bram81
Ah, I could have guessed.. Thanks again, working like a charm now!