How to create a scheduled job in lua?

Moderator: leecollings

Post Reply
p0puptoast
Posts: 7
Joined: Saturday 08 October 2016 11:37
Target OS: Linux
Domoticz version:
Contact:

How to create a scheduled job in lua?

Post 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
JustME125
Posts: 31
Joined: Monday 27 April 2015 16:19
Target OS: Raspberry Pi / ODroid
Domoticz version: 2.4547
Location: The Netherlands
Contact:

Re: How to create a scheduled job in lua?

Post by JustME125 »

Have a look at dZvents, works great for me.

Verstuurd vanaf mijn HTC One_M8 met Tapatalk
p0puptoast
Posts: 7
Joined: Saturday 08 October 2016 11:37
Target OS: Linux
Domoticz version:
Contact:

Re: How to create a scheduled job in lua?

Post 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
JustME125
Posts: 31
Joined: Monday 27 April 2015 16:19
Target OS: Raspberry Pi / ODroid
Domoticz version: 2.4547
Location: The Netherlands
Contact:

Re: How to create a scheduled job in lua?

Post by JustME125 »

What was your issue? Mine works like a charm

Verstuurd vanaf mijn HTC One_M8 met Tapatalk
p0puptoast
Posts: 7
Joined: Saturday 08 October 2016 11:37
Target OS: Linux
Domoticz version:
Contact:

Re: How to create a scheduled job in lua?

Post by p0puptoast »

JustME125 wrote:What was your issue? Mine works like a charm
See viewtopic.php?f=23&t=13646&p=98626#p98626.
p0puptoast
Posts: 7
Joined: Saturday 08 October 2016 11:37
Target OS: Linux
Domoticz version:
Contact:

Re: How to create a scheduled job in lua?

Post 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.
JustME125
Posts: 31
Joined: Monday 27 April 2015 16:19
Target OS: Raspberry Pi / ODroid
Domoticz version: 2.4547
Location: The Netherlands
Contact:

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

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

Who is online

Users browsing this forum: No registered users and 1 guest