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 = ?
calculations in time?
Moderator: leecollings
- waaren
- Posts: 6028
- Joined: Tuesday 03 January 2017 14:18
- Target OS: Linux
- Domoticz version: Beta
- Location: Netherlands
- Contact:
Re: calculations in time?
There will always be better ways but one solution in LUA is:renerene wrote: ↑Saturday 03 February 2018 9: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 = ?
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
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Re: calculations in time?
I was hoping for more built in routines.
It becomes complex if you want to calculate larger time zones, end of months, etc.
It becomes complex if you want to calculate larger time zones, end of months, etc.
-
- Posts: 3
- Joined: Sunday 04 February 2018 23:47
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Contact:
Re: calculations in time?
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:
I find this method really stupid but I wasn't able to find an easier way. Hope it helps.
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.
Who is online
Users browsing this forum: No registered users and 1 guest