Need to get rid of RulesEngine
-
Hey guys...
Long time...
Since my first day with Vera, I'm using RulesEngine from @vosmont to handle complex rules that will do something based on multiple condition base on "true/false" and also based on time.
Do you think I will be able to do that directly with LUA in openLuup ?
For example..
IF bedroom-motion1 is not detecting motion for 15 minutes
AND
IF bedroom-motion2 is not detecting motion for 15 minutes
AND
IF current-time is between 6am and 11pm
AND
IF binary-light1 is OFF
AND
IF binary-light2 is OFF
THEN
execute LUA code
WAIT 2 minute
execute LUA codeBUT IF any "conditions" failed while in the "THEN" , It need to stop...
I currently have around 60 rules like that
-
With rules like this, having no specific event trigger, it's easy enough to evaluate them periodically (say, every minute?) in a scene.
Using openLuup's object-oriented API, you can make the conditions very readable...
local API = require "openLuup.api" local now = os.time() local m_15_ago = now - 15 * 60 -- 15 minutes ago local hour = os.date "*t" .hour local b1 = API[nnn].security -- bedroom 1 motion sensor local b2 = API[ppp].security -- bedroom 2 motion sensor local bl1 = API[xxx].switch -- binary light 1 local bl2 = API[yyy].switch -- binary light 2 if b1.Tripped == '0' and b1.LastTrip < m_15_ago and b2.Tripped == '0' and b2.LastTrip < m_15_ago and hour >= 6 and hour < 23 and bl1.Status == '0' and bl2.Status == '0' then -- execute LUA code end
I'm a bit unclear about your...
BUT IF any "conditions" failed while in the "THEN" , It need to stop
...but I'm sure you can explain further.
-
With rules like this, having no specific event trigger, it's easy enough to evaluate them periodically (say, every minute?) in a scene.
Using openLuup's object-oriented API, you can make the conditions very readable...
local API = require "openLuup.api" local now = os.time() local m_15_ago = now - 15 * 60 -- 15 minutes ago local hour = os.date "*t" .hour local b1 = API[nnn].security -- bedroom 1 motion sensor local b2 = API[ppp].security -- bedroom 2 motion sensor local bl1 = API[xxx].switch -- binary light 1 local bl2 = API[yyy].switch -- binary light 2 if b1.Tripped == '0' and b1.LastTrip < m_15_ago and b2.Tripped == '0' and b2.LastTrip < m_15_ago and hour >= 6 and hour < 23 and bl1.Status == '0' and bl2.Status == '0' then -- execute LUA code end
I'm a bit unclear about your...
BUT IF any "conditions" failed while in the "THEN" , It need to stop
...but I'm sure you can explain further.
@akbooer said in Need to get rid of RulesEngine:
'm a bit unclear about your...
BUT IF any "conditions" failed while in the "THEN" , It need to stop
...but I'm sure you can explain further.For that, in the "THEN" block, if I have a wait 2 minutes somewhere, and while "waiting" some condititions changes, for example, a motion detector detect something, I need that the "THEN" stop executing.
-
You mean that 2 minutes wait you mention here...
THEN execute LUA code WAIT 2 minute execute LUA code
...you'd just have to re-check the compound condition again before executing the second code block.
So the easiest way would simply be to wrap the test in a function so you can call it in a one-liner...
local API = require "openLuup.api" local now = os.time() local m_15_ago = now - 15 * 60 -- 15 minutes ago local hour = os.date "*t" .hour local b1 = API[nnn].security -- bedroom 1 motion sensor local b2 = API[ppp].security -- bedroom 2 motion sensor local bl1 = API[xxx].switch -- binary light 1 local bl2 = API[yyy].switch -- binary light 2 local function ok_to_go () return b1.Tripped == '0' and b1.LastTrip < m_15_ago and b2.Tripped == '0' and b2.LastTrip < m_15_ago and hour >= 6 and hour < 23 and bl1.Status == '0' and bl2.Status == '0' end if ok_to_go() then -- execute LUA code, block #1 if ok_to_go() then -- execute LUA code, block #2 end end