Author Topic: PLUGIN: Rules Engine - V0.19.2  (Read 70455 times)

Offline vosmont

  • Hero Member
  • *****
  • Posts: 687
  • Karma: +60/-8
PLUGIN: Rules Engine - V0.19.2
« on: October 28, 2015, 11:48:12 am »
Introduction

This plugin allows to create visually rules for the Vera.
It's designed to work with ALTUI (http://forum.micasaverde.com/index.php/topic,33309.0.html).

For the moment, this is a beta version.

Main notions :
- a rule is active or not active.
- as soon as the condition (calculated from an assembly of conditions) is satisfied, the rule becomes active.
  In contrast, as soon as the condition is no more satisfied, the rule is not active any more.
- actions can be made for the various stages of the life of a rule : at the start, as long as it is active (reminder) and at the end.
- condition can be added to group of actions, in order to discard it even if the rule is still active.
- level for conditions, which allows to make actions just for this level.
- linked rules : ability to use an active rule as a trigger for another.
- enable/disable : status of the rule will change but without effects.
- rule acknowledgement : the actions for reminder event are no more done.
- create visually your rules
- in conditions and actions, first select service/variable, and then the devices implied (by filters)
- the shapes of the items in the editor can be modified with the gear icon

Main benefits :
- no dependence with scenes (can work at the same time)
- no longer need of LUUP reloading
- management of complex conditions
- customizable (actions with LUA code, custom modules, custom Blockly blocks, ...)
- creation and edition made with a graphical editor
- openLuup compliant


Installation

You will find the plugin by different ways :

- Mios Marketplace (0.11) :
    UI5 : http://apps.mios.com/plugin.php?id=8522
    UI7 : "Apps->Install apps" by searching "RulesEngine".

- Alternate App Store on ALTUI (0.12.1)

- Github : https://github.com/vosmont/Vera-Plugin-RulesEngine
  Upload the files in "luup files" in the Vera (by the standard UI in "Apps-->Develop Apps-->Luup files").
  Create a new device in "Apps-->Develop Apps-->Create device", and set "Upnp Device Filename" to "D_RulesEngine1.xml".

Just UI5: There's no JSON decoder installed by default.
If you have the error "No JSON decoder", you have to upload the library in "/usr/lib/lua". You can take "json.lua" or "dkjson.lua".
​You will find "json.lua" here : http://code.mios.com/trac/mios_genericutils/export/20/json.lua
This code can be executed in "Apps->Develop apps->Test Luup code (Lua)"
Code: [Select]
os.execute("wget -q -O /usr/lib/lua/json.lua http://code.mios.com/trac/mios_genericutils/export/20/json.lua")


Usage

Open the page of the rules of the plugin in ALTUI. You can add a new rule : a Blockly editor will be displayed.
Create your rule and press the button "OK", it will be upload on the Vera (into a file "C_RulesEngine_Rules.xml").

You can import rules in the Blockly editor with "Import XML".


Backup

As this plugin is still in development, and important changes can be done between versions, you should really backup regularly your rules.
Just download the file "C_RulesEngine_Rules.xml" (by the standard UI in "Apps-->Develop Apps-->Luup files").
In case of problem, you will be able to import XML fragment of rule (in the rule editor).


ImperiHome

You can visualize your rules on ImperiHome : RulesEngine is compliant with ImperiHome Standard System API (ISS).

Just add an ISS system on ImperiHome with this URL :
- on legacy Vera :
http://{ip}/port_3480/data_request?id=lr_RulesEngine&command=ISS&path=

- on openLuup :
http://{ip}:3480/data_request?id=lr_RulesEngine&command=ISS&path=

For the moment, the rules are in the room "Rules".


Settings

Here is the list of the parameters (variables in advanced panel) :

RuleFiles (default "C_RulesEngine_Rules.xml")
List of the names of the files (separated by commas) containing the xml definition of the rules (Blockly).

StartupFiles (default "C_RulesEngine_Startup.lua")
List of the names of the files (separated by commas) containing the LUA code which has to be executed at the startup of the plugin.
If you define global functions, they will be available in the action of type "LUA function" or custom actions.

For example :
File "C_RulesEngine_Startup.lua"
Code: [Select]
NotificationHelper = {
sendVocal = function (message)
-- Some code to send vocal message
end
}

RulesEngine.addActionType(
"action_vocal",
function (action, context)
local message = RulesEngine.getEnhancedMessage(action.message, context)
RulesEngine.log("Vocal message \"" .. message .. "\"", "ActionType.Vocal", 1)
NotificationHelper.sendVocal(message)
end
)

File "J_RulesEngine1_Blockly_Custom.js" (declared in "ToolboxConfig" variable)
Code: [Select]
//# sourceURL=J_RulesEngine1_Blockly_Custom.js
"use strict";

goog.require( "Blockly.Blocks" );
goog.require( "Blockly.Blocks.actions" );

Blockly.Blocks['action_vocal'] = {
init: function () {
this.setColour(Blockly.Blocks.actions.HUE2);

this.appendDummyInput()
.appendField('Vocal');
this.appendDummyInput()
.appendField('message :')
.appendField(new Blockly.FieldTextArea(''), 'message');

this.setInputsInline(false);
this.setPreviousStatement(true, 'ActionType');
this.setNextStatement(true, 'ActionType');
}
};

Modules
List of modules to import at the startup of the engine.
It is intended for addons (like Virtual Alarm Panel plugin)

ToolboxConfig
A JSON structure defining custom config for the Blockly toolbox.
Code: [Select]
[{"type":"alarm_panel","category":"Properties","resource":"J_RulesEngine1_Blockly_AlarmPanel.js"},{"type":"action_vocal","category":"Actions,Types","resource":"J_RulesEngine1_Blockly_Custom.js"}]


Preview

You can test the rule editor by opening the file "/test/ALTUI/rules.html" in your browser.


Todo

There remains some work :
- save modifications on the rules directly on the Vera.
- find a way for displaying a lot of rules (manipulating several rules into Blockly is a bit hard).
- backup XML files of the rules.
- start/stop rules without Luup reload.
- send history by syslog.


Logs

You can control your rules execution in the logs. Just set the variable "Debug" to a value between 0 and 4.
Then in a ssh terminal :
Code: [Select]
tail -f /var/log/cmh/LuaUPnP.log | grep "^01\|RulesEngine"
« Last Edit: January 25, 2018, 04:36:18 pm by vosmont »

Offline mrv777

  • Full Member
  • ***
  • Posts: 152
  • Karma: +1/-4
Re: PLUGIN: Rules Engine - V0.01
« Reply #1 on: October 28, 2015, 02:01:25 pm »
Interesting, when I get a chance I may give it a try.
I didn't see, but can you do both OR and AND statements?
Example:
Turn Light on if Garage Door is Open OR Laundry Room Door is Open
Turn Light on if Front Door is Open AND Is Night

Even though I haven't tried it yet, thanks for working on it!  :)

Offline vosmont

  • Hero Member
  • *****
  • Posts: 687
  • Karma: +60/-8
Re: PLUGIN: Rules Engine - V0.01
« Reply #2 on: October 28, 2015, 03:01:16 pm »
I've just added some screenshots

Offline mrv777

  • Full Member
  • ***
  • Posts: 152
  • Karma: +1/-4
Re: PLUGIN: Rules Engine - V0.01
« Reply #3 on: October 28, 2015, 03:36:24 pm »
Looks very nice.  Thank you!

Offline CudaNet

  • Hero Member
  • *****
  • Posts: 1401
  • Karma: +42/-10
  • Chimichanga !
Re: PLUGIN: Rules Engine - V0.01
« Reply #4 on: October 28, 2015, 04:40:02 pm »
Awesome job...
openLuup, AltUI, Zway and HomeWave, enough said...

Offline RHINESEL

  • Sr. Member
  • ****
  • Posts: 359
  • Karma: +25/-10
