Little Lua Help

hmjgriffon

Known around here
Joined
Mar 30, 2014
Messages
3,386
Reaction score
979
Location
North Florida
So I literally started using lua probably less than a week ago and pieced together some pretty simple ones. This one basically activated an armed home mode with my system. I am trying to figure out how to make it run only on Sunday through Thursday, its working fine on weekdays so I know the script is okay. Doing things with time and dates are a little confusing and I'm not a programmer, any help is appreciated.

time = os.date("*t")
weekday = os.date("%A")

commandArray = {}
if (time.hour == 22) and (time.min == 30) and (weekday ~= 'Weekend') then
commandArray['Armed Home'] = "On"

end
return commandArray


FYI, I'm using this with domoticz.
 

nayr

IPCT Contributor
Joined
Jul 16, 2014
Messages
9,329
Reaction score
5,325
Location
Denver, CO
use code tags bro,
Screenshot 2017-02-07 19.59.22.png

then read: Programming in Lua : 22.1


Code:
time = os.date("*t")
day = os.date("%w") -- 0 = Sunday / 6 = Saturday

commandArray = {}
if (day >= 4) or (day == 0) then
   if (time.hour == 22) and (time.min == 30) then 
       commandArray['Armed Home'] = "On"
   end
end
return commandArray
thats untested, but give it a try.
 

hmjgriffon

Known around here
Joined
Mar 30, 2014
Messages
3,386
Reaction score
979
Location
North Florida
use code tags bro,
View attachment 14887

then read: Programming in Lua : 22.1


Code:
time = os.date("*t")
day = os.date("%w") -- 0 = Sunday / 6 = Saturday

commandArray = {}
if (day >= 4) or (day == 0) then
   if (time.hour == 22) and (time.min == 30) then 
       commandArray['Armed Home'] = "On"
   end
end
return commandArray
thats untested, but give it a try.
I KNEW there was a way to refer to days by number but I googled all over and read stuff but couldn't figure it out lol thanks I will try this and report back
 

hmjgriffon

Known around here
Joined
Mar 30, 2014
Messages
3,386
Reaction score
979
Location
North Florida
got a bunch of:

2017-02-08 18:59:00.181 Error: EventSystem: in /home/pi/domoticz/scripts/lua/script_time_autoarmedhome.lua: /home/pi/domoticz/scripts/lua/script_time_autoarmedhome.lua:5: attempt to compare number with string

in my log lol.
 

nayr

IPCT Contributor
Joined
Jul 16, 2014
Messages
9,329
Reaction score
5,325
Location
Denver, CO
ah, try changing:
Code:
day = tonumber(os.date("%w")) -- 0 = Sunday / 6 = Saturday
it looks like os.date is returning a string, needa convert it to an interger.. in machine: "1" !== 1

Programming in Lua : 2.4
 

hmjgriffon

Known around here
Joined
Mar 30, 2014
Messages
3,386
Reaction score
979
Location
North Florida
ah, try changing:
Code:
day = tonumber(os.date("%w")) -- 0 = Sunday / 6 = Saturday
it looks like os.date is returning a string, needa convert it to an interger.. in machine: "1" !== 1
I need to read up on that tonumber thing.
 

hmjgriffon

Known around here
Joined
Mar 30, 2014
Messages
3,386
Reaction score
979
Location
North Florida
that's my new reading site, so I had to make ONE tweak.

Code:
time = os.date("*t")
day = tonumber(os.date("%w")) -- 0 = Sunday / 6 = Saturday

commandArray = {}
if (day <= 4) or (day == 0) then
   if (time.hour == 22) and (time.min == 30) then
       commandArray['Armed Home'] = "On"
   end
end
return commandArray
had to make it <4. You da man.
 

nayr

IPCT Contributor
Joined
Jul 16, 2014
Messages
9,329
Reaction score
5,325
Location
Denver, CO
lua scripts in domoticz are ran every min, so after editing any lua script its wise to open the log and wait for the next min to roll over and check for errors.. just to make sure you dont have any major errors blowing up domoticz; one bad quote can fuck up all scripts and drive u bonkers.
 

hmjgriffon

Known around here
Joined
Mar 30, 2014
Messages
3,386
Reaction score
979
Location
North Florida
lua scripts in domoticz are ran every min, so after editing any lua script its wise to open the log and wait for the next min to roll over and check for errors.. just to make sure you dont have any major errors blowing up domoticz; one bad quote can fuck up all scripts and drive u bonkers.
so far so good, right now I've got it set to email me on error, but of course the true test will see if it runs on every day I expect it to lol.
 
Top