Page 1 of 1
Comparing stored time with lastUpdate
Posted: Monday 20 November 2017 23:13
by jsiegmund
Help! I've been messing around with time objects but I can't seem to get it to do what I need it to
Basically what I want to calculate is the time between the activation of a PIR sensor and the last update. So what I did:
- When the PIR activates (and triggers the lights) I store the datetime in a global var like this:
Code: Select all
local Time = require('Time')
local currentTime = Time().raw
domoticz.globalData.eye01Activation.add(currentTime)
The reason I use raw in this example is mainly because I could not get it to work...
- Next I have a script which triggers every minute and checks the lastUpdate of the PIR sensor:
Code: Select all
local lastUpdate = eye01.lastUpdate
local Time = require('Time')
local lastActivationRaw = domoticz.globalData.eye01Activation.getLatest().data
domoticz.log('LAST ACTIVATION RAW: ' .. lastActivationRaw)
if (lastActivationRaw ~= nil) then
local lastActivationTime = Time(lastActivationRaw)
local currentTime = Time()
local minutesActivated = lastUpdate.compare(lastActivationTime)
domoticz.log('MINUTES ACTIVATED: ' .. tostring(minutesActivated.minutes))
end
So eye01 is my sensor from which I get the lastUpdate. Next I construct a Time object with the value stored in the global data. This value is correct, I've checked that. Then I try to compare those things. It all works, but the result of minutesActivated.minutes is a nil value. Why? I was expecting the number of minutes of difference between the two time objects. Any ideas?
Disclaimer: I removed some lines which were not relevant to reproduce the issue. It might not compile like this, did not check, the idea is clear I hope

Re: Comparing stored time with lastUpdate
Posted: Tuesday 21 November 2017 7:24
by dannybloe
Oh dear, I just noticed that I made a mistake in the documentation for the compare function. It is not .minutes but .mins (same for .secs and .ms). Sorry about that but that explains why you get the nil error.
Having said that.. why do you store the lastUpdate value manually and not just access it in your timer script from the device:
Code: Select all
local ago = domoticz.devices('eye01').lastUpdate.minutesAgo
That should work or something else is wrong.
Re: Comparing stored time with lastUpdate
Posted: Tuesday 21 November 2017 12:32
by jsiegmund
dannybloe wrote: ↑Tuesday 21 November 2017 7:24
Oh dear, I just noticed that I made a mistake in the documentation for the compare function. It is not .minutes but .mins (same for .secs and .ms). Sorry about that but that explains why you get the nil error.
Having said that.. why do you store the lastUpdate value manually and not just access it in your timer script from the device:
Code: Select all
local ago = domoticz.devices('eye01').lastUpdate.minutesAgo
That should work or something else is wrong.
Thanks!
The reason I do this is that I want to store the time the PIR activated the lights. I do this to enable the following scenario:
- I quickly walk into a room. Lights go on by PIR activation. Walk out again, after 1 min the lights go out.
- I work in a room for a couple of hours. Walk out of the room. Lights stay on at least 10 minutes, I might come back or I'm sitting still so the PIR doesnt notice.
To do this, I need to calculate the amount of time between the first activation and the last time the PIR was updated. That will amount to the total 'activity time' inside that room, so that's the reason I'm storing it.
Thanks for the info, I'm sure I'll get it working now. BTW I personally would find "minutes" more logical than "mins" anyway.
Re: Comparing stored time with lastUpdate
Posted: Tuesday 21 November 2017 12:52
by htilburgs
Thanks!
The reason I do this is that I want to store the time the PIR activated the lights. I do this to enable the following scenario:
- I quickly walk into a room. Lights go on by PIR activation. Walk out again, after 1 min the lights go out.
- I work in a room for a couple of hours. Walk out of the room. Lights stay on at least 10 minutes, I might come back or I'm sitting still so the PIR doesnt notice.
To do this, I need to calculate the amount of time between the first activation and the last time the PIR was updated. That will amount to the total 'activity time' inside that room, so that's the reason I'm storing it.
Thanks for the info, I'm sure I'll get it working now. BTW I personally would find "minutes" more logical than "mins" anyway.
Would be nice if you like to share when you're ready.
Have a simular situation soon with a PIR and I like this approach!
Re: Comparing stored time with lastUpdate
Posted: Tuesday 21 November 2017 13:02
by dannybloe
The reason I use mins instead of minutes is that for lastUpdate etc it is also min, sec, hour etc. That's the same as standard Lua time functions.
Re: Comparing stored time with lastUpdate
Posted: Tuesday 21 November 2017 13:45
by jsiegmund
Sure? I'm pretty sure I was using lastUpdate.minutesAgo up to now.
Re: Comparing stored time with lastUpdate
Posted: Tuesday 21 November 2017 14:17
by Freemann
This is working for weeks now;
Code: Select all
return {
on = {timer = {'Every 5 minutes'}},
execute = function(domoticz)
if (domoticz.devices('RegenSensor1').lastUpdate.minutesAgo >= 15 and domoticz.devices('RegenSensor1').state == 'Off') then
domoticz.devices('RegenSensorMain').switchOff()
domoticz.notify('Het doet niet regenen')
end
end
}
So also usings the
minutesAgo
Re: Comparing stored time with lastUpdate
Posted: Tuesday 21 November 2017 14:45
by jsiegmund
htilburgs wrote: ↑Tuesday 21 November 2017 12:52
Would be nice if you like to share when you're ready.
Have a simular situation soon with a PIR and I like this approach!
Sure, no problem. I've made the habit to put everything in a github repo, so you'll find it there in a couple of days once I have everything working
https://github.com/jsiegmund/domoticz-dzvents
Re: Comparing stored time with lastUpdate
Posted: Monday 27 November 2017 20:25
by jsiegmund
@dannybloe any comment on the differently named lua stuff?
Re: Comparing stored time with lastUpdate
Posted: Monday 27 November 2017 20:34
by dannybloe
yeah, it's minutesAgo. That's correct if that's what you mean. Not what I meant though.. But it is what it is

Re: Comparing stored time with lastUpdate
Posted: Monday 27 November 2017 20:52
by jsiegmund
dannybloe wrote: ↑Monday 27 November 2017 20:34
yeah, it's minutesAgo. That's correct if that's what you mean. Not what I meant though.. But it is what it is
Haha no. What I mean is that it's
minutesAgo and not minsAgo. So the minutes/mins thing is starting to get a little confusing, it should be one or the other.
Anyways; I've updated my repo with the now working scripts! Feel free to use it.
https://github.com/jsiegmund/domoticz-dzvents
Re: Comparing stored time with lastUpdate
Posted: Monday 27 November 2017 21:10
by dannybloe
Yeah, you are right. I added seconds/minutes/milliseconds in the next update. (min/sec/ms will still be available).
Re: Comparing stored time with lastUpdate
Posted: Monday 27 November 2017 21:36
by jsiegmund
dannybloe wrote: ↑Monday 27 November 2017 21:10
Yeah, you are right. I added seconds/minutes/milliseconds in the next update. (min/sec/ms will still be available).
Superb, thanks!