LUA if date between a and b, and time between c and d?

Moderator: leecollings

Post Reply
thorbj
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?

Post 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!
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
ThinkPad
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?

Post by ThinkPad »

I am not active on this forum anymore.
BakSeeDaa
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?

Post by BakSeeDaa »

You can also try the os.date function.

Code: Select all

local t = os.date("*t")
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 :D
thorbj
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?

Post 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!
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
ThinkPad
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?

Post by ThinkPad »

I don't know (script is a bit hard to read due to the print statements :oops: ), 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.
thorbj
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?

Post by thorbj »

ThinkPad wrote:I don't know (script is a bit hard to read due to the print statements :oops: ), 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 :lol:
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
User avatar
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?

Post by jvdz »

This will never be true! or will require magic ;)

Code: Select all

h >= 13 and h <= 6
Did you want this:?

Code: Select all

if ((h >= 13 or h <= 6) and uservariables[xmas_var] == 1 and otherdevices[xmas_lights] == 'Off')
Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
thorbj
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?

Post by thorbj »

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
Derik
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?

Post by Derik »

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
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest