This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Script with last updated event
#1
Hi,

I am trying to write a event-script that includes a check if a certain object has been changed/updated within the last 16 hours.

For example:
If a certain door has been opened/changed status within the last 16 hours, and someone sets the house in night mode, then a certain action should be performed.

value = event.getvalue
value1 = 1/1/1 (if door opened within 16 hours == true it not false)

if value == true and value1 == true then
grp.write('3/3/3', 50)
end

Thanks,
Mr.D
Reply
#2
For checking if some object was updated in the last 16 hours you can do this:
Code:
last_timestamp = grp.find("1/1/1").updatetime

now = os.microtime()

if (now - last_timestamp) >= 16*60*60 then updated = true else updated = false end
Reply
#3
(06.05.2017, 13:09)buuuudzik Wrote: For checking if some object was updated in the last 16 hours you can do this:
Code:
last_timestamp = grp.find("1/1/1").updatetime

now = os.microtime()

if (now - last_timestamp) >= 16*60*60 then updated = true else updated = false end

Thanks,

Works like a charm!

BR,
Mr.D
Reply
#4
For such long period(16hours) sometimes you must also consider the time shift. For this you can use:
Code:
-- is daylight saving time?
isdst = os.date('*t').isdst
Reply
#5
Hi,

How do I use the os.microtime() if I would want an event-script to react only if its between certain hours of the day?
For example:
Event-based script
now = os.microtime()
if now == between 2 o'clock and 4 o'clock then
grp.write(1/1/1)
end

BR,
MrBig Grin
Reply
#6
os.microtime just gives you the time in milliseconds.
Maybe the easiest way is to create a new object that is controlled with the scheduler, set it to turn on at 2, and turn off at 4.
This way it will be easier to just change the scheduler than editing the script as well.

Start your script with

if grp.getvalue('Schedule switch') == true then


Mvh Wink

Best regards, Jørn.
Reply
#7
Hi,

Try this:

Code:
-- Settings ------------------------------

output = '1/1/1'
min = 14
max = 16

-- Logic ---------------------------------

now = os.date('*t')
if now.hour >= min and now.hour < max then
  if grp.getvalue(output) == false then
   grp.write(output, true)
  end
else
  if grp.getvalue(output) == true then
    grp.write(output, false)
  end
end

BR,

Erwin
Reply
#8
Hi Erwin,

Thanks for your help.
However I can not get this to work:
Code:
value1 = grp.getvalue('Timer - Settverdi - Alarm')
now = os.date('*t')
min = 20
max = 23
if now.hour >= min and now.hour < max then
grp.write('Timer - Alarm', value1)
script.enable('Alarm - Timer')
end

What am I doing wrong here...?
BR.
Mr.D
Reply
#9
Hi,

Just tested it and the script works as intended, so you might need to check on of these items:

1) Is your script activated?
2) Are the object names in your script correct?
3) Is the object type from object 'Timer - Settverdi - Alarm' the same for object 'Timer - Alarm' ? (can it handle the value from other object)
4) Is your time / timezone correct?

BR,

Erwin
Reply
#10
Hi Erwin,

I cant figure out whats wrong...

1) Is your script activated?
2) Are the object names in your script correct?
3) Is the object type from object 'Timer - Settverdi - Alarm' the same for object 'Timer - Alarm' ? (can it handle the value from other object)
4) Is your time / timezone correct?

For question 1, 2 and 3:
The following code works:
Event-Based
Code:
value1 = grp.getvalue('Timer - Settverdi - Alarm')
if event.getvalue() then
grp.write('Timer - Alarm', value1)
script.enable('Alarm - Timer')
end
This code does not:
Event-Based
Code:
value1 = grp.getvalue('Timer - Settverdi - Alarm')
now = os.date('*t')
min = 20
max = 23
if now.hour >= min and now.hour < max then
grp.write('Timer - Alarm', value1)
script.enable('Alarm - Timer')
end
For question 4:
I have set up the spacelynk with a NTP (time.apple.com) and the timezone is Europe/Oslo. 
The update times for objects are correct as far as I can see.
I have also tried setting min = 01 and max = 23 and still nothing happens... 
BR,
Mr.D
Reply
#11
Hi,

Do you see errors in the error log?

BR,

Erwin
Reply
#12
Hi Erwin,

I could not find anything in the error log.
However, I copied the code and deleted the script. 
Created a new script for the same tag object and pasted the code into the new script.
No changes what so ever to the code, but now it works...

So I am one happy camper Smile
Thanks for your help!

Mr.D
Reply


Forum Jump: