-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEventRunner5.lua
More file actions
168 lines (141 loc) · 8.23 KB
/
Copy pathEventRunner5.lua
File metadata and controls
168 lines (141 loc) · 8.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
_DEVELOP="../hc3emu"
if require and not QuickApp then require('hc3emu') end
--%%name=MyQA
--%%type=com.fibaro.binarySwitch
--%%root=<automatic>
--%%include=Include.lua
--%%name=EventRunner5
--%%uid=UPD896846032517892
--%%save=EventRunner5.fqa
--%%remote=alarms/v1/partitions:1
--%%remote=profiles:*
--%%debug=refresh:false
--%%u1={label='title',text='EventRunner5'}
--%%u2={{button='listRules',text='List rules', onReleased='listRules'},{button='listRulesExt',text='List rules ext.', onReleased='listRulesExt'}}
--%%u3={{button='listTimers',text='List timers', onReleased='listTimers'},{button='listVars',text='List variables', onReleased='listVariables'}}
--%%u4={{button='listRuleStats',text='List stats', onReleased='listRuleStats'},{button='Restart',text='Restart', onReleased='restart'}}
--%%u5={label='stats',text=''}
function QuickApp:main(er) -- Main function, place to define rules
local rule,eval,var,triggerVar,Util = er.eval,er.eval,er.variables,er.triggerVariables,er
self:enableTriggerType({"device","global-variable","custom-event","profile","alarm","weather","location","quickvar","user"}) -- types of events we want
-- example rules for main() - edit and create your own rules here
local HT = { -- Test Home Table with "fake" devices - create your own...
keyfob = 46,
doorSensor = er.createBinaryDevice(),
temp = er.createMultilevelDevice(),
lux = er.createMultilevelDevice(),
roomlight = er.createMultilevelDevice(),
gardenlight = er.createBinaryDevice(),
frontlight = er.createBinaryDevice(),
}
-- er.setTime("03/07/2024 07:00")
-- rule("log(osdate('%c'))")
er.defvars(HT) -- Make HomeTable variables available as variables in rules.
er.reverseMapDef(HT)
--er.setTime("12/01/2023 12:00:00") --mm/dd/yyyy-hh:mm:ss
--er.speedTime(2*24) -- 24 hours
-- rule([[#alarm{id='$id', property='breached'} => -- Log when a partition is breached
-- fibaro.warning(__TAG,efmt('BREACHED partition:%s',env.event.id))
-- ]])
-- rule([[#alarm{property='homeBreached'} => -- Log when home is breached
-- fibaro.warning(__TAG,efmt('BREACHED home'))
-- ]])
local msgOpts = { evalResult=false, userLogColor='yellow' }
local function expr(str) return rule(str,msgOpts) end -- Helper function to create non-logging expr with msgOpts
-- rule("keyfob:central.keyId == 1 => log('Keyfob button 1 pressed')")
-- expr("wait(2); keyfob:sim_pressed=1") -- Fake key press
expr("log('HC3 uptime %s',uptimeStr)")
expr("elog('Sunrise at %t, Sunset at %t',sunrise,sunset)")
expr("log('Weather is %s',weather)")
expr("log('Weather condition is %s',weather:condition)")
expr("log('Temperature is %s°',weather:temperature)")
expr("log('Wind is %sms',weather:wind)")
-- At start, log all devices with battery < 50%
expr("elog('Devices with <50 battery are %l',[_:bat&_:bat<50,fmt('%s:%s',_,_:name) in devices])")
var.i = 0 -- initialize ER variable
--rule("@@00:00:05 => i=i+1; log('ping: %s seconds',i*5)",{ruleTrue=false}) -- test rule, ping every 5 seconds
R = rule("@now+10 => log('Ok %s',now+10)").disable() -- Rule that triggers 10 seconds from now
R.enable()
R._setupDailys(true)
local ruleSet = {
rule("@sunset+00:10 => log('Ok, sunset+00:10')"),
rule("@sunrise-00:10 => log('Ok, sunrise-00:10')")
}
er.disable(ruleSet) -- Disable rules in ruleSet
-- rule("log('Time:%s',http.get('http://worldtimeapi.org/api/timezone/Europe/Stockholm').datetime)")
local ruleOpts = { silent=true }
rule("#UI{cmd='listRules'} => listRules(false)",ruleOpts)
rule("#UI{cmd='listRulesExt'} => listRules(true)",ruleOpts)
rule("#UI{cmd='listVars'} => listVariables()",ruleOpts)
rule("#UI{cmd='listTimers'} => listTimers()",ruleOpts)
rule("#UI{cmd='listRuleStats'} => listStats()",ruleOpts)
rule("#UI{cmd='Restart'} => plugin.restart()",ruleOpts)
end
function QuickApp:onInit()
--fibaro.setupVariableCheck(true,4)
self:EventRunnerEngine( -- Create EventRunner engine and pass callback function
function(er)
-- Settings
-- Global debug flags, can be overridden by ruleOptions
er.debug.ruleTrigger = false -- log rules being triggered
er.debug.ruleTrue = true -- log rules with condition succeeding
er.debug.ruleFalse = true -- log rules with condition failing
er.debug.ruleResult = false -- log results of rules running
er.debug.evalResult = true -- log results of evaluations
er.debug.post = true -- log events being posted
er.debug.sourceTrigger = false -- log incoming sourceTriggers
er.debug.refreshEvents = false -- log incoming refreshEvents
-- Global settings
er.settings.marshall = true -- autoconvert globalVariables values to numbers, booleans, tables when accessed
--er.settings.systemLogTag = "ER"..self.id -- log tag for ER system messages, defaults to __TAG
er.settings.ignoreInvisibleChars = false -- Check code for invisible characters (xC2xA0) before evaluating
er.settings.truncLog = 100 -- truncation of log output
er.settings.truncStr = 80 -- truncation of log strings
-- er.settings.logFibaro = true -- log to fibaro.call, fibaro.setVariable, etc.
-- er.settings.logApi = true -- log all api.* calls
-- er.settings.bannerColor = "orange" -- color of banner in log, defaults to "orange"
-- er.settings.listColor = "purple" -- color of list log (list rules etc), defaults to "purple"
-- er.settings.statsColor = "green" -- color of statistics log, defaults to "green"
-- er.settings.userLogFunction = function(rule,tag,str) return fibaro.debug(tag,str) end -- function to use for user log(), defaults to fibaro.debug if nil
-- Rule/expresion options, overrides global settings when passed to individual rules/expressions. nil means use global setting
-- local expressionOptions = {
-- listCode = true, -- list code when expression/rule is created
-- trace = false, -- trace code when expression/rule is run
-- silent = true, -- don't log rule creation and rule results
-- ruleTrigger = true, -- log rule being triggered
-- ruleTrue = true, -- log rule when condition succeeds
-- ruleFalse = true, -- log rule when condition fails
-- ruleResult = true, -- log result of rule return value
-- evalResult = true, -- log result of expression evaluation
-- userLogColor = 'yellow', -- if set, wraps user log messages in color
-- }
local TRUE = er.color("lightgreen","TRUE")
local FALSE = er.color("lightred","FALSE")
function er.settings.runRuleLogFun(co,rule,ok,event)
co.LOG("%s %s -> %s",ok and TRUE or FALSE,tostring(event) // 20,rule.src:gsub("\n","") // 40)
end
function er.settings.userLogFunction(rule,tag,str) -- custom user log function with color and tag support, #C:color# and #T:tag#
local color = nil
str = str:gsub("(#T:)(.-)(#)",function(_,t) tag=t return "" end)
str = str:gsub("(#C:)(.-)(#)",function(_,c) color=c return "" end)
if color then str=string.format("<font color=%s>%s</font>",color,str) end
fibaro.debug(tag,str);
return str
end
--[[
Setup your own modules like this (or just add rules to main.lua):
In QA file 'u_myrules' (u_ prefix is required)
local prio = 2 -- lower prio loaded first..., prio < 0 loaded before 'main' and prio > 0 loaded after 'main',
local modulename = "House rules"
local function rules(self,er) -- self is the QA self
local rule,eval,var,triggerVar,Util = er.eval,er.eval,er.variables,er.triggerVariables,er
rule(...)
rule(...)
:
end
MODULES = MODULES or {}
MODULES[#MODULES+1]={name=modulename,prio=prio,loader=rules}
--]]
end
)
end