Logic Machine Forum
Script with last updated event - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: Script with last updated event (/showthread.php?tid=777)



Script with last updated event - Mr.D - 06.05.2017

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


RE: Script with last updated event - buuuudzik - 06.05.2017

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



RE: Script with last updated event - Mr.D - 06.05.2017

(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


RE: Script with last updated event - buuuudzik - 07.05.2017

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



RE: Script with last updated event - Mr.D - 11.05.2017

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


RE: Script with last updated event - Jørn - 11.05.2017

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


RE: Script with last updated event - Erwin van der Zwart - 11.05.2017

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


RE: Script with last updated event - Mr.D - 13.05.2017

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


RE: Script with last updated event - Erwin van der Zwart - 13.05.2017

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


RE: Script with last updated event - Mr.D - 16.05.2017

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


RE: Script with last updated event - Erwin van der Zwart - 16.05.2017

Hi,

Do you see errors in the error log?

BR,

Erwin


RE: Script with last updated event - Mr.D - 18.05.2017

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