How to use time in LUA?

Topics (not sure which fora)
when not sure where to post, post here and mods will move it to right forum.

Moderators: leecollings, remb0

Post Reply
Bram81
Posts: 22
Joined: Saturday 02 May 2015 11:00
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.9700
Location: Utrecht, NL
Contact:

How to use time in LUA?

Post by Bram81 »

Hey,

Maybe this is a stupid question, but I haven't been able to find the answer on this forum nor on the wiki. I'm writing a lua script that has to run with different actions on a device change depending on the time of day. This works fine:

Code: Select all

 time = os.date("*t")
 commandArray = {}
   if (devicechanged['Slot Voordeur'] == 'Closed' and time.hour >= 12 and time.hour <=17) then
    commandArray['Spots Woonkamer']='On' 
But this way it's checking for entire hours. How could I for example use the time between 09.15 and 09.44?
Using

Code: Select all

time = (os.date(“%H:%M”))

gives me an error unexpected symbol near character 226.

I'm pretty new to lua, but I hope someone can help me out..
User avatar
jvdz
Posts: 2334
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: How to use time in LUA?

Post by jvdz »

This is an example with the function I use:

Code: Select all

function timebetween(s,e)
	timenow = os.date("*t")
	year = timenow.year
	month = timenow.month
	day = timenow.day
	s = s .. ":00"  -- add seconds in case only hh:mm is supplied
	e = e .. ":00"
	shour = string.sub(s, 1, 2)
	sminutes = string.sub(s, 4, 5)
	sseconds = string.sub(s, 7, 8)
	ehour = string.sub(e, 1, 2)
	eminutes = string.sub(e, 4, 5)
	eseconds = string.sub(e, 7, 8)
	t1 = os.time()
	t2 = os.time{year=year, month=month, day=day, hour=shour, min=sminutes, sec=sseconds}
	t3 = os.time{year=year, month=month, day=day, hour=ehour, min=eminutes, sec=eseconds}
	sdifference = os.difftime (t1, t2)
	edifference = os.difftime (t1, t3)
	isbetween = false
	if sdifference >= 0 and edifference <= 0 then
		isbetween = true
	end
--~ 	print(" s:" .. s  .. "  e:" .. e .. "  sdifference:" .. sdifference.. "  edifference:" .. edifference)
	return isbetween
end


commandArray = {}

if timebetween("09:15:00","09:44:00") then
	--- perform action
end

return commandArray

Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
jannl
Posts: 673
Joined: Thursday 02 October 2014 6:36
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.2
Location: Geleen
Contact:

Re: How to use time in LUA?

Post by jannl »

You could create a time_in_minutes variable: time_in_minutes = time.minutes + time.hour * 60
if time_in_minutes >= 720 and time_in_minutes <= 1020 then

Could do the trick
Bram81
Posts: 22
Joined: Saturday 02 May 2015 11:00
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.9700
Location: Utrecht, NL
Contact:

Re: How to use time in LUA?

Post by Bram81 »

jvdz wrote:This is an example with the function I use:

Code: Select all

function timebetween(s,e)
	timenow = os.date("*t")
	year = timenow.year
	month = timenow.month
	day = timenow.day
	s = s .. ":00"  -- add seconds in case only hh:mm is supplied
	e = e .. ":00"
	shour = string.sub(s, 1, 2)
	sminutes = string.sub(s, 4, 5)
	sseconds = string.sub(s, 7, 8)
	ehour = string.sub(e, 1, 2)
	eminutes = string.sub(e, 4, 5)
	eseconds = string.sub(e, 7, 8)
	t1 = os.time()
	t2 = os.time{year=year, month=month, day=day, hour=shour, min=sminutes, sec=sseconds}
	t3 = os.time{year=year, month=month, day=day, hour=ehour, min=eminutes, sec=eseconds}
	sdifference = os.difftime (t1, t2)
	edifference = os.difftime (t1, t3)
	isbetween = false
	if sdifference >= 0 and edifference <= 0 then
		isbetween = true
	end
--~ 	print(" s:" .. s  .. "  e:" .. e .. "  sdifference:" .. sdifference.. "  edifference:" .. edifference)
	return isbetween
end


commandArray = {}

if timebetween("09:15:00","09:44:00") then
	--- perform action
end

return commandArray

Jos

Hi, thank you! Seems to be working now using this

Code: Select all

 commandArray = {}
 if (devicechanged['Slot Voordeur'] == 'Closed' and timebetween ("07:00:00","16:59:59"))then
        commandArray['Slapen']='On'
        commandArray['Verwarming Bespaarstand']='On'
The strange thing is that the first commandArray Slapen (which is a scene) is not executed, but the second one Verwarming Bespaarstand is.
Log shows no errors. Any idea on that one?
User avatar
jvdz
Posts: 2334
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: How to use time in LUA?

Post by jvdz »

The scene needs a little different command:

Code: Select all

commandArray['Scene:Slapen']='On'
Check here for all details: https://www.domoticz.com/wiki/Events

Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
Bram81
Posts: 22
Joined: Saturday 02 May 2015 11:00
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.9700
Location: Utrecht, NL
Contact:

Re: How to use time in LUA?

Post by Bram81 »

Ah, I could have guessed.. Thanks again, working like a charm now!
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest