Page 1 of 1

only run once

Posted: Wednesday 18 November 2020 7:51
by foscolino
Hello, i have this script to check if light is on every first, second, third hours and after send mail notification, but the problems is a "time trigger" and run every minutes. :?
is it possible run this script only run once, not every minutes!
thanks a lot.

Code: Select all

if (otherdevices['S1 - Antibagno - 192.168.20.73'] == 'On' and difference > 3600 and difference < 7200) then
   commandArray['SendNotification']='Luce Antibagno#Attenzione, Luce accesa da più di 1 ora!'
   print('Luce Antibagno accesa da più di 1 ora')
elseif (otherdevices['S1 - Antibagno - 192.168.20.73'] == 'On' and difference > 3600 and difference < 10800) then
   commandArray['SendNotification']='Luce Antibagno#Attenzione, Luce accesa da più di 2 ore!'
   print('Luce Antibagno accesa da più di 2 ora')
elseif (otherdevices['S1 - Antibagno - 192.168.20.73'] == 'On' and difference > 3600 and difference < 14400) then
   commandArray['SendNotification']='Luce Antibagno#Attenzione, Luce accesa da più di 3 ore!'
   print('Luce Antibagno accesa da più di 3 ore')
end

Re: only run once

Posted: Wednesday 18 November 2020 8:36
by waaren
foscolino wrote: Wednesday 18 November 2020 7:51 Hello, i have this script to check if light is on every first, second, third hours and after send mail notification, but the problems is a "time trigger" and run every minutes. :?
is it possible run this script only run once, not every minutes!
It is not possible to prevent the triggering of a time based classic Lua script but you can prevent that the notifications are send and/or the prints going to the log by using the logic from the code below.

Code: Select all


local atMinute = '07'

if os.date('%M') ~= atMinute then
    print ('no execution required')
    return commandArray
end

print ('I will process rest of the script logic now')

if (otherdevices['S1 - Antibagno - 192.168.20.73'] == 'On' and difference > 3600 and difference < 7200) then
   commandArray['SendNotification']='Luce Antibagno#Attenzione, Luce accesa da più di 1 ora!'
   print('Luce Antibagno accesa da più di 1 ora')
elseif (otherdevices['S1 - Antibagno - 192.168.20.73'] == 'On' and difference > 3600 and difference < 10800) then
   commandArray['SendNotification']='Luce Antibagno#Attenzione, Luce accesa da più di 2 ore!'
   print('Luce Antibagno accesa da più di 2 ora')
elseif (otherdevices['S1 - Antibagno - 192.168.20.73'] == 'On' and difference > 3600 and difference < 14400) then
   commandArray['SendNotification']='Luce Antibagno#Attenzione, Luce accesa da più di 3 ore!'
   print('Luce Antibagno accesa da più di 3 ore')
end

return commandArray

.. or use the dzVents Lua framework where you can set the script to only execute once per hour , -day ,-week, etc..

Re: only run once

Posted: Wednesday 18 November 2020 9:03
by foscolino
Hi waaren, thank you very much for your feedback, but I don't understand the logic you wrote :?

local atMinute = '07' is a random value?

if the date minutes is about or equal to seventh minute the script not work, correctly ? ;)
thanks for you support

Re: only run once

Posted: Wednesday 18 November 2020 9:48
by waaren
foscolino wrote: Wednesday 18 November 2020 9:03 local atMinute = '07' is a random value?
Yes atMinute can be anything from '00' to '59'
if the date minutes is about or equal to seventh minute the script not work, correctly ? ;)
No. If the date minutes = '07' the rest of the script will be executed. If the date minutes are not '07' the script will stop without executing the rest of the script

Re: only run once

Posted: Wednesday 18 November 2020 12:37
by foscolino
Hi waaren,
thank you very much, now I understand ;)