Re: PLUGIN: Rules Engine - V0.01
« Reply #5 on: October 28, 2015, 05:26:26 pm »
Soooo... PLEG alternative?  With a graphical UI?

Offline vosmont

  • Hero Member
  • *****
  • Posts: 687
  • Karma: +60/-8
Re: PLUGIN: Rules Engine - V0.01
« Reply #6 on: October 28, 2015, 05:34:25 pm »
Soooo... PLEG alternative?  With a graphical UI?
That is not intended to replace PLEG. I only share the tool that I use in my system home automation.

Offline tedp

  • Sr. Member
  • ****
  • Posts: 282
  • Karma: +6/-2
Re: PLUGIN: Rules Engine - V0.01
« Reply #7 on: October 28, 2015, 11:57:12 pm »
Wow!!! Between AltUI and this, you all are breathing new life into a tiered product. Way to go!

Offline reneboer

  • Hero Member
  • *****
  • Posts: 1276
  • Karma: +72/-30
Re: PLUGIN: Rules Engine - V0.01
« Reply #8 on: October 29, 2015, 05:16:12 am »
Wow indeed. I think I have to replan my weekend. New things to do  ;D

Hitting the -now virtual- karma button again for you.

Cheers Rene

PS: karma is back, so just applauded you with a click.
« Last Edit: November 08, 2015, 12:53:06 pm by reneboer »
2xVeraLite, VeraEdge, openLuup, ALTUI, 20 switches, 10 dimmers, 20 sensors, 10 scene controllers, 1 Harmony Hub, many plug-ins. Not enough time.

Offline Aaron

  • Hero Member
  • *****
  • Posts: 1985
  • Karma: +61/-193
Re: PLUGIN: Rules Engine - V0.01
« Reply #9 on: October 29, 2015, 10:54:49 pm »
WHOLLY... you guys keep proving how awesome the community coders are, and that the Vera coders are kindergarten level.

Offline vosmont

  • Hero Member
  • *****
  • Posts: 687
  • Karma: +60/-8
Re: PLUGIN: Rules Engine - V0.01
« Reply #10 on: October 30, 2015, 02:35:30 pm »
Changelog:
- Auto-register with ALTUI (no need to modify ALTUI sources)

https://github.com/vosmont/Vera-Plugin-RulesEngine

Offline amg0

  • Sr. Hero Member
  • ******
  • Posts: 2879
  • Karma: +189/-8
Re: PLUGIN: Rules Engine - V0.01
« Reply #11 on: October 30, 2015, 03:06:27 pm »
Impressive !
Merci !

Offline vosmont

  • Hero Member
  • *****
  • Posts: 687
  • Karma: +60/-8
Re: PLUGIN: Rules Engine - V0.01
« Reply #12 on: October 30, 2015, 03:17:12 pm »
Thank you everybody  :)

Keep in mind that it is an early version and that the UI is not succeeded yet.

Offline Zooz

  • Jr. Member
  • **
  • Posts: 80
  • Karma: +1/-0
Re: PLUGIN: Rules Engine - V0.01
« Reply #13 on: November 07, 2015, 04:07:24 pm »
An interesting plugin.
I installed it to check it out but got stuck when opening the panel in ALTUI to create rules. I get "Triggered ajaxError handler" error message. Any idea what is going wrong ::) ?
EDIT: noticed that the files had been updated a couple of days ago, so used them and seems to work now. Just don't know what to do next..
EDIT2: Except now there's nothing in this "Blocky" section. Should one upload also other files to Vera than the 7 luup files? Sorry for bothering with these questions.
« Last Edit: November 17, 2015, 05:50:35 am by Zooz »

Offline vosmont

  • Hero Member
  • *****
  • Posts: 687
  • Karma: +60/-8
Re: PLUGIN: Rules Engine - V0.02
« Reply #14 on: November 08, 2015, 06:08:04 pm »
Version 0.02 is available :
- Some UI improvements
- Upload XML Files

It should be easier to create a new rule.