Newbie in LUA - event script triggering

Topics (not sure which fora)
when not sure where to post, post here and mods will move it to right forum.

Moderators: leecollings, remb0

Post Reply
ensingg
Posts: 65
Joined: Saturday 22 April 2017 17:35
Target OS: Windows
Domoticz version:
Contact:

Newbie in LUA - event script triggering

Post 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?
jannl
Posts: 673
Joined: Thursday 02 October 2014 6:36
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.2
Location: Geleen
Contact:

Re: Newbie in LUA - event script triggering

Post 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
ensingg
Posts: 65
Joined: Saturday 22 April 2017 17:35
Target OS: Windows
Domoticz version:
Contact:

Re: Newbie in LUA - event script triggering

Post 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?
simonrg
Posts: 329
Joined: Tuesday 16 July 2013 22:54
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8807
Location: North East England
Contact:

Re: Newbie in LUA - event script triggering

Post 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.
Raspberry Pi 2 B - 2A@5V PSU - Raspbian + Domoticz + RFXtrx(89), LightwaveRF House(dimmers, sockets, wireless/mood switches), Owl CM113, 4 LaCross Temp / Humidity Sensors, 4 Siemens PIR, Smappee, Solaredge, ESP8266
jannl
Posts: 673
Joined: Thursday 02 October 2014 6:36
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.2
Location: Geleen
Contact:

Re: Newbie in LUA - event script triggering

Post by jannl »

Or create a dummy switch and use something like 'On FOR x'

Verstuurd vanaf mijn SM-G930F met Tapatalk
ensingg
Posts: 65
Joined: Saturday 22 April 2017 17:35
Target OS: Windows
Domoticz version:
Contact:

Re: Newbie in LUA - event script triggering

Post 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?
ensingg
Posts: 65
Joined: Saturday 22 April 2017 17:35
Target OS: Windows
Domoticz version:
Contact:

Re: Newbie in LUA - event script triggering

Post 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)
jannl
Posts: 673
Joined: Thursday 02 October 2014 6:36
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.2
Location: Geleen
Contact:

Re: Newbie in LUA - event script triggering

Post by jannl »

'Off AFTER xx' is in seconds, 'On FOR xx' is in minutes. It is in the wiki
ensingg
Posts: 65
Joined: Saturday 22 April 2017 17:35
Target OS: Windows
Domoticz version:
Contact:

The solution with just a device event

Post 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
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest