Skip to main content Link Search Menu Expand Document (external link)

Rule Syntax

require 'openhab'

rule 'name' do |<rule>|
   <zero or more triggers>
   <zero or more execution blocks>
   <zero or more guards>
end

All of the properties that are available to the rule resource are

PropertyTypeLast/MultipleOptionsDefaultDescriptionExamples
descriptionStringSingle  Set the rule description 
tagsString or Array of String or Semantics classSingle  Set the rule tagstags ‘lighting’, ‘security’
everySymbol or DurationMultipleat: String or TimeOfDay When to execute ruleSymbol (:second, :minute, :hour, :day, :week, :month, :year, :monday, :tuesday, :wednesday, :thursday, :friday, :saturday, :sunday) or duration (5.minutes, 20.seconds, 14.hours), at: ‘5:15’ or TimeOfDay(h:5, m:15)
cronStringMultiple  OpenHAB Style Cron Expression’* * * * * * ?’
changedItem or Item Array[] or Group or Group.members or Thing or Thing Array []Multiplefrom: State, to: State, for: Duration Execute rule on item state changeBedroomLightSwitch, from: OFF to ON
updatedItem or Item Array[] or Group or Group.members or Thing or Thing Array []Multipleto: State Execute rule on item updateBedroomLightSwitch, to: ON
received_commandItem or Item Array[] or Group or Group.membersMultiplecommand: Execute rule on item commandBedroomLightSwitch command: ON
channelChannelMultipletriggered: Execute rule on channel trigger'astro:sun:home:rise#event', triggered: 'START'
on_startBooleanSingle falseExecute rule on system starton_start
runBlock passed eventMultiple  Code to execute on rule trigger 
triggeredBlock passed itemMultiple  Code with triggering item to execute on rule trigger 
delayDurationMultiple  Duration to wait between or after run blocksdelay 5.seconds
otherwiseBlock passed eventMultiple  Code to execute on rule trigger if guards are not satisfied 
betweenRange of TimeOfDay or String ObjectsSingle  Only execute rule if current time is between supplied time ranges‘6:05’..’14:05:05’ (Include end) or ‘6:05’…‘14:05:05’ (Excludes end second) or TimeOfDay.new(h:6,m:5)..TimeOfDay.new(h:14,m:15,s:5)
only_ifItem or Item Array, or BlockMultiple  Only execute rule if all supplied items are “On” and/or block returns trueBedroomLightSwitch, BackyardLightSwitch or {BedroomLightSwitch.state == ON}
not_ifItem or Item Array, or BlockMultiple  Do NOT execute rule if any of the supplied items or blocks returns trueBedroomLightSwitch
enabledBooleanSingle trueEnable or disable the rule from executing 

Last means that last value for the property is used
Multiple indicates that multiple entries of the same property can be used in aggregate

An optional variable can be provided to the block to access the rule configuration from within execution blocks and guards.

Terse Rules

If you have a single trigger and execution block, you can use a terse rule:

changed TestSwitch do |event|
  logger.info("TestSwitch changed to #{event.state}")
end

All parameters to the trigger are passed through, and an optional name: parameter is added:

received_command TestSwitch, name: "My Test Switch Rule", command: ON do
  loogger.info("TestSwitch received command ON")
end