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.

Send notification email after change value (delay)
#1
Hello,
I have simple Event-based script (in SL) who sends an email when the status changes in a particular group. This code works pretty well, however I have two questions:
1. Why script works even when the status has not changed. For example, the controller performs a daily scan - and at that time scripts work and send a notification. (status has not changed). How to fix it?
2. How can I put a delay on sending notifications? This means, if the particular group status has switched and is unchanged for 10 seconds, then only send a notification.


Code:
value = event.getvalue()
mydata = storage.get('myobjectdata', 2)
if value then
  if value ~= mydata then
    --email for ON (1)
    subject = 'Main switch = ON'
message = '\nState: Normal'
  mail('1234@yyyyyyy.ww', subject, message) 
  end
else
 
  if value ~= mydata then
   --email for OFF (0)
    subject = 'Main switch = OFF'
message = '\nState: ALARM!!'
    mail(' 1234@yyyyyyy.ww ', subject, message)
  end
end 
storage.set('myobjectdata', value)


Thank you.
Reply
#2
1. Check that you have "Execute on group read" enabled. It might cause the script to run. Enabled logging for the mapped object to see where the value write or read comes from.

2. Set script "Execution mode" to "Last instance only" and use this code:
Code:
curr_value = event.getvalue()
prev_value = storage.get('prev_value')

if curr_value == prev_value then
  return
end

storage.set('prev_value', curr_value)

os.sleep(10)

if curr_value then
  subject = 'Main switch = ON'
  message = '\nState: Normal'
else
  subject = 'Main switch = OFF'
  message = '\nState: ALARM!!'
end

mail('1234@yyyyyyy.ww', subject, message)
Reply
#3
(15.10.2024, 13:47)admin Wrote: 1. Check that you have "Execute on group read" enabled. It might cause the script to run. Enabled logging for the mapped object to see where the value write or read comes from.

2. Set script "Execution mode" to "Last instance only" and use this code:
Code:
curr_value = event.getvalue()
prev_value = storage.get('prev_value')

if curr_value == prev_value then
  return
end

storage.set('prev_value', curr_value)

os.sleep(10)

if curr_value then
  subject = 'Main switch = ON'
  message = '\nState: Normal'
else
  subject = 'Main switch = OFF'
  message = '\nState: ALARM!!'
end

mail('1234@yyyyyyy.ww', subject, message)

1. I already disabled "Execute on group read". In "Object logs" it turns out that the signal from the controller (in this case a very old JUNG unit) is checked and the SL responds ("14/2/188 decoded value 1" and it hasn't changed), and after that script is running.
2. Thanks for the code - everything works. But I probably defined the problem inaccurately - the script should work ONLY if the value of the group has changed and remained for at least 10 seconds. (the script should not work if there is a fast switching ---> from 1 to 0 and back (within 3 sec for example).
Currently, the last value after switching is sent.
Reply
#4
2. Set script "Execution mode" to "Last instance only" and use this code:
------------------------------
Ctrl+F5
Reply
#5
(15.10.2024, 14:31)Daniel Wrote: 2. Set script "Execution mode" to "Last instance only" and use this code:

The specified option is turned on. In this case - after a short status change (3sec) - the last status is sent.
I want to create an algorithm when absolutely nothing should be sent if the contact is fast-switching.
Reply
#6
You can try moving os.sleep(10) to the top of your script.
Reply
#7
(16.10.2024, 06:52)Krists Wrote:
(15.10.2024, 14:31)Daniel Wrote: 2. Set script "Execution mode" to "Last instance only" and use this code:

The specified option is turned on. In this case - after a short status change (3sec) - the last status is sent.
I want to create an algorithm when absolutely nothing should be sent if the contact is fast-switching.

The problem seems to be solved. I moved os.sleep(10) before the first if loop
Reply


Forum Jump: