Comparing stored time with lastUpdate

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

Moderator: leecollings

Post Reply
jsiegmund
Posts: 33
Joined: Sunday 14 May 2017 21:13
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Comparing stored time with lastUpdate

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

Re: Comparing stored time with lastUpdate

Post 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.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
jsiegmund
Posts: 33
Joined: Sunday 14 May 2017 21:13
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Comparing stored time with lastUpdate

Post 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.
User avatar
htilburgs
Posts: 464
Joined: Tuesday 03 November 2015 11:01
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Comparing stored time with lastUpdate

Post 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!
Hardware:
RPi3 - Aeon Labs Z-Stick GEN5 - Fibaro Dimmer 2 - Fibaro Roller Shutter 2 - Fibaro Smoke Sensor - Yeelight RGBW Led bulb - Yeelight Smart LED Light Strip - Neo Coolcam PIR Motion Sensor - Neo Coolcam PowerPlug - Nest Thermostat v3
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Comparing stored time with lastUpdate

Post 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.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
jsiegmund
Posts: 33
Joined: Sunday 14 May 2017 21:13
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Comparing stored time with lastUpdate

Post by jsiegmund »

Sure? I'm pretty sure I was using lastUpdate.minutesAgo up to now.
Freemann
Posts: 94
Joined: Thursday 24 November 2016 14:55
Target OS: Linux
Domoticz version: >=Béta
Location: Netherlands
Contact:

Re: Comparing stored time with lastUpdate

Post 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
Domotica/graphs "freak" :)
NUC8i3BEH(8gb/250gb),
Lubuntu 19.04,
Aeotec Z-Stick S2(Gen5)
HarmonyElite
HUE(5 bulbs, 2 blooms)
NetAtmo(complete setup)
MiLight iboxV6(2 MiLightBulbs)
IP-Cam

https://www.frijduurzaam.nl
jsiegmund
Posts: 33
Joined: Sunday 14 May 2017 21:13
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Comparing stored time with lastUpdate

Post 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
jsiegmund
Posts: 33
Joined: Sunday 14 May 2017 21:13
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Comparing stored time with lastUpdate

Post by jsiegmund »

@dannybloe any comment on the differently named lua stuff?
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Comparing stored time with lastUpdate

Post 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 :)
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
jsiegmund
Posts: 33
Joined: Sunday 14 May 2017 21:13
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Comparing stored time with lastUpdate

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

Re: Comparing stored time with lastUpdate

Post by dannybloe »

Yeah, you are right. I added seconds/minutes/milliseconds in the next update. (min/sec/ms will still be available).
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
jsiegmund
Posts: 33
Joined: Sunday 14 May 2017 21:13
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Comparing stored time with lastUpdate

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

Who is online

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