LUA if date between a and b, and time between c and d?
Moderator: leecollings
-
- Posts: 113
- Joined: Thursday 20 November 2014 22:11
- Target OS: Raspberry Pi / ODroid
- Domoticz version: beta
- Location: Norway
- Contact:
LUA if date between a and b, and time between c and d?
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!
Thanks!
2xRaspberry Pi Model 2 w/RaZberry z-wave chip (master/slave)|Fibaro Wall Plug|Fibaro Universal Dimmer 500W|Aeon Labs Multisensor|RFXtrx433E|Nexa WMR-1000|Nexa Pe-3|Nexa Remote|Nexa LGDR3500|Lightify Gateway|Lightify RGBW bulb
-
- Posts: 890
- Joined: Tuesday 30 September 2014 8:49
- Target OS: Linux
- Domoticz version: beta
- Location: The Netherlands
- Contact:
Re: LUA if date between a and b, and time between c and d?
Does this help you: Bitbucket: script_device_season.lua ?
I am not active on this forum anymore.
-
- Posts: 485
- Joined: Thursday 17 September 2015 10:13
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
Re: LUA if date between a and b, and time between c and d?
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 ...
It's just a start. I leave it up to You to figure out how to complete the script 
Code: Select all
local t = os.date("*t")
{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

-
- Posts: 113
- Joined: Thursday 20 November 2014 22:11
- Target OS: Raspberry Pi / ODroid
- Domoticz version: beta
- Location: Norway
- Contact:
Re: LUA if date between a and b, and time between c and d?
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?
Thanks!
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
Last edited by thorbj on Monday 23 November 2015 14:25, edited 1 time in total.
2xRaspberry Pi Model 2 w/RaZberry z-wave chip (master/slave)|Fibaro Wall Plug|Fibaro Universal Dimmer 500W|Aeon Labs Multisensor|RFXtrx433E|Nexa WMR-1000|Nexa Pe-3|Nexa Remote|Nexa LGDR3500|Lightify Gateway|Lightify RGBW bulb
-
- Posts: 890
- Joined: Tuesday 30 September 2014 8:49
- Target OS: Linux
- Domoticz version: beta
- Location: The Netherlands
- Contact:
Re: LUA if date between a and b, and time between c and d?
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?

I am not active on this forum anymore.
-
- Posts: 113
- Joined: Thursday 20 November 2014 22:11
- Target OS: Raspberry Pi / ODroid
- Domoticz version: beta
- Location: Norway
- Contact:
Re: LUA if date between a and b, and time between c and d?
Sorry, I've removed the print statementsThinkPad 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?

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
2xRaspberry Pi Model 2 w/RaZberry z-wave chip (master/slave)|Fibaro Wall Plug|Fibaro Universal Dimmer 500W|Aeon Labs Multisensor|RFXtrx433E|Nexa WMR-1000|Nexa Pe-3|Nexa Remote|Nexa LGDR3500|Lightify Gateway|Lightify RGBW bulb
- jvdz
- Posts: 2328
- Joined: Tuesday 30 December 2014 19:25
- Target OS: Raspberry Pi / ODroid
- Domoticz version: 4.107
- Location: Netherlands
- Contact:
Re: LUA if date between a and b, and time between c and d?
This will never be true! or will require magic
Did you want this:?
Jos

Code: Select all
h >= 13 and h <= 6
Code: Select all
if ((h >= 13 or h <= 6) and uservariables[xmas_var] == 1 and otherdevices[xmas_lights] == 'Off')
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
-
- Posts: 113
- Joined: Thursday 20 November 2014 22:11
- Target OS: Raspberry Pi / ODroid
- Domoticz version: beta
- Location: Norway
- Contact:
Re: LUA if date between a and b, and time between c and d?
Great, it looks like that did the trick! Thank you!
2xRaspberry Pi Model 2 w/RaZberry z-wave chip (master/slave)|Fibaro Wall Plug|Fibaro Universal Dimmer 500W|Aeon Labs Multisensor|RFXtrx433E|Nexa WMR-1000|Nexa Pe-3|Nexa Remote|Nexa LGDR3500|Lightify Gateway|Lightify RGBW bulb
-
- Posts: 1602
- Joined: Friday 18 October 2013 23:33
- Target OS: Raspberry Pi / ODroid
- Domoticz version: BETA
- Location: Arnhem/Nijmegen Nederland
- Contact:
Re: LUA if date between a and b, and time between c and d?
ThinkPad wrote:Does this help you: Bitbucket: script_device_season.lua ?
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?
Xu4: Beta Extreme antenna RFXcomE,WU Fi Ping ip P1 Gen5 PVOutput Harmony HUE SolarmanPv OTG Winddelen Alive ESP Buienradar MySensors WOL Winddelen counting RPi: Beta SMAspot RFlinkTest Domoticz ...Different backups
Who is online
Users browsing this forum: No registered users and 1 guest