Convert text time to minutes after midnight

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
paul402
Posts: 96
Joined: Monday 23 May 2016 23:07
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Convert text time to minutes after midnight

Post 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?
User avatar
waltervl
Posts: 5890
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Convert text time to minutes after midnight

Post 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
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
paul402
Posts: 96
Joined: Monday 23 May 2016 23:07
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Convert text time to minutes after midnight

Post 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.
paul402
Posts: 96
Joined: Monday 23 May 2016 23:07
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Convert text time to minutes after midnight

Post 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.
User avatar
habahabahaba
Posts: 233
Joined: Saturday 18 March 2023 14:44
Target OS: Windows
Domoticz version: 2024.4
Contact:

Re: Convert text time to minutes after midnight

Post 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)
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest