time difference not work

Moderator: leecollings

Post Reply
foscolino
Posts: 20
Joined: Monday 02 November 2020 18:38
Target OS: Windows
Domoticz version: 2.20
Location: Italy
Contact:

time difference not work

Post by foscolino »

Hi, i'm very frustrated for simple script to send mail if "Antibagno light" is "on" for more one minute. :(
I have used how template garage_door_demo.lua and I have changed only my "name devices" and "time offset", but the script not work.
I don't see any log in my web interface. If I remove "and difference > 60 and difference < 70" the mail was sent correctly.
I don't understand what is the problem, can you help me, please? :roll:

My domoticz installation is 2020.2 on windows system.

This is the script:

Code: Select all

-- script controllo Luci di Casa
t1 = os.time()
s = otherdevices_lastupdate['S1 - Antibagno - 192.168.20.73']
-- returns a date time like 2013-07-11 17:23:12

year = string.sub(s, 1, 4)
month = string.sub(s, 6, 7)
day = string.sub(s, 9, 10)
hour = string.sub(s, 12, 13)
minutes = string.sub(s, 15, 16)
seconds = string.sub(s, 18, 19)
commandArray = {}
t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
difference = (os.difftime (t1, t2))

if (otherdevices['S1 - Antibagno - 192.168.20.73'] == 'On' and difference > 60 and difference < 70) then
   commandArray['SendNotification']='Luce Antibagno#Attenzione, Luce accessa da più di 1 minuto!'
   print('Luce Antibagno accesa da più di 1 minuto')
end 
return commandArray
User avatar
jvdz
Posts: 2328
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: time difference not work

Post by jvdz »

Well, assuming this is a TIME script, your current logic would only send the email in the 10 seconds window, minute after the light was switched.
You probably want it to be "and difference >= 60 and difference <= 120" as the time script only runs one time each 60 seconds.

Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
foscolino
Posts: 20
Joined: Monday 02 November 2020 18:38
Target OS: Windows
Domoticz version: 2.20
Location: Italy
Contact:

Re: time difference not work

Post by foscolino »

Hi jvdz, thank you very much, time difference work correctly, now i have added multiple conditions by "if elseif" how this:

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
all ok, now next step is to turn off the switch after the third hour (3 hours). Do you think that's correct?

Code: Select all

elseif (otherdevices['S1 - Antibagno - 192.168.20.73'] == 'On' and difference > 3600 and difference < 14400) then
     commandArray['S1 - Antibagno - 192.168.20.73']='Off'
 end
User avatar
jvdz
Posts: 2328
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: time difference not work

Post by jvdz »

What about doing that this way?:

Code: Select all

if (otherdevices['S1 - Antibagno - 192.168.20.73'] == 'On' and difference > 3600 then 
   if  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 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 difference < 14400 then
      commandArray['SendNotification']='Luce Antibagno#Attenzione, Luce accesa da più di 3 ore!'
      print('Luce Antibagno accesa da più di 3 ore')
   else
      commandArray['S1 - Antibagno - 192.168.20.73']='Off'
   end
end
.. but doesn't your code send you a notification each minute during the 3 hours and is that what you want?
Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest