Page 1 of 1

Storing a Time object as a persistent variable

Posted: Tuesday 31 October 2017 16:17
by garethhowell
I have a dzEvents script that takes action when a PIR fires. To prevent too much activity, I want the script to be able to determine the last time the PIR fired (i.e. before the current activation) and do nothing if this was within (say 60 seconds.

My idea was to have a persistent variable to hold the lastFired time object and then the script can compare now() with that time. (If there's a better weay, let me know)

Unfortunately, I can't see how to initialise the persistent variable to be a Time object. I tried

Code: Select all

    data = {
        TimePIRLastFired = {initial = Time()}
        }
but of course you need to have a

Code: Select all

local Time = require('Time')
somewhere.

Re: Storing a Time object as a persistent variable

Posted: Tuesday 31 October 2017 20:52
by dannybloe
why don’t you use the lastUpdate property of the pir device? That gives you all the info thAt you need.

if myPIR.lastUpdate.secondsAgo>60 then ...

Re: Storing a Time object as a persistent variable

Posted: Wednesday 01 November 2017 8:08
by garethhowell
dannybloe wrote:why don’t you use the lastUpdate property of the pir device? That gives you all the info thAt you need.

if myPIR.lastUpdate.secondsAgo>60 then ...
Thanks, Danny
I guess I mis-interpreted the documentation. I saw that property, but assumed it would be for the current update. Nonsense of course now that I think about it, as it would always be now.
Gareth

Re: Storing a Time object as a persistent variable

Posted: Wednesday 01 November 2017 8:09
by garethhowell
Mind you, that doesn’t answer the actual question of whether you can store an object in a persistent variable?

Re: Storing a Time object as a persistent variable

Posted: Wednesday 01 November 2017 8:09
by dannybloe
yeah, the current update is always 0 seconds ago. In fact, earlier versions of Domoticz had this wrong indeed and it was indeed 0s. We fixed that in one of the latest betas. So now it always reflects the previous update.

Re: Storing a Time object as a persistent variable

Posted: Wednesday 01 November 2017 8:12
by dannybloe
garethhowell wrote: Wednesday 01 November 2017 8:09 Mind you, that doesn’t answer the actual question of whether you can store an object in a persistent variable?
There are several ways to do so. The easiest way is to just store the raw time stamp. Every Time object has a raw property. That's the string that was used to initialise the object. You can store that into the persistent variable and use that value to recreate the object.
Or you can use an historical persistent variable (with max size == 1). Every value in such a variable is automatically time-stamped to the moment it was added.

Re: Storing a Time object as a persistent variable

Posted: Wednesday 01 November 2017 10:27
by garethhowell
dannybloe wrote: Wednesday 01 November 2017 8:12 Or you can use an historical persistent variable (with max size == 1). Every value in such a variable is automatically time-stamped to the moment it was added.
That's neat.