JRuby OpenHAB Rules System

I have been severely delinquent in announcing new releases here as we are up to 4.8!
Lots of changes to go over, but I think the biggest one is that these rules has moved from alpha status to beta and are ready to be used in production systems.

Biggest changes since 3.9:

  1. All types and items are now implemented (location, color)
  2. ensure: add ensure feature
  3. timer: supports reentrant timers and timed commands for items
  4. Lots of other fixes, and changes. See changelog for more details.

Special shout out to @ccutrer who came in with his Ruby experience and highly simplified a lot of code and completed adding all types to the language.

The ensure feature performs a check and only sends a command if the item is not already in that state.
For example:
Switch.ensure.on is now valid and will only send the ON command if the Switch is not in the ON state, this replaces having to do Switch.on unless Switch.on? throughout the code. See the docs for more details.

One thing this rules system seeks to do is make everything easier. I, personally, have always found timers in the OpenHAB to complex, error prone and require a lot of boilerplate to accomplish very simple tasks. A couple of changes were introduced in the latest release to address that:

Timers are now reentrant (meaning self resettable) if they are assigned an ‘id’ and are currently running rather than create a new timer they will reschedule. See the docs for more details.

Related to the reentrant timers, all items now have an implicit timer associated with commands. This is modeled by how some automation panels (Leviton OmniPro and others) enable simple timers without the complex logic needed to cancel, reschedule, etc. These ‘timed commands’ automatically cancel the timers if the state of the item is updated in anyway. For ON and OFF commands at the expiration of the timer they change to their inverse state, for other commands they revert to the state they were in when the command was issued.

For example, to turn a light on for 10 minutes:

light.on for: 10.minutes

This will turn the light on for 10 minutes and then turn it off after 10.minutes, if that command is issued again, the 10 minute timer will be reset.

For example, if you have some closet lights you want to turn on for 10 minutes and then turn off if they closet door is closed or if 10 minutes expire, you can do this:

rule 'Closet Door Lights' do
  changed ClosetDoors.members
  run do |event|
     light = items["#{event.item.name}_Light"]
     case event.state
     when OPEN   then light.on for: 10.minutes
     when CLOSED then light.off
     end
  end
  not_if { |event| items["#{event.item.name}_Light"].nil? }
  otherwise { |event| logger.error "No matching light found for closet door #{event.item.name}"  } 
end

No need for complex timer logic, no creating virtual items with the expire binding…

More information is located in the docs.

1 Like