Storing a Time object as a persistent variable

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

Moderator: leecollings

Post Reply
garethhowell
Posts: 115
Joined: Tuesday 17 November 2015 21:19
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.9317
Location: St Neots, UK
Contact:

Storing a Time object as a persistent variable

Post 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.
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Storing a Time object as a persistent variable

Post 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 ...
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
garethhowell
Posts: 115
Joined: Tuesday 17 November 2015 21:19
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.9317
Location: St Neots, UK
Contact:

Re: Storing a Time object as a persistent variable

Post 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
garethhowell
Posts: 115
Joined: Tuesday 17 November 2015 21:19
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.9317
Location: St Neots, UK
Contact:

Re: Storing a Time object as a persistent variable

Post by garethhowell »

Mind you, that doesn’t answer the actual question of whether you can store an object in a persistent variable?
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Storing a Time object as a persistent variable

Post 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.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Storing a Time object as a persistent variable

Post 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.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
garethhowell
Posts: 115
Joined: Tuesday 17 November 2015 21:19
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.9317
Location: St Neots, UK
Contact:

Re: Storing a Time object as a persistent variable

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

Who is online

Users browsing this forum: No registered users and 1 guest