Logic Machine Forum
Send notification email after change value (delay) - 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: Send notification email after change value (delay) (/showthread.php?tid=5671)



Send notification email after change value (delay) - Krists - 15.10.2024

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.


RE: Send notification email after change value (delay) - admin - 15.10.2024

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)



RE: Send notification email after change value (delay) - Krists - 15.10.2024

(Yesterday, 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.


RE: Send notification email after change value (delay) - Daniel - 15.10.2024

2. Set script "Execution mode" to "Last instance only" and use this code:


RE: Send notification email after change value (delay) - Krists - 16.10.2024

(Yesterday, 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.


RE: Send notification email after change value (delay) - admin - 16.10.2024

You can try moving os.sleep(10) to the top of your script.


RE: Send notification email after change value (delay) - Krists - 16.10.2024

(8 hours ago)Krists Wrote:
(Yesterday, 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