Page 1 of 1

Lua script - send notification when button is open for 30 seconds

Posted: Thursday 23 March 2017 21:28
by lukcinek
Hi

I have problem to make script to send notification to mail or pushover only when my fridgedoor is open longer than 30s.
Mayby You guys will help me?

Best regards

Re: Lua script - send notification when button is open for 30 seconds

Posted: Thursday 23 March 2017 22:05
by heggink
Dummy switch with on delay? Won't switch on until after 30 seconds..

Re: Lua script - send notification when button is open for 30 seconds

Posted: Friday 24 March 2017 0:17
by lukcinek
Thx for answer but I don't understand :(
Maybe You can write it more precisely ?
Best regards

Edit:
Ok. I know what You mean
It doesn't work
I think maybe becouse my Fridge Door switch is "contact" type
Maybe there is some simple way to do that in lua script?

Re: Lua script - send notification when button is open for 30 seconds

Posted: Friday 24 March 2017 9:13
by jvdz
How does domoticz currently get the information about the refrigerator door status (Open/Close)?
Is it simply a swith is Domoticz of which you want to test whether it is in the Open status for 30 seconds?

Jos

Re: Lua script - send notification when button is open for 30 seconds

Posted: Friday 24 March 2017 9:27
by lvsigo
script_time_demo.lua :shock:

Re: Lua script - send notification when button is open for 30 seconds

Posted: Friday 24 March 2017 9:30
by Bikey
heggink wrote:Dummy switch with on delay? Won't switch on until after 30 seconds..
That's not going to work: it ignores "Off" commands during that period so it will switch on after 30 seconds even if the door was closed again within that time.

Re: Lua script - send notification when button is open for 30 seconds

Posted: Friday 24 March 2017 10:39
by heggink
That's why I suggested a dummy device: a contact device does not have an on/off delay so create a dummy device that has an on delay of 30 seconds and a script behind it. Then build a lua/blocky that switches this device On on open and Off on closed. If the door gets closed before the dummy switches on then the dummy gets switched off and never switches on (the on delay is reset). I just tested it and it works as expected (open window, dummy switches on after 10 secs in my case, close window within 10 secs and the dummy never switches on). I used 2 simple blockies to switch the dummy based on the window sensor.

Re: Lua script - send notification when button is open for 30 seconds

Posted: Friday 24 March 2017 11:13
by Bikey
A contact device indeed does nog have a delay, but you could change it to a regular on/of switch. Then however it does not work because, like I mentioned the device itself seems to ignore the "off" command during its delay time. It does however work - as you suggested - with an additional dummy switch which has a delay.
This script works fine:

Code: Select all

local Notification = 'TestSwitch'
-- set your delay at the switch properties of the 'TestSwitch'

local TestContact = 'TestContact'

commandArray = {}

    if devicechanged[TestContact] == 'On'
    then
        commandArray[Notification]='On'
    end  
    if devicechanged[TestContact] == 'Off'
    then
        commandArray[Notification]='Off'
    end
   if devicechanged[Notification] == 'On'
    then
        print('Door was open too long!')
    end
return commandArray

Re: Lua script - send notification when button is open for 30 seconds

Posted: Friday 24 March 2017 11:26
by heggink
Never tried to switch a 'contact as a regular switch' with a delay. Good to know it does not work that way.

For me, I typically use dummy devices as it allows more granular understanding of where things go wrong in my scripts (I can see the states independently and I can test manually before tying to a real device). Downside is that I have a LOT of dummy devices :-) and probably not so good programming skills that I need all this debugging :D .

Re: Lua script - send notification when button is open for 30 seconds

Posted: Friday 24 March 2017 11:32
by jimtrout87
If you use the Fuction time diff you may be able to do it without a a dummy switch but you would need a variable if you didnt want to be constantly notified. I dont have any contacts so not sure how they report back but using below you should be able to get it working.

Code: Select all

-- script_Fridge.lua

function timedifference(s)
   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)
   t1 = os.time()
   t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
   difference = os.difftime (t1, t2)
   return difference
end
 
commandArray = {}

if (otherdevices['Fridge Conact'] == 'Off' and timedifference(otherdevices_lastupdate['Fidge Contact']) > 30) then
	commandArray['SendNotification']='Fridge Open for more than 30 Seconds'
end

return commandArray

Re: Lua script - send notification when button is open for 30 seconds

Posted: Friday 24 March 2017 21:27
by lukcinek
Hi

I try Your script but I don't know how to define variable and add it to script.
When I use Your script without variable Domoticz hangs and I receive notification all the time.

I will try version with Dummy switch.

EDIT:
Thx for Dummy switch idea but I have strange problem. I made Dummy switch named (Lodówka1). My Fridge door switch is (Drzwi - Lodówka). I made two Blocky Scripts but everytime I activate them and try opem my fridge my Dummy switch do nothing. When I open my Blocky scripts page my new two script are Purple colour and deactivated (mayby someone had this problem - I see Purple colour for he first time)

Thanks Everyone for help
Best regards

Re: Lua script - send notification when button is open for 30 seconds

Posted: Monday 27 March 2017 13:55
by jimtrout87
Not seen purple before but a quick look on Events Wiki - "Purple = Disabled because a missing device was used [ Domoticz stops this Blockly automatic! ]"
Both devices showing in switches ok?


Once sorted you probably want to change the drop down on the right from "All" to "Device" and you could use a else if for the off statement too for the off statement.

If you want to use the time one. Create a variable under Setup > More Options > User Variables > give it a name, type = integer, Value = 0

Code: Select all

- script_Fridge.lua

function timedifference(s)
   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)
   t1 = os.time()
   t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
   difference = os.difftime (t1, t2)
   return difference
end
 
commandArray = {}

if (uservariables['Variable name'] == 0 and otherdevices['Fridge Conact'] == 'Open' and timedifference(otherdevices_lastupdate['Fidge Contact']) > 30) then
   commandArray['SendNotification']='Fridge Open for more than 30 Seconds'
   commandArray['Variable:Variable name'] = '1'
elseif (uservariables['Variable name'] == 1 and otherdevices['Fridge Conact'] == 'Closed' then
   commandArray['Variable:Variable name'] = '0'
end

return commandArray

Re: Lua script - send notification when button is open for 30 seconds

Posted: Monday 27 March 2017 14:00
by lukcinek
Thx a lot for help
I changed All to Device and now everythong works!:)
I will also try Your script some time later and write if works.
EDIT:
It works like a charm:)
There was one bracket missing in Your code (in this line - elseif (uservariables['fridge'] == 1 and otherdevices['Drzwi - Lodówka']) == 'Closed' then - but it works! Thank You very much!
Best regards

Re: Lua script - send notification when button is open for 30 seconds

Posted: Monday 24 April 2017 8:42
by ensingg
Please post your script. I need a similar option. I have a magnetic switch on a gate. I like a notification when the gate if open for more than 30 sec without closing.

I do have a device script that triggers when the gate opens. But where should I put the timer?
When I change the event to a ALL or TIME it will fire up every minute, but the devicechanged command gives a NIL.
And furthermore the interval should be less that the 1 minute for the TIME scripts.