handling time in dzvents  [Solved]

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

Moderator: leecollings

Post Reply
ricorico94
Posts: 94
Joined: Monday 26 October 2015 10:41
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

handling time in dzvents

Post by ricorico94 »

Hi,

I have script based on timer (running every 3 minutes for instance) in which I'd like to use time elapsed since "something" happened, but I can't make it work. I have error messages or nothing returned.. I tried to follow wiki examples, but I didn't find exact similar context, so I' probably doing something wrong..
Here's the structure of my script (I removed most of the code for clarity):

Code: Select all

return
{
    on =
    {
        timer =
        {
            'every 3 minutes',                -- causes the script to be called every 3 minutes
        },
    },
    data = {
	   lastmeasure = {initial=0},
	   resetUPS= {initial=0},
	   lastReset = {initial=""}
	   -- some other variables declared here (removed to ease reading..
	},
    execute = function(dz)
        local UPSPlug = dz.devices(9999) -- device for UPS powercontrol 
	local unplugduration = 10 -- in minutes
	local Time = require('Time')
	-- some other variables declared here (removed to ease reading..
	
	-- some more code here
		if dz.data.resetUPS == 'Off' then
					dz.data.resetUPS='On'
					-- in following lines I want to store the time when I reset my UPS
					local now= Time()
					dz.data.lastReset = now
					UPSplug.switchOff()
					UPSplug.switchOn().afterMin(unplugduration)
		end
	dz.log('last reset occurred  :'..dz.data.lastReset.minutesAgo ..' minutes ago' , dz.LOG_INFO)
    end
}
	
In the log, it displays 'occurred 0 minutes ago' (even though the "if" block does not run every time the script is triggered).
I tried also to display the value of resetUPS with .getISO() like this:

Code: Select all

dz.log('last reset occurred at :'..dz.data.lastReset.getISO() ..' minutes ago' , dz.LOG_INFO) 
but in such case, I have an error message.
If I use this instead:

Code: Select all

dz.log('last reset occurred  at:'..now.getISO() ..' minutes ago' , dz.LOG_INFO)
I have no error message and it displays a date & time (but of course it's not the current time I want to display..)

So I probably misunderstood something on how to use dzvents objects in general or the Time in particular...
Could you help me ? Which code should I use to display a date & time variable ? and how to use the .minutesAgo ?
Thanks a lot,
Ricorico94
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: handling time in dzvents

Post by waaren »

ricorico94 wrote: Sunday 25 October 2020 15:52 So I probably misunderstood something on how to use dzvents objects in general or the Time in particular...
Could you help me ? Which code should I use to display a date & time variable ? and how to use the .minutesAgo ?
In dzVents lastUpdate is a complete time object. Although you can store that in persistent data, for this script it would be a bit too much.
Here you can just use os.time() which is defined as

Gets the system time. 'The returned value is a number. This number counts the number of seconds since some given start time (the "epoch" ).
example script
Spoiler: show

Code: Select all

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

    data = 
    {
	   lastReset = 
	   { 
	       initial = 0,
	   },
	},
    
    execute = function(dz)
		local deltaMinutes = dz.utils.round( ( ( os.time()  - dz.data.lastReset ) / 60 ), 1 ) -- os.time() 
	    dz.log('last execution was  '..deltaMinutes ..' minutes ago' , dz.LOG_INFO)
        dz.data.lastReset = os.time()
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
ricorico94
Posts: 94
Joined: Monday 26 October 2015 10:41
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: handling time in dzvents

Post by ricorico94 »

Hi,
Interesting workaround.
Could you clarify how to set lastReset to the date of the script using lastUpdate ? Would it be dz.data.lastReset=dz.lastUpdate ?

Also, if I use lastUpdate directly, I understand that it would send the time/date when the script ran before this call. But if my "lastReset" occurred several iterations of trigger in the past, I am forced to use a persistent variable, correct ?

And do you understand why my .getISO() call makes an error and why my .minutesAgo always returns 0 ?

br,
Ricorico94
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: handling time in dzvents

Post by waaren »

ricorico94 wrote: Sunday 25 October 2020 17:22 Could you clarify how to set lastReset to the date of the script using lastUpdate ? Would it be dz.data.lastReset=dz.lastUpdate ?
No, you should do dz.data.lastReset = dz.time.rawDateTime
Also, if I use lastUpdate directly, I understand that it would send the time/date when the script ran before this call. But if my "lastReset" occurred several iterations of trigger in the past, I am forced to use a persistent variable, correct ?
Yes that is the easiest way of doing it
And do you understand why my .getISO() call makes an error and why my .minutesAgo always returns 0 ?
Yes. getISO() is a function and functions cannot be store stored as persistent data.

I tried to explain it with comments in your code

Code: Select all

return
{
    on =
    {
        timer =
        {
            'every 3 minutes',                -- causes the script to be called every 3 minutes
        },
    },
    data = {
       lastmeasure = {initial=0},
       resetUPS= {initial=0},
       lastReset = {initial=""}
       -- some other variables declared here (removed to ease reading..
    },
    execute = function(dz)
        local UPSPlug = dz.devices(9999) -- device for UPS powercontrol
    local unplugduration = 10 -- in minutes
    local Time = require('Time')
    -- some other variables declared here (removed to ease reading..

    -- some more code here
        if dz.data.resetUPS == 'Off' then
                    dz.data.resetUPS='On'
                    -- in following lines I want to store the time when I reset my UPS
                    local now= Time() -- Time () is a dzVents time object with attributes and functions see below
                    dz.data.lastReset = now -- If you store an object in persistent data only attributes can be store not the functions
                    UPSplug.switchOff()
                    UPSplug.switchOn().afterMin(unplugduration)
        end

    -- Here you read the minutesAgo attribute of the lastreset persistent variable. The attributes are fixed values set at write time.
    -- at that Time minutes ago was 0 and that is what you will see.
    dz.log('last reset occurred  :'..dz.data.lastReset.minutesAgo ..' minutes ago' , dz.LOG_INFO)
    dz.log()

    end
}
a time object stored as persistent data
Spoiler: show

Code: Select all

-- Persistent Data
local multiRefObjects = {

} -- multiRefObjects
local obj1 = {
        ["lastReset"] = {
                ["ruleIsOnDate"] = nil --[[function]]
;
                ["month"] = 10;
                ["ruleIsOnDay"] = nil --[[function]]
;
                ["isdst"] = false;
                ["secondsAgo"] = 0;
                ["dDate"] = 1603647960;
                ["ruleIsAfterCivilTwilightEnd"] = nil --[[function]]
;
                ["milliseconds"] = 434;
                ["addHours"] = nil --[[function]]
;
                ["ruleIsBeforeCivilTwilightEnd"] = nil --[[function]]
;
                ["ruleIsBeforeSunrise"] = nil --[[function]]
;
                ["hoursAgo"] = 0;
                ["minutesSinceMidnight"] = 1126;
                ["minutes"] = 46;
                ["millisecondsAgo"] = 0;
                ["ruleIsAtNight"] = nil --[[function]]
;
                ["makeTime"] = nil --[[function]]
;
                ["secondsSinceMidnight"] = 67560;
                ["ruleIsAtCivilTwilightEnd"] = nil --[[function]]
;
                ["ruleMatchesBetweenRange"] = nil --[[function]]
;
                ["yday"] = 299;
                ["ruleIsInWeek"] = nil --[[function]]
;
                ["ruleIsAtSolarnoon"] = nil --[[function]]
;
                ["wday"] = 1;
                ["monthAbbrName"] = "oct";
                ["monthName"] = "October";
                ["min"] = 46;
                ["ruleIsAfterSolarnoon"] = nil --[[function]]
;
                ["addSeconds"] = nil --[[function]]
;
                ["sec"] = 0;
                ["addDays"] = nil --[[function]]
;
                ["ruleIsBeforeSolarnoon"] = nil --[[function]]
;
                ["rawTime"] = "18:46:00";
                ["milliSeconds"] = 434;
                ["ruleIsAtCivilTwilightStart"] = nil --[[function]]
;
                ["ruleMatchesTimeRange"] = nil --[[function]]
;
                ["getISO"] = nil --[[function]]
;
                ["day"] = 25;
                ["isToday"] = true;
                ["ruleIsAtCivilNightTime"] = nil --[[function]]
;
                ["week"] = 43;
                ["raw"] = "2020-10-25 18:46:0.434";
                ["ruleIsBeforeSunset"] = nil --[[function]]
;
                ["seconds"] = 0;
                ["ruleMatchesTime"] = nil --[[function]]
;
                ["ruleMatchesHourSpecification"] = nil --[[function]]
;
                ["rawDate"] = "2020-10-25";
                ["year"] = 2020;
                ["ruleMatchesMinuteSpecification"] = nil --[[function]]
;
                ["hour"] = 18;
                ["dayName"] = "Sunday";
                ["isUTC"] = false;
                ["ruleIsAtCivilDayTime"] = nil --[[function]]
;
                ["ruleIsAfterSunset"] = nil --[[function]]
;
                ["matchesRule"] = nil --[[function]]
;
                ["ruleIsAfterCivilTwilightStart"] = nil --[[function]]
;
                ["daysAgo"] = 0;
                ["minutesAgo"] = 0;
                ["time"] = "18:46";
                ["rawDateTime"] = "2020-10-25 18:46:00";
                ["ruleIsAfterSunrise"] = nil --[[function]]
;
                ["ruleIsAtSunrise"] = nil --[[function]]
;
                ["compare"] = nil --[[function]]
;
                ["addMinutes"] = nil --[[function]]
;
                ["ruleIsBeforeCivilTwilightStart"] = nil --[[function]]
;
                ["toUTC"] = nil --[[function]]
;
                ["utils"] = {
                        ["toXML"] = nil --[[function]]
;
                        ["fromJSON"] = nil --[[function]]
;
                        ["leftPad"] = nil --[[function]]
;
                        ["print"] = nil --[[function]]
;
                        ["urlDecode"] = nil --[[function]]
;
                        ["fromXML"] = nil --[[function]]
;
                        ["toCelsius"] = nil --[[function]]
;
                        ["toBase64"] = nil --[[function]]
;
                        ["toStr"] = nil --[[function]]
;
                        ["hsbToRGB"] = nil --[[function]]
;
                        ["cameraExists"] = nil --[[function]]
;
                        ["LOG_MODULE_EXEC_INFO"] = 2;
                        ["stringSplit"] = nil --[[function]]
;
                        ["LOG_INFO"] = 3;
                        ["isXML"] = nil --[[function]]
;
                        ["log"] = nil --[[function]]
;
                        ["urlEncode"] = nil --[[function]]
;
                        ["hardwareExists"] = nil --[[function]]
;
                        ["osExecute"] = nil --[[function]]
;
                        ["updatedBy"] = nil --[[function]]
;
                        ["deviceExists"] = nil --[[function]]
;
                        ["DZVERSION"] = "3.0.15";
                        ["variableExists"] = nil --[[function]]
;
                        ["numDecimals"] = nil --[[function]]
;
                        ["humidityStatus"] = nil --[[function]]
;
                        ["groupExists"] = nil --[[function]]
;
                        ["leadingZeros"] = nil --[[function]]
;
                        ["round"] = nil --[[function]]
;
                        ["inTable"] = nil --[[function]]
;
                        ["dumpSelection"] = nil --[[function]]
;
                        ["dumpTable"] = nil --[[function]]
;
                        ["setLogMarker"] = nil --[[function]]
;
                        ["rgbToHSB"] = nil --[[function]]
;
                        ["stringToSeconds"] = nil --[[function]]
;
                        ["centerPad"] = nil --[[function]]
;
                        ["toJSON"] = nil --[[function]]
;
                        ["isJSON"] = nil --[[function]]
;
                        ["fuzzyLookup"] = nil --[[function]]
;
                        ["rightPad"] = nil --[[function]]
;
                        ["fromBase64"] = nil --[[function]]
;
                        ["osCommand"] = nil --[[function]]
;
                        ["LOG_ERROR"] = 1;
                        ["sceneExists"] = nil --[[function]]
;
                        ["LOG_FORCE"] = 0.5;
                        ["LOG_DEBUG"] = 4;
                        ["LOG_WARNING"] = 3;
                        ["fileExists"] = nil --[[function]]
;
                };
                ["msAgo"] = 0;
                ["current"] = {
                        ["month"] = 10;
                        ["sec"] = 0;
                        ["day"] = 25;
                        ["yday"] = 299;
                        ["year"] = 2020;
                        ["wday"] = 1;
                        ["isdst"] = false;
                        ["hour"] = 18;
                        ["min"] = 46;
                };
                ["ruleIsAtDayTime"] = nil --[[function]]
;
                ["dayAbbrOfWeek"] = "sun";
                ["ruleIsAtSunset"] = nil --[[function]]
;
        };
}
return obj1
The code you could use

Code: Select all

return
{
    on =
    {
        timer =
        {
            'every minute',                -- causes the script to be called every 3 minutes
        },
    },
 
 data = 
 {
    lastResetDateTime =
    {
        initial = 0,
    },
  },

    execute = function(dz)

        if dz.time.matchesRule('every 3 minutes') or dz.data.lastReset == 0 then 
            dz.data.lastResetDateTime = dz.time.rawDateTime -- store timestring in persistent data 
        else
           local lastReset = dz.time.makeTime(dz.data.lastResetDateTime) -- create a time object based on timestring
           dz.log('This persistent data was created: '.. lastReset.minutesAgo ..' minutes ago' , dz.LOG_INFO)
           dz.log('This persistent data was created: '.. lastReset.getISO() , dz.LOG_INFO)
        end
    end
}

Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
ricorico94
Posts: 94
Joined: Monday 26 October 2015 10:41
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: handling time in dzvents  [Solved]

Post by ricorico94 »

Hi Waaren,
Thanks a lot ! Your explanations are very interesting to better understand the mechanics behind and to suggest several ways to proceed !
Thanks again,
Ricorico94
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 1 guest