Little Lua Help

hmjgriffon

Known around here
Mar 30, 2014
3,369
976
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.
 
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.
 
  • Like
Reactions: hmjgriffon
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
 
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.
 
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
 
  • Like
Reactions: hmjgriffon
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.
 
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.