PLUGIN: Rules Engine - V0.19.2

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 :

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)”

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”

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)

//# 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.

[{"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 :

tail -f /var/log/cmh/LuaUPnP.log | grep "^01\|RulesEngine"

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! :slight_smile:

I’ve just added some screenshots

Looks very nice. Thank you!

Awesome job…

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.

Wow!!! Between AltUI and this, you all are breathing new life into a tiered product. Way to go!

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.

WHOLLY… you guys keep proving how awesome the community coders are, and that the Vera coders are kindergarten level.

Changelog:

  • Auto-register with ALTUI (no need to modify ALTUI sources)

Impressive !
Merci !

Thank you everybody :slight_smile:

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

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 ::slight_smile: ?
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.

Version 0.02 is available :

  • Some UI improvements
  • Upload XML Files

It should be easier to create a new rule.

Anyway to get this to work with a non-ALTUI (UI5/7)? definitely heads and shoulders above the current UI7 scene panels. Mike

The engine will work on UI5/UI7, but you need Blockly to edit the rules.
It’s much more simplier to do that with ALTUI, because it knows all your devices.

Anyway, ALTUI is very easy to install and has a little footprint on your Vera (all the job is done by the browser).

Version 0.03 is available :

  • add Imperihome compatibility
  • internal rework for saving rules

Just installed it on my Edge@ui7+AltUI that I do not use productively, looking forward to discover this, looks very prommising, good job!

Wait a little, I’m just about to release a new version. There’s a bug when saving rules.