Page 1 of 1
Convert text time to minutes after midnight
Posted: Tuesday 28 May 2024 16:32
by paul402
I have decided to try and integrate my, working for years, Python functionality into Dzvents but I have a problem with my conversion.
The functionality that I want to replicate is
time1 say '08:00'
time2 say '09:00'
if sunrise is < time1 then sunrise==time1
if sunrise is > time 2 then sunrise==time2
if now=sunrise then move the shutter
How do I convert a text time value into minutes after midnight? The help that I looked at suggested building a time with the year, month and day based on 'now'.
Is this really necessary or is there an easier way?
Re: Convert text time to minutes after midnight
Posted: Tuesday 28 May 2024 17:10
by waltervl
Something like this?
8.00 is 480 minutes after midnight
9.00 is 540 minutes after midnight
shuttertime = domoticz.time.sunriseInMinutes
If domoticz.time.sunriseInMinutes < 480 then shuttertime = 480
If domoticz.time.sunriseInMinutes > 540 then shuttertime = 540
If domoticz.time.minutesSinceMidnight == shuttertime then move the shutter
Re: Convert text time to minutes after midnight
Posted: Tuesday 28 May 2024 19:20
by paul402
Thanks for the feedback waltervl but I want to keep the original data in '09:00' hours/minutes format because it is easier to understand and to convert the variables to minutes after midnight. I just cannot see a simple way to do it.
I have no problem with the logic of the if statements.
May be I will use DZvents to call my Python and return a value. Another rabbit hole to go down when you learn another language.
Re: Convert text time to minutes after midnight
Posted: Tuesday 28 May 2024 19:38
by paul402
So I think that I have solved part of the problem.
Code: Select all
local sometime=domoticz.utils.stringSplit(t1,':',convertNumber)
domoticz.log('stringsplit ' .. tostring(sometime[1]) .. tostring(sometime[2]) ')
local t1minutes=sometime[1]*60+sometime[2]
domoticz.log('t1 minutes past midnight' .. tostring(t1minutes))
That gives me minutes past midnight for my variable.
Re: Convert text time to minutes after midnight
Posted: Tuesday 28 May 2024 22:09
by habahabahaba
Code: Select all
local currentDate = os.time()
local setTime = '08:00'
local time1 = os.date("%Y-%m-%d "..setTime, currentDate)
local time1toStamp = domoticz.time.dateToTimestamp(time1)
local minutesToSunrise = domoticz.time.sunriseInMinutes
local time2 = os.date("%Y-%m-%d 00:00", currentDate)
local CurrentSunriseTimeStamp = domoticz.time.dateToTimestamp(time2) + minutesToSunrise * 60
local todaySunriseTime = domoticz.time.timestampToDate(CurrentSunriseTimeStamp,'%Y-%m-%d time')
if time1toStamp > CurrentSunriseTimeStamp then
domoticz.log('Set time is after sunrise', domoticz.LOG_ERROR)
else
domoticz.log('Set time is befor sunrise', domoticz.LOG_ERROR)
end
domoticz.log('Minutes to sunrise: '.. minutesToSunrise, domoticz.LOG_ERROR)
domoticz.log('Time1: '.. time1, domoticz.LOG_ERROR)
domoticz.log('time1toStamp: '.. time1toStamp, domoticz.LOG_ERROR)
domoticz.log('time2: '.. time2, domoticz.LOG_ERROR)
domoticz.log('CurrentSunriseTimeStamp: '.. CurrentSunriseTimeStamp, domoticz.LOG_ERROR)
domoticz.log('todaySunriseTime: '.. todaySunriseTime, domoticz.LOG_ERROR)