compare time from fixing time..

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

Moderator: leecollings

Post Reply
OedzesG
Posts: 106
Joined: Monday 11 March 2019 0:14
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

compare time from fixing time..

Post by OedzesG »

hallo,

here a noob question again :D

for a script i needed to know how manny minutes are past since a fixed time..

for example:

fix time is: '15:00'
now its 15:30

so the number 30 is what i need..

this dosnt work

Code: Select all

dz.time.compare('15:00:00').minutes
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: compare time from fixing time..

Post by waaren »

OedzesG wrote: Friday 14 February 2020 15:33 this doesn't work

Code: Select all

dz.time.compare('15:00:00').minutes
The compare method require two dzVents time objects.

Code: Select all

return 
{
	on = 
	{
		timer = 
		{
			'every minute',
		},
	},
	
	execute = function(dz)
	    local myTime = '15:00:00'
	    local someTime = dz.time.makeTime(dz.time.rawDate .. ' ' .. myTime) -- this will create the time object someTime
	
	    -- compare require two dzVents time objects (first one here is dz.time, second one is someTime )  
		dz.log('Delta time between  ' .. dz.time.rawTime .. ' and ' .. someTime.rawTime .. ' is ' .. dz.time.compare(someTime).minutes .. ' minutes.', dz.LOG_FORCE)
	end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
OedzesG
Posts: 106
Joined: Monday 11 March 2019 0:14
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: compare time from fixing time..

Post by OedzesG »

The compare method require two dzVents time objects.
thanks again waaren! :D

is there also a way to get the current minute in this week, month and year?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: compare time from fixing time..

Post by waaren »

OedzesG wrote: Friday 14 February 2020 21:38 is there also a way to get the current minute in this week, month and year?
you could try this

Code: Select all

return 
{
    on = 
    {
        timer = 
        {
            'every minute',
        }
    },

    execute = function(dz)
        local day = 86400 -- (24 * 60 * 60)

        local function makeTime(date)
            return dz.time.makeTime(date .. ' 00:00:00')
        end 

        local weekStart = makeTime(os.date("%Y-%m-%d",os.time()- ( dz.time.wday - 1 ) * day))
        local dayStart = makeTime(dz.time.rawDate)
        local monthStart = makeTime(dz.time.year .. '-' .. dz.time.month ..'-01')
        local yearStart = makeTime(dz.time.year .. '-01-01')

        dz.log('Minutes today: ' .. dayStart.minutesAgo, dz.LOG_FORCE)
        dz.log('Minutes this week: ' .. weekStart.minutesAgo, dz.LOG_FORCE)
        dz.log('Minutes this month: ' .. monthStart.minutesAgo, dz.LOG_FORCE)
        dz.log('Minutes this year: ' .. yearStart.minutesAgo, dz.LOG_FORCE)
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
OedzesG
Posts: 106
Joined: Monday 11 March 2019 0:14
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: compare time from fixing time..

Post by OedzesG »

Thnx waaren! thats exactly what i needed..
User avatar
andrehj
Posts: 45
Joined: Sunday 06 January 2019 14:26
Target OS: -
Domoticz version:
Contact:

Re: compare time from fixing time..

Post by andrehj »

(Sorry to revive an old topic, but I couldn't find anything more recent.)
waaren wrote: Friday 14 February 2020 16:47 The compare method require two dzVents time objects.

Code: Select all

return 
{
	on = 
	{
		timer = 
		{
			'every minute',
		},
	},
	
	execute = function(dz)
	    local myTime = '15:00:00'
	    local someTime = dz.time.makeTime(dz.time.rawDate .. ' ' .. myTime) -- this will create the time object someTime
	
	    -- compare require two dzVents time objects (first one here is dz.time, second one is someTime )  
		dz.log('Delta time between  ' .. dz.time.rawTime .. ' and ' .. someTime.rawTime .. ' is ' .. dz.time.compare(someTime).minutes .. ' minutes.', dz.LOG_FORCE)
	end
}
After a lot of studying, I still fail to understand how the Compare function should be used.
As far as I understand, this "dz.time.compare(someTime).minutes" only compares the current time with the time stored in the time object "someTime" and returns that difference in a number of minutes.

But how can one compare two time objects named for instance "Time1" and "Time2"? :?:

The manual doesn't help either, see https://wiki.domoticz.com/DzVents:_next ... ime_object

Code: Select all

    -- compare two times
    print(domoticz.time.compare(myDevice.lastUpdate).secs))
It says it compares two times, but all it does is compare the current time with another time....
User avatar
boum
Posts: 135
Joined: Friday 18 January 2019 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: France
Contact:

Re: compare time from fixing time..

Post by boum »

andrehj wrote: But how can one compare two time objects named for instance "Time1" and "Time2"? :?:
have you just tried:

Code: Select all

    -- compare two times
    print(Time1.compare(Time2).minutes)
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests