Skip to content

YAML Rules ​

This page describes the rules element for YAML Configuration.

Rules ​

The rules top-level key contains a map of rules, defined by their rule UIDs. The main building blocks of rules are triggers, conditions and actions, collectively called modules.

YAML Definition Syntax ​

yaml
rules:
  <rule_uid>:
    template: <rule_template_uid> # Optional - only used when the rule is based on a rule template
    label: <rule_label> # Required
    description: <rule_description> # Optional
    tags: # Optional
      - <tag_name>
    visibility: <VISIBLE|HIDDEN|EXPERT> # Optional - defaults to VISIBLE
    config: # Optional - used when the rule is based on a rule template
      <config_key>: <config_value>
    triggers:
      - id: <trigger_id> # Optional - module IDs are autogenerated if not specified
        label: <trigger_label> # Optional 
        description: <trigger_description> # Optional
        type: StartLevel # Must be a valid trigger type
        config:
          startlevel: 80 # Required properties depend on the trigger type
    conditions:
      - id: <condition_id> # Optional - module IDs are autogenerated if not specified
        label: <condition_label> # Optional
        description: <condition_description> # Optional
        type: TimeOfDay # Must be a valid condition type
        config:
          startTime: 08:00 # Required properties depend on the condition type
          endTime: 16:00
    actions:
      - id: <action_id> # Optional - module IDs are autogenerated if not specified
        label: <action_label> # Optional
        description: <action_description> # Optional
        type: Script # Must be a valid action type
        config:
          type: Ruby # Required properties depend on the action type
          script: |
            puts "Hello world"
KeyRequiredDescription
<rule_uid>☑The UID of the rule being defined. The UID can be anything as long as it doesn't start or end with whitespace, and doesn't contain / or \.
template☐Contains the UID of the rule template if the rule is linked to one. This field is only relevant for template-based rules.
label☑The rule name/label.
description☐The rule description.
tags☐A list of tags to associate with the rule.
visibility☐Determines if the rule will be displayed in the UI. Rarely used, defaults to VISIBLE.
config☐A key-value map of the rule's configuration. Used with rule templates to specify the values of placeholders.
triggers☐Specify rule triggers. Triggers define when the rule should be executed.
conditions☐Specify rule conditions. Conditions control whether a rule should execute based on specific criteria.
actions☐Specify rule actions. Actions define what happens when a rule is triggered and its conditions are met.

Triggers Section ​

KeyRequiredDescription
id☐The trigger ID. It must be unique within the rule, across triggers, conditions and actions (modules). Will be generated if not specified.
label☐The trigger label.
description☐The trigger description.
type☑The trigger type, which must be a valid/existing trigger type. This is what decides how the trigger works, what mechanism is in use, and which configuration properties must be present.
config▧A key-value map of the trigger's configuration. See the trigger type documentation for which keys exist, are mandatory, and what their values should be.

Trigger Types ​

openHAB can be extended in various ways, so other trigger types than those listed here can be valid in a particular installation. Here is a list of standard trigger types that exist in any installation. The standard trigger types have been given simplified aliases for convenience, but can also be used with their "full ID".

AliasIDDescription
ChannelEventcore.ChannelEventTriggerA channel event trigger, which triggers on specific events from trigger channels.
Crontimer.GenericCronTriggerA time trigger, which triggers at specific times, defined by a Cron expression.
DateTimetimer.DateTimeTriggerA time trigger, which triggers on a DateTimeType state of an Item.
GenericEventcore.GenericEventTriggerAn "advanced" trigger, which triggers on events on the event bus, using various filters to filter out events of interest.
MemberReceivedCommandcore.GroupCommandTriggerA group trigger, which triggers on received commands among group members.
MemberChangedcore.GroupStateChangeTriggerA group trigger, which triggers on state changes among group members.
MemberUpdatedcore.GroupStateUpdateTriggerA group trigger, which triggers on state updates among group members.
ItemReceivedCommandcore.ItemCommandTriggerAn item trigger, which triggers on received commands for an item.
ItemChangedcore.ItemStateChangeTriggerAn item trigger, which triggers on state changes for an item.
ItemUpdatedcore.ItemStateUpdateTriggerAn item trigger, which triggers on state updates for an item.
StartLevelcore.SystemStartlevelTriggerA system trigger, which triggers at specific start levels during openHAB startup.
ThingChangedcore.ThingStatusChangeTriggerA thing trigger, which triggers on status changes for a thing.
ThingUpdatedcore.ThingStatusUpdateTriggerA thing trigger, which triggers on status updates for a thing.
TimeOfDaytimer.TimeOfDayTriggerA time trigger, which triggers based on time of day.

Conditions Section ​

KeyRequiredDescription
id☐The condition ID. It must be unique within the rule, across triggers, conditions and actions (modules). Will be generated if not specified.
label☐The condition label.
description☐The condition description.
type☑The condition type, which must be a valid/existing condition type. This is what decides how the condition works, what mechanism is in use, and which configuration properties must be present.
config▧A key-value map of the condition's configuration. See the condition type documentation for which keys exist, are mandatory, and what their values should be.
inputs☐Advanced: A key-value map for controlling mapping of inputs from other modules. Inputs are automatically mapped if not mapped explicitly.

Condition Types ​

openHAB can be extended in various ways, so other condition types than those listed here can be valid in a particular installation. Here is a list of standard condition types that exist in any installation. The standard condition types have been given simplified aliases for convenience, but can also be used with their "full ID".

AliasIDDescription
DayOfWeektimer.DayOfWeekConditionA condition that will only allow the rule to run on specific days of the week.
Daysetephemeris.DaysetConditionA condition that will only allow the rule to run on days in a specified set.
Holidayephemeris.HolidayConditionA condition that will only allow the rule to run on holidays.
Intervaltimer.IntervalConditionA condition that will only allow the rule to run inside a specified time interval.
ItemStatecore.ItemStateConditionA condition that will only allow the rule to run if a specific Item has a specific state.
NotHolidayephemeris.NotHolidayConditionA condition that will only allow the rule to run on non-holidays.
Scriptscript.ScriptConditionA scripted condition written in any supported scripting language, which can evaluate the condition using custom logic.
ThingStatuscore.ThingStatusConditionA condition that will only allow the rule to run if a specific Thing has a specific status.
TimeOfDaycore.TimeOfDayConditionA condition that will only allow the rule to run within a specified time window.
Weekdayephemeris.WeekdayConditionA condition that will only allow the rule to run on weekdays.
Weekendephemeris.WeekendConditionA condition that will only allow the rule to run on weekends.

Actions Section ​

KeyRequiredDescription
id☐The action ID. It must be unique within the rule, across triggers, conditions and actions (modules). Will be generated if not specified.
label☐The action label.
description☐The action description.
type☑The action type, which must be a valid/existing action type. This is what decides how the action works, what mechanism is in use, and which configuration properties must be present.
config▧A key-value map of the action's configuration. See the action type documentation for which keys exist, are mandatory, and what their values should be.
inputs☐Advanced: A key-value map for controlling mapping of inputs from other modules. Inputs are automatically mapped if not mapped explicitly.

Action Types ​

openHAB can be extended in various ways, so other action types than those listed here can be valid in a particular installation. Here is a list of standard action types that exist in any installation. The standard action types have been given simplified aliases for convenience, but can also be used with their "full ID".

AliasIDDescription
EnableRulecore.RuleEnablementActionAn action that enables or disables rules.
SendCommandcore.ItemCommandActionAn action that sends a command to an Item.
PostUpdatecore.ItemStateUpdateActionAn action that updates an Item state.
Playmedia.PlayActionAn action that plays a media file.
RunRulecore.RunRuleActionAn action that runs another rule and returns the resulting data.
Saymedia.SayActionAn action that uses voice to say something.
Scriptscript.ScriptActionAn action written in any supported scripting language and does whatever the script dictates. This is probably the most widely used action type.

Script Types ​

For scripted conditions and actions, there's an additional type parameter that must be specified under config, to identify the scripting language to use. The identifiers are based on MIME-types, but don't really adhere to any standard. It's up to each scripting add-on to pick a code, and the resulting codes reflect that. To make these easier to use and remember, the type field in a scripting module's config section supports aliases for the most commonly used type. Either the alias or the full type code will work. Other scripting languages can be specified, but the full type code must be used.

AliasType CodeDescription
DSLapplication/vnd.openhab.dsl.ruleRules DSL, openHAB's own scripting language.
Groovyapplication/x-groovyGroovy scripting.
JavaScriptapplication/javascriptJavaScript scripting using GraalJS.
Pythonapplication/pythonPython 3 scripting using GraalPy.
Jythonapplication/x-python2Python 2 scripting using Jython.
Rubyapplication/x-rubyRuby scripting using JRuby.
NashornJSapplication/javascript;version=ECMAScript-5.1JavaScript scripting using Nashorn.

Example ​

yaml
version: 1

rules:
  lights-on:
    label: "Turn on light at sunset"
    description: "This rule turns on the living room light when the sun sets."
    triggers:
      - type: ChannelEvent
        label: Sunset
        config:
          event: START
          channelUID: astro:sun:local:set#event
    actions:
      - type: SendCommand
        config:
          item: LivingRoomLight
          command: ON
  welcome-rule:
    label: Welcome Rule
    description: Welcomes daytime visitors if the house is heated. 
    tags:
      - Welcome
      - Daytime
    triggers:
      - id: startlevel
        label: Start Level Trigger
        description: This trigger triggers at start level 80.
        type: StartLevel
        config:
          startlevel: 80
      - label: Regular Trigger
        description: Triggers at every 30 minutes starting at minute :15, every hour between 08 and 20, of every day.
        type: Cron
        config:
          cronExpression: 0 15/30 8-20 ? * * *
    conditions:
      - id: weekday
        type: Weekday
      - type: TimeOfDay
        label: Daytime
        config:
          startTime: 08:00
          endTime: 20:00
      - type: ItemState
        label: Heating Power Sufficient
        config:
          itemName: CurrentPower
          operator: ">"
          state: "50"
    actions:
      - label: Print
        description: Gives a warm welcome.
        config:
          type: Ruby
          script: |
            puts "Hello and welcome to a heated house"
        type: Script
      - config:
          volume: 80
          sink: enhancedjavasound
          text: Welcome
        type: Say