Skip to content
Chris Jackson edited this page Mar 10, 2014 · 1 revision

HABmin provides a rule library to ease the task of creating common rules relating to items. The rule library allows generation of rules in a library, and when the library is updated, all rules can be regenerated. It also takes care of any item name, or other variable data substitution that may be required.

Rules are added using the Rules tab in the item configuration. When you add a rule, HABmin may ask you for parameters for the rule (depending on what the rule does) and these will be added automatically to the rule when it is generated.

Adding Item Rule

Under the hood

The library file

The library file is an XML file as shown below. It has sections for the different parts of the rule, and these are combined when the rule file is produced.

    <rule>
        <name>PowerConsumptionNow</name>
        <label>Calculate current power consumption</label>
        <type>DerivedItem</type>
        <itemtype>SwitchItem</itemtype>
        <itemtype>DimmerItem</itemtype>
        <description>Calculates the power consumed by the item given a user specified maximum power</description>
        <import>org.openhab.core.library.types.*</import>
        <import>org.openhab.model.script.actions.*</import>
        <trigger>Item %%ItemName%% changed</trigger>
        <action>var Number valState = %%ItemName%%.state as DecimalType</action>
        <action>var Number valWatts = valState * %%UserWatts%% / 100</action>
        <action>postUpdate(%%DerivedItem%%, valWatts)</action>
        <variable>
            <name>DerivedItem</name>
            <label>New Item Name</label>
            <type>Number</type>
            <value>%%ItemName%%_Watts</value>
            <description>The name of the new item.</description>
            <scope>Setup</scope>
        </variable>
        <variable>
            <name>UserWatts</name>
            <type>Number</type>
            <label>User defined Watts</label>
            <value>0</value>
            <description>Maximum power the item consumes when ON</description>
            <scope>Item</scope>
        </variable>
    </rule>

Creating the rule file

When HABmin creates the rule file, it looks at all the rules that have been specified, and creates the section with all applicable imports, and then outputs a rule for each item rule.