Page 1 of 1

How to create a scheduled job in lua?

Posted: Monday 10 October 2016 13:34
by p0puptoast
Domoticz v3.57. Linux ARM. Debian wheezy.

I want to change the mode setting on a pair of thermostats at specific times in the day.

I've looked but cannot see any examples of how to make a setting at a specific time. I am using the in-built editor and saving as a time script. Both the following methods for retrieving the current time have failed for me.

Code: Select all

time = os.date("*t") 
and

Code: Select all

time = os.time()
I am debugging by printing to the log and can see that in the first instance that time is nil since the comparison to a number I have done produced "attempt to compare number with nil".

If I use the latter code (as in the full listing below) I see the error

"Error: EventSystem: in BedroomsEnergySave_time: [string "..."]:17: attempt to index global 'timenow' (a number value)"

Full code is as follows.

Code: Select all

print ("Time based event fired")

commandArray = {}
 
timenow =  os.time() 

print (timenow.hours)
print (" hours")
print (timenow.minutes)
print (" minutes")
 
if (timenow.hours>9) then
    print ("It is after 0900 GMT")
end 
 
for deviceName,deviceValue in pairs(otherdevices) do
    if (timenow.hour == 19 and timenow.minutes == 1) then
        if (deviceName=='T stat mode') then
            if deviceValue == 'Comfort' then
                print("T stat mode is Comfort")
                commandArray['T stat mode'] = 'Energy Saving'
                print("T stat mode switched to Energy Saving")
            end
        elseif  (deviceName=='L stat mode') then
            if deviceValue == 'Comfort' then
                print("L stat mode is Comfort")
                commandArray['L stat mode'] = 'Energy Saving'
                print("L stat mode switched to Energy Saving")
            end
        end
   elseif (timenow.hour == 8 and timenow.minutes == 35) then
       if (deviceName=='T stat mode') then
            if deviceValue == 'Energy Saving' then
                print("T stat mode is Energy Saving")
                commandArray['T stat mode'] = 'Comfort'
                print("T stat mode switched to Comfort")
            end
        elseif  (deviceName=='L stat mode') then
            if deviceValue == 'Energy Saving' then
                print("L stat mode is Energy Saving")
                commandArray['L stat mode'] = 'Comfort'
                print("L stat mode switched to Comfort")
            end
        end
   end
end


return commandArray

Re: How to create a scheduled job in lua?

Posted: Monday 10 October 2016 14:11
by JustME125
Have a look at dZvents, works great for me.

Verstuurd vanaf mijn HTC One_M8 met Tapatalk

Re: How to create a scheduled job in lua?

Posted: Monday 10 October 2016 14:23
by p0puptoast
JustME125 wrote:Have a look at dZvents, works great for me.
Thanks for the tip but I had problems getting dzVents to run: nobody came back in the forum with my query on that.

Dave

Re: How to create a scheduled job in lua?

Posted: Monday 10 October 2016 15:17
by JustME125
What was your issue? Mine works like a charm

Verstuurd vanaf mijn HTC One_M8 met Tapatalk

Re: How to create a scheduled job in lua?

Posted: Monday 10 October 2016 17:46
by p0puptoast
JustME125 wrote:What was your issue? Mine works like a charm
See viewtopic.php?f=23&t=13646&p=98626#p98626.

Re: How to create a scheduled job in lua?

Posted: Tuesday 11 October 2016 0:59
by p0puptoast
I looked through domoticz/lua/doc/manual.html and http://www.cplusplus.com/reference/ctime/strftime/ and altered the starting section to

Code: Select all

timehour =  os.date("%H") 
timehour_n = tonumber(timehour)
timemin = os.date("%M")
timemin_n = tonumber(timemin)
Subsequently the time test is

Code: Select all

if (timehour_n == 22 and timemin_n == 29) then
For instance that would be true at 22:29.

I'm surprised a scheduling type of script does not appear in the examples yet.

Re: RE: Re: How to create a scheduled job in lua?

Posted: Tuesday 11 October 2016 2:14
by JustME125
p0puptoast wrote:
JustME125 wrote:What was your issue? Mine works like a charm
See viewtopic.php?f=23&t=13646&p=98626#p98626.
Just replied there

Verstuurd vanaf mijn HTC One_M8 met Tapatalk