Page 1 of 1
Newbie in LUA - event script triggering
Posted: Sunday 23 April 2017 12:07
by ensingg
I like to have an LUA event script triggered when a switch state changes.
Can I do that? Or do I have to have an event script running all the time, and watch the state changing?
The problem with that is that the LUA script runs only every minute. Can I change that frequency?
Or better can I trigger an event script when a switch changes. Like a notification?
Re: Newbie in LUA - event script triggering
Posted: Sunday 23 April 2017 12:18
by jannl
If you have a device script it is triggered every time an event is triggered (switch, pir etc). A time script is run every minute. See also the wiki.
Verstuurd vanaf mijn SM-G930F met Tapatalk
Re: Newbie in LUA - event script triggering
Posted: Sunday 23 April 2017 18:25
by ensingg
I think i have to go a step further.
When the script is in the state DEVICE (all/time/device) it will trigger when the state changes. And the function devicechanged is available.
But I like to start a timer after the devicechanged event that should wait for x minutes from this starting point.
Because the devicechanged event wont be activated again in this period a lua script from the that device won't be fired up again.
When I set it to all. An error will occur for the devicechanged = nil every minute that the time event is fired up. And as it seems wont run the rest of the script.
Whats wrong in my ideas?
Re: Newbie in LUA - event script triggering
Posted: Sunday 23 April 2017 20:19
by simonrg
A few thoughts that come to mind from your description.
device scripts only run for a maximum of 10 seconds.
If you want something to happen x minutes after a device has changed state, then have a look at otherdeives_lastupdate on the wiki -
http://www.domoticz.com/wiki/Events, so use a time script which will check every minute and do something if the otherdevice['your device'] is in the state you want it to be in (i.e. 'On') and it is more than x minutes since last update and less than x+1 minutes. There is a funciton timedifference in a number of different scripts which will help - search in the wiki and forum.
Have a go at writing a script, if you are stuck post the script and others will help.
Re: Newbie in LUA - event script triggering
Posted: Monday 24 April 2017 7:10
by jannl
Or create a dummy switch and use something like 'On FOR x'
Verstuurd vanaf mijn SM-G930F met Tapatalk
Re: Newbie in LUA - event script triggering
Posted: Monday 24 April 2017 8:47
by ensingg
Situation: Gate with a magnetic switch
Goal : An notification when it opens. And another alert with it stays open (too long) for so 30 seconds.
I need an direct reaction to the devicechanged so a LUA script of type DEVICE is what I need. A TIMER script will fireup only every minute.
But the device script with fireup only when the gate opens.
A timer script won't react to the devicechange fast enough so its no option.
At the moment I am thinkg of using another dummy switch. Not sure how but maybe trigger a dummy switch with a delay and a notification. Is that the right way?
Re: Newbie in LUA - event script triggering
Posted: Monday 24 April 2017 9:20
by ensingg
Just read about the ON FOR. Cannot test it right now but should this do the trick:
On the devicechange event of de Gateswitch:
ON : set a ON FOR on the dummy switch.
OFF : set dummy switch off (this is when the gate closes within the alert period
And then a devicechange event on the dummy switch that when it changes to OFF to send a notification
Is there a way to change the frequency ? I like to have a period of 30 seconds (< 1 minute)
Re: Newbie in LUA - event script triggering
Posted: Monday 24 April 2017 11:37
by jannl
'Off AFTER xx' is in seconds, 'On FOR xx' is in minutes. It is in the wiki
The solution with just a device event
Posted: Monday 24 April 2017 19:15
by ensingg
-- ~/domoticz/scripts/lua/hekcheck.lua
-- Case : gate with a magnetic sensor
-- Goal : notification when opening AND a notification when open longer than 30 seconds
-- Using a dummy switch "HekMagneet" triggered by a magnetic switch connected to the ESP8266
-- Using a dummy switch "HekTimerSW" triggered with a off AFTER 30
-- On the switch HekMagneet an event when it goes to off
-- On the switch HekTimerSW an event when it goes to off
commandArray = {}
if (devicechanged['HekMagneet'] == 'On') then
print("Hek is dicht")
commandArray['HekTimerSW']='On'
end
if (devicechanged['HekMagneet'] == 'Off') then
print("Hek is open")
commandArray['HekTimerSW']='Off AFTER 30'
end
return commandArray