calculations in time?

Post Reply
renerene
Posts: 149
Joined: Wednesday 03 August 2016 11:36
Target OS: -
Domoticz version:
Contact:

calculations in time?

Post by renerene » Saturday 03 February 2018 10:15

How to best calculate with time values?

I want my room temperature to be 20 degrees at 8:00 AM. The start time of the boiler depends on in- and outside temperature, so starttime=targettime minus heating time. What is the best way to calculate 8:00 AM - 2 hours - 14 minutes = ?

User avatar
waaren
Posts: 1350
Joined: Tuesday 03 January 2017 15:18
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Netherlands
Contact:

Re: calculations in time?

Post by waaren » Saturday 03 February 2018 12:33

renerene wrote:
Saturday 03 February 2018 10:15
How to best calculate with time values?

I want my room temperature to be 20 degrees at 8:00 AM. The start time of the boiler depends on in- and outside temperature, so starttime=targettime minus heating time. What is the best way to calculate 8:00 AM - 2 hours - 14 minutes = ?
There will always be better ways but one solution in LUA is:

Code: Select all

 t = os.date('*t')
 
 local SetHour   = 8
 local HourDelta = 2
 local SetMinute = 60
 local MinuteDelta = 14
 local SetMinute = SetMinute - MinuteDelta
 if SetMinute < 60 then
	HourDelta = HourDelta + 1
 end
 Sethour = SetHour - HourDelta
 local TargetTime = (t.hour - HourDelta) * 3600 + (t.min - MinuteDelta) * 60
 local SetTime = Sethour * 3600 

 if ( TargetTime - SetTime ) > 0 then
	print("***********************************")
	print("Target time in seconds: " .. tostring(TargetTime),"Set time in seconds: " .. tostring(SetTime),"Delta in seconds: " .. tostring(TargetTime - SetTime) )
	print("***********************************")
	print("It's Time to do something....")
end	
 
Raspberry (debian stretch via berryboot on Synology DS916+) , Domoticz (almost) latest Beta, , dzVents 2.6, RFLink, RFXtrx433e, P1, Youless, Harmony, Hue, Yeelight, Xiaomi, HomeWizard, Zwave, Amazon echo

renerene
Posts: 149
Joined: Wednesday 03 August 2016 11:36
Target OS: -
Domoticz version:
Contact:

Re: calculations in time?

Post by renerene » Saturday 03 February 2018 14:53

I was hoping for more built in routines.
It becomes complex if you want to calculate larger time zones, end of months, etc.

d3nn1s
Posts: 3
Joined: Monday 05 February 2018 0:47
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: calculations in time?

Post by d3nn1s » Saturday 10 February 2018 21:35

Hi I've had a similar problem and I find it frustrading that there seems to be no built in routines to easily calculate with time. I thought e.g. 07:35-20minutes is a simple thing to but I had to do a workaround which makes this simple task seem complicated. I have to say that I'm a LUA beginner and I'm not good at coding.

I wanted to substract a number of minutes from my alarm time, in order to begin the wake up process. I solved it the following way:

Code: Select all

-- uservariables["Wecker_Uhrzeit"] is the alarm time which I set from my smart phone. It's a user time variable holding a string in the format HH:MM.
-- Dimmer_Dauer is the number of minutes I want the wake up process to start before my alarm rings.

commandArray = {}
local Dimmer_Dauer=25 -- Set dimming duration to 25 Minutes
local Hour=string.sub(uservariables["Wecker_Uhrzeit"],1,2); -- Here it gets funny: The string holding the alarm time has to be decomposed into hours and minutes. Here I extract the hours by taking the first two etrys of the string into the local variable Hour
local Minute=string.sub(uservariables["Wecker_Uhrzeit"],4,5);  -- Here I extract the minutes by taking the third and fourth etry of the string into the local variable Minute.
local Wecker_Sekunden=os.time{year=1970, month=1, day=1, hour=tonumber(Hour), min=tonumber(Minute)}; --here there strigs with the extracted hours and minutes have to converted into numbers and then into seconds, which I find really funny. This is done by the os.time function. The year, month and day don't matter at all, because after the following math I only extract the hour and minute again. They have to be defined however.
local WakeUp_Sekunden=Wecker_Sekunden-Dimmer_Dauer*60; -- This is the part which does what I intentionally wanted to do. Substracting the duration of the dimming from my alarm time. The local variable WakeUp_Sekunden now holds the desired beginning of the wake up process as a number of seconds from the date defined in the os.time function above.
local WakeUp_Beginn=tostring(os.date("%H:%M",WakeUp_Sekunden)); --the os.date function transforms the desired wake up time from seconds back to a string in the format HH:MM. This is done by choosing only hours and minutes through the %H, the : and the %M.
commandArray['Variable:WakeUp_Beginn']=WakeUp_Beginn; --This updates my user variable which blockly uses to start the wake up process.
return commandArray

-- google about os.time and os.date if you have trouble understanding what these functions do.
I find this method really stupid but I wasn't able to find an easier way. Hope it helps.

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests