Page 1 of 1
LUA if date between a and b, and time between c and d?
Posted: Monday 23 November 2015 8:21
by thorbj
Hi, is it possible to run commands in a script only at given dates. For example, if I want to turn on a wallplug between december 1st. and january 31st. and between 5pm and 9 am. How could I do this?
Thanks!
Re: LUA if date between a and b, and time between c and d?
Posted: Monday 23 November 2015 8:40
by ThinkPad
Re: LUA if date between a and b, and time between c and d?
Posted: Monday 23 November 2015 8:48
by BakSeeDaa
You can also try the os.date function.
produces the table
{year = 1998, month = 9, day = 16, yday = 259, wday = 4,
hour = 23, min = 48, sec = 10, isdst = false}
Then just go ahead scripting ...
Code: Select all
local t = os.date("*t")
if (t.month == 12) and (t.hour >= 5) then
DO SOMETHING
end
It's just a start. I leave it up to You to figure out how to complete the script

Re: LUA if date between a and b, and time between c and d?
Posted: Monday 23 November 2015 13:36
by thorbj
Thank you @ThinkPad and @BakSeeDaa!
I have managed to come up with the following script, but I can't get it to work right.
It sets the correct value in the uservariable, but it won't turn on the switch.
What can I be missing?
Code: Select all
-- script_device_xmaslights.lua
commandArray = {}
local xmas_var = 'TidForJulelys' -- UserVariable
local xmas_lights = 'Julelys' -- Switch for Christmas-lights
time = os.time()
month = tonumber(os.date('%m', time))
day = tonumber(os.date('%d', time))
h = tonumber(os.date('%H', time))
-- Function for define a UserVariable based on day and month.
if month == 11 and day >= 23 and uservariables[xmas_var] == 0 then
commandArray['Variable:TidForJulelys'] = '1'
elseif month == 02 and day >= 01 and uservariables[xmas_var] == 1 then
commandArray['Variable:TidForJulelys'] = '0'
end
-- Turn on a switch if the UserVariable == 1 and within a given time.
if (h >= 13 and h <= 6 and uservariables[xmas_var] == 1 and otherdevices[xmas_lights] == 'Off')
then
commandArray[xmas_lights]='On'
elseif (uservariables[xmas_var] == 0 and otherdevices[xmas_lights] == 'On') then
commandArray[xmas_lights]='Off'
end
return commandArray
Thanks!
Re: LUA if date between a and b, and time between c and d?
Posted: Monday 23 November 2015 14:14
by ThinkPad
I don't know (script is a bit hard to read due to the print statements

), but as a workaround you could create a virtual switch 'TidForJulelys' and turn that on by the script, so you can check for the status of that?
Re: LUA if date between a and b, and time between c and d?
Posted: Monday 23 November 2015 14:28
by thorbj
ThinkPad wrote:I don't know (script is a bit hard to read due to the print statements

), but as a workaround you could create a virtual switch 'TidForJulelys' and turn that on by the script, so you can check for the status of that?
Sorry, I've removed the print statements
I also tried with a virtual switch, but that didn't work neither. I suspect it is my time-command that messes it up somehow.
But I am not able to see what I'm failing on here.
Code: Select all
if (h >= 13 and h <= 6 and uservariables[xmas_var] == 1 and otherdevices[xmas_lights] == 'Off')
then
commandArray[xmas_lights]='On'
elseif (uservariables[xmas_var] == 0 and otherdevices[xmas_lights] == 'On') then
commandArray[xmas_lights]='Off'
end
Re: LUA if date between a and b, and time between c and d?
Posted: Monday 23 November 2015 17:02
by jvdz
This will never be true! or will require magic
Did you want this:?
Code: Select all
if ((h >= 13 or h <= 6) and uservariables[xmas_var] == 1 and otherdevices[xmas_lights] == 'Off')
Jos
Re: LUA if date between a and b, and time between c and d?
Posted: Wednesday 25 November 2015 8:45
by thorbj
Great, it looks like that did the trick! Thank you!
Re: LUA if date between a and b, and time between c and d?
Posted: Sunday 24 January 2016 9:57
by Derik
Is there someone that can help me with this script?
Do i need 4 dunmmy switches? lente zomer etc.
What is that "isdonker"decive?
How is this script runnng? crontab?