Page 1 of 1

Lua Time scripts

Posted: Saturday 12 March 2016 0:21
by CronoS
Hi,

A quick question. I am building my first Lua script for my Lux sensor, the script is running fine so far. It does what I wanted to do. But I now I want to do something which involves time .

I have created a time script. Basically when the scripts executes, I want to place the current OS time in a user variabele, preferable in the format HH:MM.. Now, when the script executes again (one minute further); I want to read the user variabele, calculate the new time and I want to calculate the difference on both the times in minutes. If the result is 10 minutes or more, for example, then I want to do something in the script.

I like to use this time setting on my Lux sensor / lua script. If the lux sensor goes down because of a cloud, I don't want that Domoticz will close my sunscreen immediately because of the low lux.. I want to evaluate the Lux sensor 10 times or so before I close my sunscreen outside. At this stage I am able to evaluate the sensor ten times before it closes, but I want to time frame it basically.

I know how the variables works, but I really don't get the time settings in Lua. I simply don't get the format and how to calculate with it. I was able to get a String value in HH:MM and place this in a variable, but then I don't know how to calculate the difference because it is a string?

Can someone give me a push in the right direction, because I don't see it :)

Re: Lua Time scripts

Posted: Saturday 12 March 2016 5:41
by Dnpwwo
Check the wiki, its got lots of useful stuff http://www.domoticz.com/wiki/LUA_comman ... difference

I wouldn't use a string, just work off the number of seconds otherwise you have to handle special cases around midnight and the like.

And do something like:

Code: Select all

waitTime = 300           -- 5 minutes, time to leave devices on without movement
commandArray = {}
for deviceName,deviceValue in pairs(otherdevices) do
    if (timedifference(otherdevices_lastupdate[deviceName]) > waitTime) then
    	-- do stuff
    end
end

Re: Lua Time scripts

Posted: Saturday 12 March 2016 7:38
by CronoS
I have seen all the wiki's, but I still don't get it :) I have placed this in the script:

Code: Select all

		
waitTime = 300
if (timedifference(uservariables_lastupdate[Lux_schakelaar]) >  waitTime) then
	print ("Groter dan 5")
end	
But I get an error that timedifference is zero .. so that didn't work ?
In you're example; you placed a loop with Do.. I guess I don't need that? I want to evaluate variables immediately basically?

Re: Lua Time scripts

Posted: Saturday 12 March 2016 8:00
by Dnpwwo
Well you need to include the timedifference function in your script:

Code: Select all

function timedifference (s)
  ret = os.difftime (os.time(), os.time{year=string.sub(s, 1, 4), month=string.sub(s, 6, 7), day=string.sub(s, 9, 10), hour=string.sub(s, 12, 13), min=string.sub(s, 15, 16), sec=string.sub(s, 18, 19)})  return ret
end

Re: Lua Time scripts

Posted: Saturday 12 March 2016 8:06
by CronoS
Thanks for the effort.. I am getting there.. :)

I have placed the function in top of the Lua script

Now I got this error on line 4, which is the function line:
4: bad argument #1 to 'sub' (string expected, got nil)

Was I able to copy you're code in the script ?

Re: Lua Time scripts

Posted: Saturday 12 March 2016 9:06
by Dnpwwo
You were.

This error means that the value 'nil' was passed into the function which means that:

Code: Select all

uservariables_lastupdate[Lux_schakelaar]
returned 'nil' (which means 'not found' in this case). if Lux_schakelaar is the name of your device you need to use

Code: Select all

uservariables_lastupdate["Lux_schakelaar"]

Re: Lua Time scripts

Posted: Saturday 12 March 2016 13:00
by CronoS
Thanks ... It was indeed this typo :) Still can't translate this function, but it works :) I have more tries and testing this weekend, but my script seems to work OK for now :)

Re: Lua Time scripts

Posted: Saturday 12 March 2016 21:06
by dannybloe
<shameless-plug>
Or you use this: dzVents :mrgreen:
It does all the nasty time stuff for you. Couple of lines of code and your done.
</shameless-plug>